From 809cac22661ab616340b9507ba7542b534ea9578 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Fri, 19 Jul 2019 14:16:16 +0200 Subject: [PATCH] [MNG-6723] MavenProject.getParentFile() not set when using ProjectBuilder.build() This closes #273 --- .../maven/project/DefaultProjectBuilder.java | 4 ++ .../maven/project/ProjectBuilderTest.java | 61 +++++++++++++++++++ .../project-builder/MNG-6723/child/pom.xml | 11 ++++ .../projects/project-builder/MNG-6723/pom.xml | 11 ++++ 4 files changed, 87 insertions(+) create mode 100644 maven-core/src/test/projects/project-builder/MNG-6723/child/pom.xml create mode 100644 maven-core/src/test/projects/project-builder/MNG-6723/pom.xml diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index 400b7166fe..51c90ccf0a 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -976,6 +976,10 @@ public class DefaultProjectBuilder } } project.setParent( parent ); + if ( project.getParentFile() == null && parent != null ) + { + project.setParentFile( parent.getFile() ); + } } } 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 a7ed939bb5..95c3149f87 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 @@ -20,6 +20,7 @@ package org.apache.maven.project; */ import java.io.File; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Properties; @@ -245,4 +246,64 @@ public class ProjectBuilderTest } } + public void testReadParentAndChildWithRegularVersionSetParentFile() + throws Exception + { + List toRead = new ArrayList<>( 2 ); + File parentPom = getProject( "MNG-6723" ); + toRead.add( parentPom ); + toRead.add( new File( parentPom.getParentFile(), "child/pom.xml" ) ); + MavenSession mavenSession = createMavenSession( null ); + ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest(); + configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL ); + configuration.setRepositorySession( mavenSession.getRepositorySession() ); + org.apache.maven.project.ProjectBuilder projectBuilder = + lookup( org.apache.maven.project.ProjectBuilder.class ); + + // read poms separately + boolean parentFileWasFoundOnChild = false; + for ( File file : toRead ) + { + List results = projectBuilder.build( Collections.singletonList( file ), false, configuration ); + assertResultShowNoError( results ); + MavenProject project = findChildProject( results ); + if ( project != null ) + { + assertEquals( parentPom, project.getParentFile() ); + parentFileWasFoundOnChild = true; + } + } + assertTrue( parentFileWasFoundOnChild ); + + // read projects together + List results = projectBuilder.build( toRead, false, configuration ); + assertResultShowNoError( results ); + assertEquals( parentPom, findChildProject( results ).getParentFile() ); + Collections.reverse( toRead ); + results = projectBuilder.build( toRead, false, configuration ); + assertResultShowNoError( results ); + assertEquals( parentPom, findChildProject( results ).getParentFile() ); + } + + private MavenProject findChildProject( List results ) + { + for ( ProjectBuildingResult result : results ) + { + if ( result.getPomFile().getParentFile().getName().equals( "child" ) ) + { + return result.getProject(); + } + } + return null; + } + + private void assertResultShowNoError(List results) + { + for ( ProjectBuildingResult result : results ) + { + assertTrue( result.getProblems().isEmpty() ); + assertNotNull( result.getProject() ); + } + } + } diff --git a/maven-core/src/test/projects/project-builder/MNG-6723/child/pom.xml b/maven-core/src/test/projects/project-builder/MNG-6723/child/pom.xml new file mode 100644 index 0000000000..20284eb993 --- /dev/null +++ b/maven-core/src/test/projects/project-builder/MNG-6723/child/pom.xml @@ -0,0 +1,11 @@ + + + 4.0.0 + + example.eclipse-548652 + parent + 0.0.1-SNAPSHOT + + child + jar + diff --git a/maven-core/src/test/projects/project-builder/MNG-6723/pom.xml b/maven-core/src/test/projects/project-builder/MNG-6723/pom.xml new file mode 100644 index 0000000000..e21f824c52 --- /dev/null +++ b/maven-core/src/test/projects/project-builder/MNG-6723/pom.xml @@ -0,0 +1,11 @@ + + + 4.0.0 + example.eclipse-548652 + parent + 0.0.1-SNAPSHOT + pom + + child + +