[MNG-2199] Support version ranges in parent elements

o Updated to fix parent version range resolution broken since 3.2.3 and to
  remove parent version range resolution logic obsolete since Maven
  3.2.3 which changed the initialization of MavenProject instances.
o Updated local parent resolution to behave the same way remote parent
  resolution behaves. When referencing a parent using version ranges,
  inheriting the version or using version expressions should not be supported.
  It has been implemented that way for remote parent resolution as it got
  introduced in Maven 3.2.2. For local parent resolution the version in parent
  declarations had been ignored completely as of Maven 3.3.0 due to commit
  be3fb20032 removing all local parent version
  validation. Work on fixing this is tracked by MNG-5840 released with Maven
  3.3.9. This commit adds the final missing bits to make local and remote parent
  resolution behave the same way as much as possible. As an exception, remote
  parent resolution still is a bit more strict than local parent resolution due
  to a different API in use. When resolving a parent from a repository using
  version ranges, the ModelBuilder verifies the range in use to declare an upper
  bound. When resolving a parent locally using version ranges, those ranges are
  not required to declare an upper bound because the API in use does not support
  querying that. Authoring a POM relying on this difference should be considered
  a bug.
o Added test cases to maven-core testing parent version range resolution for
  local and remote parent models.
This commit is contained in:
Christian Schulte 2015-12-12 20:28:56 +01:00
parent f4e07acbc2
commit 0514c1b14e
20 changed files with 484 additions and 55 deletions

View File

@ -43,7 +43,6 @@ import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.DeploymentRepository;
import org.apache.maven.model.Extension;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.Profile;
import org.apache.maven.model.ReportPlugin;
@ -73,9 +72,6 @@ import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.WorkspaceRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.resolution.VersionRangeRequest;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.eclipse.aether.resolution.VersionRangeResult;
/**
*/
@ -298,44 +294,6 @@ public class DefaultProjectBuilder
boolean localProject;
if ( request.isResolveVersionRanges() )
{
VersionRangeRequest versionRangeRequest = new VersionRangeRequest( pomArtifact, config.repositories, null );
try
{
VersionRangeResult versionRangeResult =
repoSystem.resolveVersionRange( config.session, versionRangeRequest );
if ( versionRangeResult.getHighestVersion() == null )
{
throw new ProjectBuildingException(
artifact.getId(), "Error resolving project artifact: No versions matched the requested range",
(Throwable) null );
}
if ( versionRangeResult.getVersionConstraint() != null
&& versionRangeResult.getVersionConstraint().getRange() != null
&& versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
{
throw new ProjectBuildingException(
artifact.getId(),
"Error resolving project artifact: The requested version range does not specify an upper bound",
(Throwable) null );
}
pomArtifact = pomArtifact.setVersion( versionRangeResult.getHighestVersion().toString() );
}
catch ( VersionRangeResolutionException e )
{
throw new ProjectBuildingException(
artifact.getId(), "Error resolving project artifact: " + e.getMessage(), e );
}
}
try
{
ArtifactRequest pomRequest = new ArtifactRequest();
@ -656,11 +614,20 @@ public class DefaultProjectBuilder
project.setModel( model );
project.setOriginalModel( result.getRawModel() );
project.setFile( model.getPomFile() );
Parent p = model.getParent();
if ( p != null )
Model parentModel = result.getModelIds().size() > 1 && !result.getModelIds().get( 1 ).isEmpty()
? result.getRawModel( result.getModelIds().get( 1 ) )
: null;
if ( parentModel != null )
{
project.setParentArtifact( repositorySystem.createProjectArtifact( p.getGroupId(), p.getArtifactId(),
p.getVersion() ) );
final String parentGroupId = inheritedGroupId( result, 1 );
final String parentVersion = inheritedVersion( result, 1 );
project.setParentArtifact( repositorySystem.createProjectArtifact( parentGroupId,
parentModel.getArtifactId(),
parentVersion ) );
// org.apache.maven.its.mng4834:parent:0.1
String parentModelId = result.getModelIds().get( 1 );
File parentPomFile = result.getRawModel( parentModelId ).getPomFile();
@ -683,7 +650,16 @@ public class DefaultProjectBuilder
catch ( ProjectBuildingException e )
{
// MNG-4488 where let invalid parents slide on by
logger.warn( "Failed to build parent project for " + project.getId() );
if ( logger.isDebugEnabled() )
{
// Message below is checked for in the MNG-2199 core IT.
logger.warn( "Failed to build parent project for " + project.getId(), e );
}
else
{
// Message below is checked for in the MNG-2199 core IT.
logger.warn( "Failed to build parent project for " + project.getId() );
}
}
}
else
@ -696,7 +672,16 @@ public class DefaultProjectBuilder
catch ( ProjectBuildingException e )
{
// MNG-4488 where let invalid parents slide on by
logger.warn( "Failed to build parent project for " + project.getId() );
if ( logger.isDebugEnabled() )
{
// Message below is checked for in the MNG-2199 core IT.
logger.warn( "Failed to build parent project for " + project.getId(), e );
}
else
{
// Message below is checked for in the MNG-2199 core IT.
logger.warn( "Failed to build parent project for " + project.getId() );
}
}
}
}
@ -878,6 +863,40 @@ public class DefaultProjectBuilder
}
}
private static String inheritedGroupId( final ModelBuildingResult result, final int modelIndex )
{
String groupId = null;
final String modelId = result.getModelIds().get( modelIndex );
if ( !modelId.isEmpty() )
{
final Model model = result.getRawModel( modelId );
groupId = model.getGroupId() != null
? model.getGroupId()
: inheritedGroupId( result, modelIndex + 1 );
}
return groupId;
}
private static String inheritedVersion( final ModelBuildingResult result, final int modelIndex )
{
String version = null;
final String modelId = result.getModelIds().get( modelIndex );
if ( !modelId.isEmpty() )
{
final Model model = result.getRawModel( modelId );
version = model.getVersion() != null
? model.getVersion()
: inheritedVersion( result, modelIndex + 1 );
}
return version;
}
private String findProfilesXml( ModelBuildingResult result, Map<File, Boolean> profilesXmls )
{
for ( String modelId : result.getModelIds() )

View File

@ -63,6 +63,7 @@ public class DefaultProjectBuildingRequest
private boolean resolveDependencies;
@Deprecated
private boolean resolveVersionRanges;
private RepositoryMerging repositoryMerging = RepositoryMerging.POM_DOMINANT;
@ -218,14 +219,24 @@ public class DefaultProjectBuildingRequest
return resolveDependencies;
}
/** @since 3.2.2 */
/**
* @since 3.2.2
* @deprecated This got added when implementing MNG-2199 and is no longer used.
* Commit 6cf9320942c34bc68205425ab696b1712ace9ba4 updated the way 'MavenProject' objects are initialized.
*/
@Deprecated
public ProjectBuildingRequest setResolveVersionRanges( boolean value )
{
this.resolveVersionRanges = value;
return this;
}
/** @since 3.2.2 */
/**
* @since 3.2.2
* @deprecated This got added when implementing MNG-2199 and is no longer used.
* Commit 6cf9320942c34bc68205425ab696b1712ace9ba4 updated the way 'MavenProject' objects are initialized.
*/
@Deprecated
public boolean isResolveVersionRanges()
{
return this.resolveVersionRanges;

View File

@ -168,10 +168,20 @@ public interface ProjectBuildingRequest
*/
RepositoryMerging getRepositoryMerging();
/** @since 3.2.2 */
/**
* @since 3.2.2
* @deprecated This got added when implementing MNG-2199 and is no longer used.
* Commit 6cf9320942c34bc68205425ab696b1712ace9ba4 updated the way 'MavenProject' objects are initialized.
*/
@Deprecated
boolean isResolveVersionRanges();
/** @since 3.2.2 */
/**
* @since 3.2.2
* @deprecated This got added when implementing MNG-2199 and is no longer used.
* Commit 6cf9320942c34bc68205425ab696b1712ace9ba4 updated the way 'MavenProject' objects are initialized.
*/
@Deprecated
ProjectBuildingRequest setResolveVersionRanges( boolean value );
/**

View File

@ -228,6 +228,7 @@ public class ProjectModelResolver
&& versionRangeResult.getVersionConstraint().getRange() != null
&& versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
{
// Message below is checked for in the MNG-2199 core IT.
throw new UnresolvableModelException( "The requested version range '" + parent.getVersion()
+ "' does not specify an upper bound", parent.getGroupId(),
parent.getArtifactId(), parent.getVersion() );

View File

@ -169,6 +169,17 @@ public abstract class AbstractMavenProjectTestCase
return projectBuilder.build( pom, configuration ).getProject();
}
protected MavenProject getProjectFromRemoteRepository( final File pom )
throws Exception
{
final ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
configuration.setLocalRepository( this.getLocalRepository() );
configuration.setRemoteRepositories( Arrays.asList( this.repositorySystem.createDefaultRemoteRepository() ) );
initRepoSession( configuration );
return projectBuilder.build( pom, configuration ).getProject();
}
protected ProjectBuildingRequest newBuildingRequest()
throws Exception
{

View File

@ -189,4 +189,134 @@ public class DefaultMavenProjectBuilderTest
request.setResolveDependencies( true );
projectBuilder.build( pomFile, request );
}
/**
* Tests whether local version range parent references are build correctly.
*
* @throws Exception
*/
public void testBuildValidParentVersionRangeLocally() throws Exception
{
File f1 = getTestFile( "src/test/resources/projects/parent-version-range-local-valid/child/pom.xml" );
final MavenProject childProject = getProject( f1 );
assertNotNull( childProject.getParentArtifact() );
assertEquals( childProject.getParentArtifact().getVersion(), "1" );
assertNotNull( childProject.getParent() );
assertEquals( childProject.getParent().getVersion(), "1" );
assertNotNull( childProject.getModel().getParent() );
assertEquals( childProject.getModel().getParent().getVersion(), "[1,10]" );
}
/**
* Tests whether local version range parent references are build correctly.
*
* @throws Exception
*/
public void testBuildParentVersionRangeLocallyWithoutChildVersion() throws Exception
{
File f1 =
getTestFile( "src/test/resources/projects/parent-version-range-local-child-without-version/child/pom.xml" );
try
{
getProject( f1 );
fail( "Expected 'ProjectBuildingException' not thrown." );
}
catch ( final ProjectBuildingException e )
{
assertNotNull( e.getMessage() );
assertTrue( e.getMessage().contains( "Version must be a constant" ) );
}
}
/**
* Tests whether local version range parent references are build correctly.
*
* @throws Exception
*/
public void testBuildParentVersionRangeLocallyWithChildVersionExpression() throws Exception
{
File f1 =
getTestFile(
"src/test/resources/projects/parent-version-range-local-child-version-expression/child/pom.xml" );
try
{
getProject( f1 );
fail( "Expected 'ProjectBuildingException' not thrown." );
}
catch ( final ProjectBuildingException e )
{
assertNotNull( e.getMessage() );
assertTrue( e.getMessage().contains( "Version must be a constant" ) );
}
}
/**
* Tests whether external version range parent references are build correctly.
*
* @throws Exception
*/
public void testBuildParentVersionRangeExternally() throws Exception
{
File f1 = getTestFile( "src/test/resources/projects/parent-version-range-external-valid/pom.xml" );
final MavenProject childProject = this.getProjectFromRemoteRepository( f1 );
assertNotNull( childProject.getParentArtifact() );
assertEquals( childProject.getParentArtifact().getVersion(), "1" );
assertNotNull( childProject.getParent() );
assertEquals( childProject.getParent().getVersion(), "1" );
assertNotNull( childProject.getModel().getParent() );
assertEquals( childProject.getModel().getParent().getVersion(), "[1,1]" );
}
/**
* Tests whether external version range parent references are build correctly.
*
* @throws Exception
*/
public void testBuildParentVersionRangeExternallyWithoutChildVersion() throws Exception
{
File f1 =
getTestFile(
"src/test/resources/projects/parent-version-range-external-child-without-version/pom.xml" );
try
{
this.getProjectFromRemoteRepository( f1 );
fail( "Expected 'ProjectBuildingException' not thrown." );
}
catch ( final ProjectBuildingException e )
{
assertNotNull( e.getMessage() );
assertTrue( e.getMessage().contains( "Version must be a constant" ) );
}
}
/**
* Tests whether external version range parent references are build correctly.
*
* @throws Exception
*/
public void testBuildParentVersionRangeExternallyWithChildVersionExpression() throws Exception
{
File f1 =
getTestFile(
"src/test/resources/projects/parent-version-range-external-child-version-expression/pom.xml" );
try
{
this.getProjectFromRemoteRepository( f1 );
fail( "Expected 'ProjectBuildingException' not thrown." );
}
catch ( final ProjectBuildingException e )
{
assertNotNull( e.getMessage() );
assertTrue( e.getMessage().contains( "Version must be a constant" ) );
}
}
}

View File

@ -27,6 +27,7 @@ import java.util.Collection;
import org.codehaus.plexus.util.FileUtils;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.metadata.Metadata;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.spi.connector.ArtifactDownload;
import org.eclipse.aether.spi.connector.ArtifactUpload;
@ -35,6 +36,8 @@ import org.eclipse.aether.spi.connector.MetadataUpload;
import org.eclipse.aether.spi.connector.RepositoryConnector;
import org.eclipse.aether.transfer.ArtifactNotFoundException;
import org.eclipse.aether.transfer.ArtifactTransferException;
import org.eclipse.aether.transfer.MetadataNotFoundException;
import org.eclipse.aether.transfer.MetadataTransferException;
/**
* @author Benjamin Bentmann
@ -89,6 +92,28 @@ public class TestRepositoryConnector
}
}
}
if ( metadataDownloads != null )
{
for ( final MetadataDownload download : metadataDownloads )
{
File remoteFile = new File( basedir, path( download.getMetadata() ) );
try
{
FileUtils.copyFile( remoteFile, download.getFile() );
}
catch ( IOException e )
{
if ( !remoteFile.exists() )
{
download.setException( new MetadataNotFoundException( download.getMetadata(), repository ) );
}
else
{
download.setException( new MetadataTransferException( download.getMetadata(), repository, e ) );
}
}
}
}
}
private String path( Artifact artifact )
@ -113,6 +138,19 @@ public class TestRepositoryConnector
return path.toString();
}
private String path( Metadata metadata )
{
StringBuilder path = new StringBuilder( 128 );
path.append( metadata.getGroupId().replace( '.', '/' ) ).append( '/' );
path.append( metadata.getArtifactId() ).append( '/' );
path.append( "maven-metadata.xml" );
return path.toString();
}
public void put( Collection<? extends ArtifactUpload> artifactUploads,
Collection<? extends MetadataUpload> metadataUploads )
{

View File

@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2005-2006 The Apache Software Foundation.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Shared parent. Doesn't define a lot of things about Apache like general mailing lists, but does
define the settings common to all projects at Apache -->
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>1</version>
<packaging>pom</packaging>
<name>The Apache Software Foundation</name>
<description>
The Apache Software Foundation provides support for the Apache community of open-source software projects.
The Apache projects are characterized by a collaborative, consensus based development process, an open and
pragmatic software license, and a desire to create high quality software that leads the way in its field.
We consider ourselves not simply a group of projects sharing a server, but rather a community of developers
and users.
</description>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<organization>
<name>Apache Software Foundation</name>
<url>http://www.apache.org/</url>
</organization>
<url>http://www.apache.org/</url>
<repositories>
<repository>
<id>apache.snapshots</id>
<name>Apache Snapshot Repository</name>
<url>http://svn.apache.org/maven-snapshot-repository</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<distributionManagement>
<!-- Site omitted - each project must provide their own -->
<repository>
<id>apache.releases</id>
<name>Apache Release Distribution Repository</name>
<url>scp://minotaur.apache.org/www/www.apache.org/dist/maven-repository</url>
</repository>
<snapshotRepository>
<id>apache.snapshots</id>
<name>Apache Development Snapshot Repository</name>
<url>scp://minotaur.apache.org/www/cvs.apache.org/maven-snapshot-repository</url>
</snapshotRepository>
</distributionManagement>
<mailingLists>
<mailingList>
<name>Apache Announce List</name>
<subscribe>announce-subscribe@apache.org</subscribe>
<unsubscribe>announce-unsubscribe@apache.org</unsubscribe>
<post>announce@apache.org</post>
<archive>http://mail-archives.apache.org/mod_mbox/www-announce/</archive>
</mailingList>
</mailingLists>
</project>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<versioning>
<latest>1</latest>
<release>1</release>
<versions>
<version>1</version>
</versions>
<lastUpdated>20150428055824</lastUpdated>
</versioning>
</metadata>

View File

@ -0,0 +1,12 @@
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>[1,1]</version>
</parent>
<artifactId>child</artifactId>
<!-- Must not use expressions from parent due to version range. -->
<version>${some.property}</version>
<packaging>pom</packaging>
</project>

View File

@ -0,0 +1,11 @@
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>[1,1]</version>
</parent>
<artifactId>child</artifactId>
<!-- version>2</version Must not inherit version from parent due to version range. -->
<packaging>pom</packaging>
</project>

View File

@ -0,0 +1,11 @@
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>[1,1]</version>
</parent>
<artifactId>child</artifactId>
<version>2</version>
<packaging>pom</packaging>
</project>

View File

@ -0,0 +1,12 @@
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>parent-version-range-local</groupId>
<artifactId>parent</artifactId>
<version>[1,10]</version>
</parent>
<artifactId>child</artifactId>
<!-- Must not use expressions from parent due to version range. -->
<version>${some.property}</version>
<packaging>pom</packaging>
</project>

View File

@ -0,0 +1,7 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>parent-version-range-local</groupId>
<artifactId>parent</artifactId>
<version>1</version>
<packaging>pom</packaging>
</project>

View File

@ -0,0 +1,11 @@
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>parent-version-range-local</groupId>
<artifactId>parent</artifactId>
<version>[1,10]</version>
</parent>
<artifactId>child</artifactId>
<!-- version>1</version Must not inherit version from parent due to version range. -->
<packaging>pom</packaging>
</project>

View File

@ -0,0 +1,7 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>parent-version-range-local</groupId>
<artifactId>parent</artifactId>
<version>1</version>
<packaging>pom</packaging>
</project>

View File

@ -0,0 +1,11 @@
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>parent-version-range-local</groupId>
<artifactId>parent</artifactId>
<version>[1,10]</version>
</parent>
<artifactId>child</artifactId>
<version>1</version>
<packaging>pom</packaging>
</project>

View File

@ -0,0 +1,7 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>parent-version-range-local</groupId>
<artifactId>parent</artifactId>
<version>1</version>
<packaging>pom</packaging>
</project>

View File

@ -333,7 +333,7 @@ public class DefaultModelBuilder
currentData = superData;
}
else if ( currentData == resultData )
{ // First iteration - add initial parent id after version resolution.
{ // First iteration - add initial id after version resolution.
currentData.setGroupId( currentData.getRawModel().getGroupId() == null ? parentData.getGroupId()
: currentData.getRawModel()
.getGroupId() );
@ -938,6 +938,28 @@ public class DefaultModelBuilder
// version skew drop back to resolution from the repository
return null;
}
// Validate versions aren't inherited when using parent ranges the same way as when read externally.
if ( childModel.getVersion() == null )
{
// Message below is checked for in the MNG-2199 core IT.
problems.add( new ModelProblemCollectorRequest( Severity.FATAL, Version.V31 )
.setMessage( "Version must be a constant" ).setLocation( childModel.getLocation( "" ) ) );
}
else
{
if ( childModel.getVersion().contains( "${" ) )
{
// Message below is checked for in the MNG-2199 core IT.
problems.add( new ModelProblemCollectorRequest( Severity.FATAL, Version.V31 )
.setMessage( "Version must be a constant" )
.setLocation( childModel.getLocation( "version" ) ) );
}
}
// MNG-2199: What else to check here ?
}
catch ( InvalidVersionSpecificationException e )
{
@ -1001,6 +1023,7 @@ public class DefaultModelBuilder
}
catch ( UnresolvableModelException e )
{
// Message below is checked for in the MNG-2199 core IT.
StringBuilder buffer = new StringBuilder( 256 );
buffer.append( "Non-resolvable parent POM" );
if ( !containsCoordinates( e.getMessage(), groupId, artifactId, version ) )
@ -1048,15 +1071,16 @@ public class DefaultModelBuilder
{
if ( childModel.getVersion() == null )
{
// Message below is checked for in the MNG-2199 core IT.
problems.add( new ModelProblemCollectorRequest( Severity.FATAL, Version.V31 )
.setMessage( "Version must be a constant" ).setLocation( childModel.getLocation( "" ) ) );
}
else
{
if ( childModel.getVersion()
.contains( "${" ) )
if ( childModel.getVersion().contains( "${" ) )
{
// Message below is checked for in the MNG-2199 core IT.
problems.add( new ModelProblemCollectorRequest( Severity.FATAL, Version.V31 )
.setMessage( "Version must be a constant" )
.setLocation( childModel.getLocation( "version" ) ) );

View File

@ -208,6 +208,7 @@ class DefaultModelResolver
&& versionRangeResult.getVersionConstraint().getRange() != null
&& versionRangeResult.getVersionConstraint().getRange().getUpperBound() == null )
{
// Message below is checked for in the MNG-2199 core IT.
throw new UnresolvableModelException( "The requested version range '" + parent.getVersion()
+ "' does not specify an upper bound", parent.getGroupId(),
parent.getArtifactId(), parent.getVersion() );