Skip to content

Commit

Permalink
[MNG-6723] MavenProject.getParentFile() not set when using ProjectBui…
Browse files Browse the repository at this point in the history
…lder.build()

This closes #273
  • Loading branch information
mickaelistria authored and michael-o committed Jul 25, 2019
1 parent 690841e commit 2deeba3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,10 @@ private void initParent( MavenProject project, Map<String, MavenProject> project
}
}
project.setParent( parent );
if ( project.getParentFile() == null && parent != null )
{
project.setParentFile( parent.getFile() );
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -245,4 +246,57 @@ public void testReadInvalidPom()
}
}

public void testReadParentAndChildWithRegularVersionSetParentFile()
throws Exception
{
List<File> 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<ProjectBuildingResult> 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<ProjectBuildingResult> 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<ProjectBuildingResult> results) {
for ( ProjectBuildingResult result : results ) {
if ( result.getPomFile().getParentFile().getName().equals( "child" ) ) {
return result.getProject();
}
}
return null;
}

private void assertResultShowNoError(List<ProjectBuildingResult> results) {
for ( ProjectBuildingResult result : results ) {
assertTrue( result.getProblems().isEmpty() );
assertNotNull( result.getProject() );
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>example.eclipse-548652</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>child</artifactId>
<packaging>jar</packaging>
</project>
11 changes: 11 additions & 0 deletions maven-core/src/test/projects/project-builder/MNG-6723/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example.eclipse-548652</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>child</module>
</modules>
</project>

0 comments on commit 2deeba3

Please sign in to comment.