diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java index a3188212c7..37d51cd49d 100644 --- a/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java @@ -39,6 +39,8 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Plugin; import org.apache.maven.model.building.FileModelSource; import org.apache.maven.model.building.ModelBuildingRequest; +import org.apache.maven.model.building.ModelProblem; +import org.apache.maven.model.building.ModelProblem.Severity; import org.apache.maven.model.building.ModelSource; import org.apache.maven.shared.utils.io.FileUtils; @@ -83,6 +85,30 @@ public class ProjectBuilderTest assertNotNull( result.getProject().getParentFile() ); } + public void testBuildFromModelSourceResolvesBasedir() + throws Exception + { + File pomFile = new File( "src/test/resources/projects/modelsourcebasedir/pom.xml" ); + MavenSession mavenSession = createMavenSession( null ); + ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest(); + configuration.setRepositorySession( mavenSession.getRepositorySession() ); + ModelSource modelSource = new FileModelSource( pomFile ); + ProjectBuildingResult result = + getContainer().lookup( org.apache.maven.project.ProjectBuilder.class ).build( modelSource, configuration ); + + assertEquals( pomFile.getAbsoluteFile(), result.getProject().getModel().getPomFile().getAbsoluteFile() ); + int errors = 0; + for ( ModelProblem p : result.getProblems() ) + { + if ( p.getSeverity() == Severity.ERROR ) + { + errors++; + } + } + assertEquals( 0, errors ); + } + + public void testVersionlessManagedDependency() throws Exception { @@ -160,7 +186,7 @@ public class ProjectBuilderTest File parent = new File( tempDir.toFile(), "pom.xml" ); String parentContent = FileUtils.fileRead( parent ); parentContent = parentContent.replaceAll( "pom", - "pomaddedValue" ); + "pomaddedValue" ); FileUtils.fileWrite( parent, "UTF-8", parentContent ); // re-build pom with modified parent ProjectBuildingResult result = projectBuilder.build( child, configuration ); diff --git a/maven-core/src/test/resources/projects/modelsourcebasedir/pom.xml b/maven-core/src/test/resources/projects/modelsourcebasedir/pom.xml new file mode 100644 index 0000000000..ec0f7d385a --- /dev/null +++ b/maven-core/src/test/resources/projects/modelsourcebasedir/pom.xml @@ -0,0 +1,19 @@ + + 4.0.0 + test.readparent + local-parent + pom + 1.0 + + + + blah + blah + 0.0.1-SNASPSHOT + system + ${basedir}/blah.jar + + + + diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java index f981944f94..a8568efb27 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java @@ -617,7 +617,14 @@ public class DefaultModelBuilder throw problems.newModelBuildingException(); } - model.setPomFile( pomFile ); + if ( pomFile != null ) + { + model.setPomFile( pomFile ); + } + else if ( modelSource instanceof FileModelSource ) + { + model.setPomFile( ( (FileModelSource) modelSource ).getFile() ); + } problems.setSource( model ); modelValidator.validateRawModel( model, request, problems );