Skip to content

Commit

Permalink
Resolves #289: Handle processParent in mojos which support it.
Browse files Browse the repository at this point in the history
Also:
- added failIfNotReplaced to ForceReleasesMojo as a first steps towards converging with UseReleasesMojo
- added unit tests and integration tests
- some refactoring
  • Loading branch information
andrzejj0 committed Nov 9, 2022
1 parent 6a950e2 commit 1d5b043
Show file tree
Hide file tree
Showing 32 changed files with 469 additions and 346 deletions.
15 changes: 15 additions & 0 deletions versions-maven-plugin/src/it-repo/dummy-with-parent.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<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-parent2</artifactId>
<version>2.0</version>
</parent>

<artifactId>dummy-with-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:compare-dependencies
invoker.mavenOpts = -DremotePom=localhost:dummy-with-parent:1.0 -DreportMode=false -DprocessParent=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<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-parent2</artifactId>
<version>1.0</version>
</parent>

<artifactId>dummy-with-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
assert project.parent.version == '2.0'
33 changes: 0 additions & 33 deletions versions-maven-plugin/src/it/it-use-latest-versions-009/verify.bsh

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
assert project.parent.version == '3.0'
62 changes: 0 additions & 62 deletions versions-maven-plugin/src/it/it-use-latest-versions-010/verify.bsh

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
assert project.parent.version == '3.0'
33 changes: 0 additions & 33 deletions versions-maven-plugin/src/it/it-use-latest-versions-011/verify.bsh

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
assert project.parent.version == '1.0.0'
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
*/

import javax.inject.Inject;
import javax.xml.stream.XMLStreamException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.Artifact;
Expand All @@ -41,7 +43,11 @@
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.recording.ChangeRecorder;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.DependencyComparator;

/**
* Base class for a mojo that updates dependency versions.
Expand All @@ -53,11 +59,16 @@
public abstract class AbstractVersionsDependencyUpdaterMojo
extends AbstractVersionsUpdaterMojo
{

private static final String END_RANGE_CHARS = "])";

private static final String START_RANGE_CHARS = "[(";

/**
* Pattern to match snapshot versions
*/
protected static final Pattern SNAPSHOT_REGEX = Pattern.compile( "^(.+)-((SNAPSHOT)|(\\d{8}\\.\\d{6}-\\d+))$" );


/**
* A comma separated list of artifact patterns to include. Follows the pattern
* "groupId:artifactId:type:classifier:version". Designed to allow specifying the set of includes from the command
Expand Down Expand Up @@ -104,15 +115,15 @@ public abstract class AbstractVersionsDependencyUpdaterMojo
* @since 1.0-alpha-3
*/
@Parameter( property = "processDependencies", defaultValue = "true" )
private boolean processDependencies;
private boolean processDependencies = true;

/**
* Whether to process the dependencyManagement section of the project.
*
* @since 1.0-alpha-3
*/
@Parameter( property = "processDependencyManagement", defaultValue = "true" )
private boolean processDependencyManagement;
private boolean processDependencyManagement = true;

/**
* Whether to process the parent section of the project. If not set will default to false.
Expand Down Expand Up @@ -142,7 +153,7 @@ public abstract class AbstractVersionsDependencyUpdaterMojo
* @since 1.0-alpha-3
*/
@Parameter( property = "excludeReactor", defaultValue = "true" )
private boolean excludeReactor;
private boolean excludeReactor = true;

@Inject
protected AbstractVersionsDependencyUpdaterMojo( RepositorySystem repositorySystem,
Expand Down Expand Up @@ -266,6 +277,20 @@ protected Artifact toArtifact( Parent model )
.build() );
}

/**
* Returns the {@link Dependency} instance for the parent project
* @return {@link Dependency} object for the parent
*/
protected Dependency getParentDependency()
{
return DependencyBuilder.newBuilder()
.withGroupId( getProject().getParent().getGroupId() )
.withArtifactId( getProject().getParent().getArtifactId() )
.withVersion( getProject().getParent().getVersion() )
.withType( "pom" )
.build();
}

protected String toString( MavenProject project )
{
StringBuilder buf = new StringBuilder();
Expand Down Expand Up @@ -507,4 +532,58 @@ private int findFirstChar( final String includeString, final String chars )
}
return nextRangeStartDelimiterIndex;
}

/**
* Attempts to update the dependency {@code dep} to the given {@code newVersion}. The dependency can either
* be the parent project or any given dependency.
*
* @param pom {@link ModifiedPomXMLEventReader} instance to update the POM XML document
* @param dep dependency to be updated (can also be a dependency made from the parent)
* @param newVersion new version to update the dependency to
* @param changeRecorderTitle title for the {@link ChangeRecorder} log
* @return {@code true} if an update has been made, {@code false} otherwise
* @throws XMLStreamException thrown if updating the XML doesn't succeed
*/
protected boolean updateDependencyVersion( ModifiedPomXMLEventReader pom, Dependency dep,
String newVersion, String changeRecorderTitle )
throws XMLStreamException
{
boolean updated = false;
if ( isProcessingParent()
&& getProject().getParent() != null
&& DependencyComparator.INSTANCE.compare( dep, DependencyBuilder.newBuilder()
.withGroupId( getProject().getParentArtifact().getGroupId() )
.withArtifactId( getProject().getParentArtifact().getArtifactId() )
.withVersion( getProject().getParentArtifact().getVersion() )
.build() ) == 0
&& PomHelper.setProjectParentVersion( pom, newVersion ) )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Made parent update from " + dep.getVersion() + " to " + newVersion );
}
getChangeRecorder().recordUpdate( changeRecorderTitle,
dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
newVersion );
updated = true;
}

if ( PomHelper.setDependencyVersion( pom,
dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
newVersion, getProject().getModel() ) )
{
if ( getLog().isInfoEnabled() )
{
getLog().info( "Updated " + toString( dep ) + " to version " + newVersion );
}
getChangeRecorder().recordUpdate( changeRecorderTitle,
dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
newVersion );
updated = true;
}

return updated;
}

// TODO: add an updatePropertyVersion as well??? (like in CompareDependenciesMojo)
}
Loading

0 comments on commit 1d5b043

Please sign in to comment.