-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make rootDirectory mandatory (#1787)
Co-authored-by: Guillaume Nodet <gnodet@gmail.com>
- Loading branch information
Showing
23 changed files
with
188 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
maven-api-impl/src/main/java/org/apache/maven/api/services/model/RootDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.maven.api.services.model; | ||
|
||
import java.nio.file.Path; | ||
|
||
import org.apache.maven.api.Service; | ||
|
||
/** | ||
* Interface used to detect is a given directory "root directory". | ||
*/ | ||
public interface RootDetector extends Service { | ||
boolean isRootDirectory(Path dir); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...pl/src/main/java/org/apache/maven/internal/impl/model/rootlocator/DefaultRootLocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.maven.internal.impl.model.rootlocator; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.ServiceLoader; | ||
|
||
import org.apache.maven.api.annotations.Nonnull; | ||
import org.apache.maven.api.di.Named; | ||
import org.apache.maven.api.services.model.RootDetector; | ||
import org.apache.maven.api.services.model.RootLocator; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
@Named | ||
public class DefaultRootLocator implements RootLocator { | ||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private final List<RootDetector> rootDetectors; | ||
|
||
public DefaultRootLocator() { | ||
this.rootDetectors = ServiceLoader.load(RootDetector.class).stream() | ||
.map(ServiceLoader.Provider::get) | ||
.toList(); | ||
} | ||
|
||
@Nonnull | ||
public Path findMandatoryRoot(@Nonnull Path basedir) { | ||
requireNonNull(basedir, "basedir is null"); | ||
Path rootDirectory = basedir; | ||
while (rootDirectory != null && !isRootDirectory(rootDirectory)) { | ||
rootDirectory = rootDirectory.getParent(); | ||
} | ||
Optional<Path> rdf = getMultiModuleProjectDirectory(); | ||
if (rootDirectory == null) { | ||
logger.warn(getNoRootMessage()); | ||
rootDirectory = rdf.orElseGet(() -> Paths.get("").toAbsolutePath()); | ||
} else { | ||
if (rdf.isPresent() && !Objects.equals(rootDirectory, rdf.get())) { | ||
logger.warn("Project root directory and multiModuleProjectDirectory are not aligned"); | ||
} | ||
} | ||
return rootDirectory; | ||
} | ||
|
||
protected boolean isRootDirectory(Path dir) { | ||
requireNonNull(dir, "dir is null"); | ||
for (RootDetector rootDetector : rootDetectors) { | ||
if (rootDetector.isRootDirectory(dir)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
protected Optional<Path> getMultiModuleProjectDirectory() { | ||
String mmpd = System.getProperty("maven.multiModuleProjectDirectory"); | ||
if (mmpd != null) { | ||
return Optional.of(Paths.get(mmpd)); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...pl/src/main/java/org/apache/maven/internal/impl/model/rootlocator/DotMvnRootDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.maven.internal.impl.model.rootlocator; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import org.apache.maven.api.di.Named; | ||
import org.apache.maven.api.services.model.RootDetector; | ||
|
||
@Named | ||
public class DotMvnRootDetector implements RootDetector { | ||
@Override | ||
public boolean isRootDirectory(Path dir) { | ||
return Files.isDirectory(dir.resolve(".mvn")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...mpl/src/main/resources/META-INF/services/org.apache.maven.api.services.model.RootDetector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.apache.maven.internal.impl.model.rootlocator.DotMvnRootDetector | ||
org.apache.maven.internal.impl.model.rootlocator.PomXmlRootDetector |
1 change: 1 addition & 0 deletions
1
...impl/src/main/resources/META-INF/services/org.apache.maven.api.services.model.RootLocator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.apache.maven.internal.impl.model.rootlocator.DefaultRootLocator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.