Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[MNG-7170] Allow to associate pomFile/${basedir} with DefaultProjectB…
…uilder.build(ModelSource, ...)

Actually a subset backport of MNG-5669 (5cdb833)

Also-By: rfscholte <rfscholte@apache.org>

This closes #478
  • Loading branch information
mickaelistria authored and michael-o committed Jun 26, 2021
1 parent 3a465e1 commit 5a89973
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
Expand Up @@ -39,6 +39,8 @@
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;

Expand Down Expand Up @@ -83,6 +85,30 @@ public void testBuildFromModelSource()
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
{
Expand Down Expand Up @@ -160,7 +186,7 @@ public void testReadModifiedPoms() throws Exception {
File parent = new File( tempDir.toFile(), "pom.xml" );
String parentContent = FileUtils.fileRead( parent );
parentContent = parentContent.replaceAll( "<packaging>pom</packaging>",
"<packaging>pom</packaging><properties><addedProperty>addedValue</addedProperty></properties>" );
"<packaging>pom</packaging><properties><addedProperty>addedValue</addedProperty></properties>" );
FileUtils.fileWrite( parent, "UTF-8", parentContent );
// re-build pom with modified parent
ProjectBuildingResult result = projectBuilder.build( child, configuration );
Expand Down
19 changes: 19 additions & 0 deletions maven-core/src/test/resources/projects/modelsourcebasedir/pom.xml
@@ -0,0 +1,19 @@
<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>test.readparent</groupId>
<artifactId>local-parent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>blah</groupId>
<artifactId>blah</artifactId>
<version>0.0.1-SNASPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/blah.jar</systemPath>
</dependency>
</dependencies>

</project>
Expand Up @@ -617,7 +617,14 @@ private Model readModel( ModelSource modelSource, File pomFile, ModelBuildingReq
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 );
Expand Down

1 comment on commit 5a89973

@pzygielo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it causes something like eclipse-ee4j/eclipselink#1242 (which might be the fault of https://github.com/jdcasey/directory-maven-plugin).

Please sign in to comment.