Skip to content

Commit

Permalink
#610: Adding the possibility to skip version resolution if parentVers…
Browse files Browse the repository at this point in the history
…ion is set
  • Loading branch information
andrzejj0 committed Sep 16, 2022
1 parent 4637b2d commit dce0651
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/it/it-update-parent-005-issue-610/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:update-parent
invoker.mavenOpts = -DparentVersion=999 -DskipResolution=true
16 changes: 16 additions & 0 deletions src/it/it-update-parent-005-issue-610/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>localhost</groupId>
<artifactId>dummy-parent4</artifactId>
<version>70</version>
</parent>

<groupId>localhsot</groupId>
<artifactId>issue-670</artifactId>
<version>0.31-SNAPSHOT</version>
<packaging>pom</packaging>

</project>
3 changes: 3 additions & 0 deletions src/it/it-update-parent-005-issue-610/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pom = new File( basedir, "pom.xml" ).text

assert pom =~ /<version>999<\/version>/
96 changes: 61 additions & 35 deletions src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -34,6 +35,8 @@
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

import static org.apache.maven.shared.utils.StringUtils.isBlank;

/**
* Sets the parent version to the latest parent version.
*
Expand All @@ -50,8 +53,12 @@ public class UpdateParentMojo extends AbstractVersionsUpdaterMojo
// ------------------------------ FIELDS ------------------------------

/**
* Version specification to control artifact resolution.
* <p>If {@code skipResolution} is not set, specifies the <em>bottom</em> version considered
* for target version resolution. If it is a version range, the resolved version will be
* restricted by that range.</p>
*
* <p>If {@code skipResolution} is {@code true}, will specify the target version to which
* the parent artifact will be updated.</p>
* @since 1.0-alpha-1
*/
@Parameter( property = "parentVersion" )
Expand All @@ -65,6 +72,15 @@ public class UpdateParentMojo extends AbstractVersionsUpdaterMojo
@Parameter( property = "forceUpdate", defaultValue = "false" )
protected boolean forceUpdate = false;

/**
* Skips version resolution, only valid if {@code parentVersion} is set.
* Will effectively set the new parent version to the one from {@code parentVersion}
*
* @since 2.13.0
*/
@Parameter( property = "skipResolution", defaultValue = "false" )
protected boolean skipResolution = false;

/**
* <p>Whether to downgrade a snapshot dependency if <code>allowSnapshots</code> is <code>false</code>
* and there exists a version within the range fulfilling the criteria.</p>
Expand All @@ -87,7 +103,7 @@ public class UpdateParentMojo extends AbstractVersionsUpdaterMojo
* @since 1.0-alpha-1
*/
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException
throws MojoExecutionException, MojoFailureException, XMLStreamException
{
if ( getProject().getParent() == null )
{
Expand All @@ -101,60 +117,70 @@ protected void update( ModifiedPomXMLEventReader pom )
return;
}

String currentVersion = getProject().getParent().getVersion();
String version = currentVersion;

if ( parentVersion != null )
if ( skipResolution && isBlank( parentVersion ) )
{
version = parentVersion;
throw new MojoExecutionException( "skipResolution is only valid if parentVersion is set" );
}

Artifact artifact = getHelper().createDependencyArtifact( DependencyBuilder.newBuilder()
.withGroupId( getProject().getParent().getGroupId() )
.withArtifactId( getProject().getParent().getArtifactId() )
.withVersion( version )
.withType( "pom" )
.build() );

VersionRange versionRange;
String initialVersion = parentVersion == null ? getProject().getParent().getVersion() : parentVersion;
try
{
versionRange = VersionRange.createFromVersionSpec( version );
if ( versionRange.getRecommendedVersion() != null )
ArtifactVersion artifactVersion = skipResolution ? new DefaultArtifactVersion( parentVersion )
: resolveTargetVersion( initialVersion );
if ( artifactVersion != null )
{
versionRange = versionRange.restrict(
VersionRange.createFromVersionSpec( "[" + versionRange.getRecommendedVersion() + ",)" ) );
getLog().info( "Updating parent from " + getProject().getParent().getVersion()
+ " to " + artifactVersion.toString() );

if ( PomHelper.setProjectParentVersion( pom, artifactVersion.toString() ) )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Made an update from " + getProject().getParent().getVersion()
+ " to " + artifactVersion );
}
getChangeRecorder().recordUpdate( "updateParent", getProject().getParent().getGroupId(),
getProject().getParent().getArtifactId(), getProject().getParent().getVersion(),
artifactVersion.toString() );
}
}
}
catch ( InvalidVersionSpecificationException e )
{
throw new MojoExecutionException( "Invalid version range specification: " + version, e );
}

ArtifactVersion artifactVersion;
try
{
artifactVersion = findLatestVersion( artifact, versionRange, null, false, allowDowngrade );
throw new MojoExecutionException( "Invalid version range specification: " + initialVersion, e );
}
catch ( ArtifactMetadataRetrievalException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}

private ArtifactVersion resolveTargetVersion( String initialVersion )
throws MojoExecutionException, ArtifactMetadataRetrievalException, InvalidVersionSpecificationException
{
Artifact artifact = getHelper().createDependencyArtifact( DependencyBuilder
.newBuilder()
.withGroupId( getProject().getParent().getGroupId() )
.withArtifactId( getProject().getParent().getArtifactId() )
.withVersion( initialVersion )
.withType( "pom" )
.build() );

if ( !shouldApplyUpdate( artifact, currentVersion, artifactVersion, forceUpdate ) )
VersionRange targetVersionRange = VersionRange.createFromVersionSpec( initialVersion );
if ( targetVersionRange.getRecommendedVersion() != null )
{
return;
targetVersionRange = targetVersionRange.restrict(
VersionRange.createFromVersionSpec( "[" + targetVersionRange.getRecommendedVersion() + ",)" ) );
}

getLog().info( "Updating parent from " + currentVersion + " to " + artifactVersion.toString() );

if ( PomHelper.setProjectParentVersion( pom, artifactVersion.toString() ) )
ArtifactVersion artifactVersion = findLatestVersion( artifact, targetVersionRange, null,
false, allowDowngrade );
if ( !shouldApplyUpdate( artifact, getProject().getParent().getVersion(), artifactVersion, forceUpdate ) )
{
getLog().debug( "Made an update from " + currentVersion + " to " + artifactVersion );

this.getChangeRecorder().recordUpdate( "updateParent", artifact.getGroupId(), artifact.getArtifactId(),
currentVersion, artifactVersion.toString() );
getLog().debug( "Update not applied. Exiting." );
return null;
}
return artifactVersion;
}

}
50 changes: 46 additions & 4 deletions src/test/java/org/codehaus/mojo/versions/UpdateParentMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ public class UpdateParentMojoTest

private static RepositorySystem repositorySystem;

@SuppressWarnings( "deprecation" )
private static ArtifactMetadataSource artifactMetadataSource;

@BeforeClass
@SuppressWarnings( "deprecation" )
public static void setUpStatic() throws ArtifactMetadataRetrievalException
public static void setUpStatic()
{
repositorySystem = mockRepositorySystem();
artifactMetadataSource = mockArtifactMetadataSource( new HashMap<String, String[]>()
{{
put( "parent-artifact", new String[] { "1.0.1-SNAPSHOT", "1.0.0", "0.9.0" } );
put( "parent-artifact", new String[] { "0.9.0", "1.0.0", "1.0.1-SNAPSHOT" } );
put( "issue-670-artifact", new String[] { "0.0.1-1", "0.0.1-1-impl-SNAPSHOT" } );
put( "unknown-artifact", new String[0] );
}} );
Expand Down Expand Up @@ -283,4 +281,48 @@ public void testIgnoredVersions()
}
assertThat( changeRecorder.getChanges(), is( empty() ) );
}

@Test
public void testSkipResolutionDowngradeUnknownVersion()
{
testSkipResolution( "0.8.0" );
}

@Test
public void testSkipResolutionDowngrade()
{
testSkipResolution( "0.9.0" );
}

@Test
public void testSkipResolutionUpgradeUnknownVersion()
{
testSkipResolution( "2.0.0" );
}

private void testSkipResolution( String version )
{
mojo.parentVersion = version;
mojo.skipResolution = true;
mojo.getProject().setParent( new MavenProject()
{{
setGroupId( "default-group" );
setArtifactId( "parent-artifact" );
setVersion( "1.0.0" );
}} );

try ( MockedStatic<PomHelper> pomHelper = mockStatic( PomHelper.class ) )
{
pomHelper.when( () -> PomHelper.setProjectParentVersion( any(), any() ) )
.thenReturn( true );
mojo.update( null );
}
catch ( MojoExecutionException | XMLStreamException | MojoFailureException e )
{
throw new RuntimeException( e );
}

assertThat( changeRecorder.getChanges(), hasItem( new VersionChange( "default-group",
"parent-artifact", "1.0.0", version ) ) );
}
}

0 comments on commit dce0651

Please sign in to comment.