[MNG-7170] Allow to associate pomFile/${basedir} with DefaultProjectBuilder.build(ModelSource, ...)

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

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

This closes #478
This commit is contained in:
Mickael Istria 2021-06-11 11:55:09 +02:00 committed by Michael Osipov
parent 3a465e1477
commit 5a89973126
3 changed files with 54 additions and 2 deletions

View File

@ -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( "<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 );

View File

@ -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>

View File

@ -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 );