Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.apache.maven.model.building.ModelSource;
import org.apache.maven.model.building.StringModelSource;
import org.apache.maven.model.resolution.ModelResolver;
import org.apache.maven.model.root.RootLocator;
import org.apache.maven.repository.internal.ArtifactDescriptorUtils;
import org.apache.maven.repository.internal.DefaultModelCacheFactory;
import org.apache.maven.repository.internal.ModelCacheFactory;
Expand Down Expand Up @@ -117,6 +118,9 @@ public class DefaultProjectBuilder implements ProjectBuilder {
@Inject
private ModelCacheFactory modelCacheFactory;

@Inject
private RootLocator rootLocator;

// ----------------------------------------------------------------------
// MavenProjectBuilder Implementation
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -166,6 +170,11 @@ private ProjectBuildingResult build(File pomFile, ModelSource modelSource, Inter
project = new MavenProject();
project.setFile(pomFile);

if (pomFile != null) {
project.setRootDirectory(
rootLocator.findRoot(pomFile.toPath().getParent()));
}

DefaultModelBuildingListener listener =
new DefaultModelBuildingListener(project, projectBuildingHelper, projectBuildingRequest);
request.setModelBuildingListener(listener);
Expand Down Expand Up @@ -437,6 +446,10 @@ private boolean build(
MavenProject project = new MavenProject();
project.setFile(pomFile);

if (pomFile != null) {
project.setRootDirectory(rootLocator.findRoot(pomFile.toPath().getParent()));
}

request.setPomFile(pomFile);
request.setTwoPhaseBuilding(true);
request.setLocationTracking(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Path;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -68,6 +69,7 @@
import org.apache.maven.model.Resource;
import org.apache.maven.model.Scm;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.model.root.RootLocator;
import org.apache.maven.project.artifact.InvalidDependencyVersionException;
import org.apache.maven.project.artifact.MavenMetadataSource;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
Expand Down Expand Up @@ -182,6 +184,8 @@ public class MavenProject implements Cloneable {

private final Set<String> lifecyclePhases = Collections.synchronizedSet(new LinkedHashSet<String>());

private Path rootDirectory;

public MavenProject() {
Model model = new Model();

Expand Down Expand Up @@ -1507,6 +1511,30 @@ public void addLifecyclePhase(String lifecyclePhase) {
lifecyclePhases.add(lifecyclePhase);
}

/**
* Gets the root directory of the project, which is the parent directory
* containing the {@code .mvn} directory.
*
* @return the root directory of the project
* @throws IllegalStateException if the root directory could not be found
* @see org.apache.maven.execution.MavenSession#getRootDirectory()
*
* @since 3.10.0
*/
public Path getRootDirectory() {
if (rootDirectory == null) {
throw new IllegalStateException(RootLocator.UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE);
}
return rootDirectory;
}

/**
* @since 3.10.0
*/
public void setRootDirectory(Path rootDirectory) {
this.rootDirectory = rootDirectory;
}

// ----------------------------------------------------------------------------------------------------------------
//
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,13 @@ public void testBuildParentVersionRangeExternallyWithChildRevisionExpression() t

assertEquals("1.0-SNAPSHOT", mp.getVersion());
}

@Test
public void rootDirectoryShouldBeDetected() throws Exception {
File f1 = getTestFile("src/test/resources/projects/root-directory/pom.xml");

MavenProject mp = getProject(f1);

assertEquals(f1.toPath().getParent(), mp.getRootDirectory());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ public void testInterpolationOfBasedirInPomWithUnusualName() throws Exception {
PomTestWrapper pom = buildPom("basedir-interpolation/pom-with-unusual-name.xml");
assertEquals(pom.getBasedir(), new File(pom.getValue("properties/prop0").toString()));
assertEquals(pom.getBasedir(), new File(pom.getValue("properties/prop1").toString()));
assertEquals(pom.getBasedir(), new File(pom.getValue("properties/prop2").toString()));
}

/* MNG-3979 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public void run() {
assertEquals(project.getArtifacts().size(), artifactsResultInAnotherThread.get());
}

@Test
public void testDontResolveDependencies() throws Exception {
File pomFile = new File("src/test/resources/projects/basic-resolveDependencies.xml");
MavenSession mavenSession = createMavenSession(null);
Expand All @@ -150,6 +151,7 @@ public void testDontResolveDependencies() throws Exception {
assertEquals(0, mavenProject.getArtifacts().size());
}

@Test
public void testReadModifiedPoms() throws Exception {
String initialValue = System.setProperty(
DefaultProjectBuilder.DISABLE_GLOBAL_MODEL_CACHE_SYSTEM_PROPERTY, Boolean.toString(true));
Expand Down Expand Up @@ -185,6 +187,7 @@ public void testReadModifiedPoms() throws Exception {
}
}

@Test
public void testReadErroneousMavenProjectContainsReference() throws Exception {
File pomFile = new File("src/test/resources/projects/artifactMissingVersion.xml").getAbsoluteFile();
MavenSession mavenSession = createMavenSession(null);
Expand Down Expand Up @@ -215,6 +218,7 @@ public void testReadErroneousMavenProjectContainsReference() throws Exception {
}
}

@Test
public void testReadInvalidPom() throws Exception {
File pomFile = new File("src/test/resources/projects/badPom.xml").getAbsoluteFile();
MavenSession mavenSession = createMavenSession(null);
Expand All @@ -240,6 +244,7 @@ public void testReadInvalidPom() throws Exception {
}
}

@Test
public void testReadParentAndChildWithRegularVersionSetParentFile() throws Exception {
List<File> toRead = new ArrayList<>(2);
File parentPom = getProject("MNG-6723");
Expand Down Expand Up @@ -290,6 +295,7 @@ private void assertResultShowNoError(List<ProjectBuildingResult> results) {
}
}

@Test
public void testBuildProperties() throws Exception {
File file = new File(getProject("MNG-6716").getParentFile(), "project/pom.xml");
MavenSession mavenSession = createMavenSession(null);
Expand All @@ -305,6 +311,7 @@ public void testBuildProperties() throws Exception {
assertEquals(1, project.getResources().size());
}

@Test
public void testPropertyInPluginManagementGroupId() throws Exception {
File pom = getProject("MNG-6983");

Expand All @@ -316,6 +323,7 @@ public void testPropertyInPluginManagementGroupId() throws Exception {
}
}

@Test
public void testLocationTrackingResolution() throws Exception {
File pom = getProject("MNG-7648");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ public Artifact createArtifactWithClassifier(

public ArtifactRepository createDefaultLocalRepository() throws InvalidRepositoryException {
return createLocalRepository(
new File(System.getProperty("basedir", ""), "target/local-repo").getAbsoluteFile());
new File(System.getProperty("basedir", "."), "target/local-repo").getAbsoluteFile());
}

public ArtifactRepository createDefaultRemoteRepository() throws InvalidRepositoryException {
return new MavenArtifactRepository(
DEFAULT_REMOTE_REPO_ID,
"file://"
+ new File(System.getProperty("basedir", ""), "src/test/remote-repo")
+ new File(System.getProperty("basedir", "."), "src/test/remote-repo")
.toURI()
.getPath(),
new DefaultRepositoryLayout(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ under the License.
<!-- this is where we collect all the interpolated values for the POM dump -->
<prop0>${basedir}</prop0>
<prop1>${project.basedir}</prop1>
<prop2>${project.rootDirectory}</prop2>
</properties>
</project>
Empty file.
6 changes: 6 additions & 0 deletions maven-core/src/test/resources/projects/root-directory/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>root-directory</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
32 changes: 10 additions & 22 deletions maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -88,6 +89,7 @@
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.message.MessageBuilder;
import org.apache.maven.model.building.ModelProcessor;
import org.apache.maven.model.root.RootLocator;
import org.apache.maven.project.MavenProject;
import org.apache.maven.properties.internal.EnvironmentUtils;
import org.apache.maven.properties.internal.SystemProperties;
Expand Down Expand Up @@ -148,9 +150,6 @@ public class MavenCli {

private static final String DOT_MVN = ".mvn";

private static final String UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE = "Unable to find the root directory. Create a "
+ DOT_MVN + " directory in the project root directory to identify it.";

private static final String PROJECT_EXTENSIONS_FILENAME = DOT_MVN + "/extensions.xml";

private static final File USER_EXTENSIONS_FILE = new File(USER_MAVEN_CONFIGURATION_HOME, "extensions.xml");
Expand Down Expand Up @@ -356,10 +355,13 @@ void initialize(CliRequest cliRequest) throws ExitException {
}
topDirectory = getCanonicalPath(topDirectory);
cliRequest.request.setTopDirectory(topDirectory);
// We're very early in the process and we don't have the container set up yet,
// so we on searchAcceptableRootDirectory method to find us acceptable directory.
// The method may return null if nothing acceptable found.
cliRequest.request.setRootDirectory(searchAcceptableRootDirectory(topDirectory));
// We're very early in the process, and we don't have the container set up yet,
// so we rely on the JDK services to eventually look up a custom RootLocator.
// This is used to compute {@code session.rootDirectory} but all {@code project.rootDirectory}
// properties will be computed through the RootLocator found in the container.
RootLocator rootLocator =
ServiceLoader.load(RootLocator.class).iterator().next();
cliRequest.request.setRootDirectory(rootLocator.findRoot(topDirectory));

//
// Make sure the Maven home directory is an absolute path to save us from confusion with say drive-relative
Expand Down Expand Up @@ -625,7 +627,7 @@ void properties(CliRequest cliRequest) throws ExitException {
System.err.println(message);
if (cliRequest.request.getRootDirectory() == null) {
System.err.println();
System.err.println(UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE);
System.err.println(RootLocator.UNABLE_TO_FIND_ROOT_PROJECT_MESSAGE);
}
throw new ExitException(1); // user error
}
Expand Down Expand Up @@ -1637,20 +1639,6 @@ static void populateProperties(CliRequest cliRequest, Properties systemPropertie
systemProperties.setProperty("maven.build.version", mavenBuildVersion);
}

protected boolean isAcceptableRootDirectory(Path path) {
return path != null && Files.isDirectory(path.resolve(DOT_MVN));
}

protected Path searchAcceptableRootDirectory(Path path) {
if (path == null) {
return null;
}
if (isAcceptableRootDirectory(path)) {
return path;
}
return searchAcceptableRootDirectory(path.getParent());
}

protected static StringSearchInterpolator createInterpolator(CliRequest cliRequest, Properties... properties) {
StringSearchInterpolator interpolator = new StringSearchInterpolator();
interpolator.addValueSource(new AbstractValueSource(false) {
Expand Down
4 changes: 4 additions & 0 deletions maven-model-builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ under the License.
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.plexus</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
import org.apache.maven.model.profile.activation.OperatingSystemProfileActivator;
import org.apache.maven.model.profile.activation.ProfileActivator;
import org.apache.maven.model.profile.activation.PropertyProfileActivator;
import org.apache.maven.model.root.DefaultRootLocator;
import org.apache.maven.model.root.RootLocator;
import org.apache.maven.model.superpom.DefaultSuperPomProvider;
import org.apache.maven.model.superpom.SuperPomProvider;
import org.apache.maven.model.validation.DefaultModelValidator;
Expand Down Expand Up @@ -113,7 +115,9 @@ protected ProfileActivator[] newProfileActivators() {
}

protected ProfileActivationFilePathInterpolator newProfileActivationFilePathInterpolator() {
return new ProfileActivationFilePathInterpolator().setPathTranslator(newPathTranslator());
return new ProfileActivationFilePathInterpolator()
.setPathTranslator(newPathTranslator())
.setRootLocator(newRootLocator());
}

protected UrlNormalizer newUrlNormalizer() {
Expand All @@ -124,13 +128,18 @@ protected PathTranslator newPathTranslator() {
return new DefaultPathTranslator();
}

private RootLocator newRootLocator() {
return new DefaultRootLocator();
}

protected ModelInterpolator newModelInterpolator() {
UrlNormalizer normalizer = newUrlNormalizer();
PathTranslator pathTranslator = newPathTranslator();
return new StringVisitorModelInterpolator()
.setPathTranslator(pathTranslator)
.setUrlNormalizer(normalizer)
.setVersionPropertiesProcessor(newModelVersionPropertiesProcessor());
.setVersionPropertiesProcessor(newModelVersionPropertiesProcessor())
.setRootLocator(newRootLocator());
}

protected ModelVersionProcessor newModelVersionPropertiesProcessor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.path.PathTranslator;
import org.apache.maven.model.path.UrlNormalizer;
import org.apache.maven.model.root.RootLocator;
import org.codehaus.plexus.interpolation.AbstractValueSource;
import org.codehaus.plexus.interpolation.InterpolationPostProcessor;
import org.codehaus.plexus.interpolation.MapBasedValueSource;
Expand Down Expand Up @@ -81,6 +82,9 @@ public abstract class AbstractStringBasedModelInterpolator implements ModelInter
@Inject
private ModelVersionProcessor versionProcessor;

@Inject
private RootLocator rootLocator;

public AbstractStringBasedModelInterpolator setPathTranslator(PathTranslator pathTranslator) {
this.pathTranslator = pathTranslator;
return this;
Expand All @@ -96,6 +100,11 @@ public AbstractStringBasedModelInterpolator setVersionPropertiesProcessor(ModelV
return this;
}

public AbstractStringBasedModelInterpolator setRootLocator(RootLocator rootLocator) {
this.rootLocator = rootLocator;
return this;
}

protected List<ValueSource> createValueSources(
final Model model,
final File projectDir,
Expand Down Expand Up @@ -131,6 +140,23 @@ public Object getValue(String expression) {
true);
valueSources.add(basedirValueSource);

ValueSource rootDirectoryValueSource = new PrefixedValueSourceWrapper(
new AbstractValueSource(false) {
@Override
public Object getValue(String expression) {
if ("rootDirectory".equals(expression)) {
return rootLocator
.findMandatoryRoot(projectDir.toPath())
.toAbsolutePath()
.toString();
}
return null;
}
},
PROJECT_PREFIXES,
true);
valueSources.add(rootDirectoryValueSource);

ValueSource baseUriValueSource = new PrefixedValueSourceWrapper(
new AbstractValueSource(false) {
@Override
Expand Down
Loading