Skip to content

Commit 4f2240a

Browse files
committed
Resolves mojohaus#289: Handle processParent in mojos which support it.
Also: - added failIfNotReplaced to ForceReleasesMojo as a first steps towards converging with UseReleasesMojo - enhanced documentation on UseReleasesMojo#allowRangeMatching - streamlined UseReleasesMojo in order to converge it with ForceReleasesMojo and then fold further with other similar mojos - added unit tests and integration tests - some refactoring
1 parent 6a950e2 commit 4f2240a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+742
-674
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ target
44
.project
55
*.iml
66
.idea
7+
.checkstyle
78
/.factorypath
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>localhost</groupId>
7+
<artifactId>dummy-parent2</artifactId>
8+
<version>2.0</version>
9+
</parent>
10+
11+
<artifactId>dummy-with-parent</artifactId>
12+
<version>1.0</version>
13+
<packaging>pom</packaging>
14+
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:compare-dependencies
2+
invoker.mavenOpts = -DremotePom=localhost:dummy-with-parent:1.0 -DreportMode=false -DprocessParent=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>localhost</groupId>
7+
<artifactId>dummy-parent2</artifactId>
8+
<version>1.0</version>
9+
</parent>
10+
11+
<artifactId>dummy-with-parent</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<packaging>pom</packaging>
14+
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
2+
assert project.parent.version == '2.0'

versions-maven-plugin/src/it/it-use-latest-versions-009/verify.bsh

-33
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
2+
assert project.parent.version == '3.0'

versions-maven-plugin/src/it/it-use-latest-versions-010/verify.bsh

-62
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
2+
assert project.parent.version == '3.0'

versions-maven-plugin/src/it/it-use-latest-versions-011/verify.bsh

-33
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def project = new XmlSlurper().parse( new File( basedir, 'pom.xml' ) )
2+
assert project.parent.version == '1.0.0'

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsDependencyUpdaterMojo.java

+83-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
*/
2121

2222
import javax.inject.Inject;
23+
import javax.xml.stream.XMLStreamException;
2324

2425
import java.util.ArrayList;
2526
import java.util.Arrays;
2627
import java.util.Collections;
2728
import java.util.List;
29+
import java.util.regex.Pattern;
2830

2931
import org.apache.commons.lang3.StringUtils;
3032
import org.apache.maven.artifact.Artifact;
@@ -41,7 +43,11 @@
4143
import org.apache.maven.repository.RepositorySystem;
4244
import org.apache.maven.shared.artifact.filter.PatternExcludesArtifactFilter;
4345
import org.apache.maven.shared.artifact.filter.PatternIncludesArtifactFilter;
46+
import org.codehaus.mojo.versions.api.PomHelper;
47+
import org.codehaus.mojo.versions.recording.ChangeRecorder;
48+
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
4449
import org.codehaus.mojo.versions.utils.DependencyBuilder;
50+
import org.codehaus.mojo.versions.utils.DependencyComparator;
4551

4652
/**
4753
* Base class for a mojo that updates dependency versions.
@@ -53,11 +59,16 @@
5359
public abstract class AbstractVersionsDependencyUpdaterMojo
5460
extends AbstractVersionsUpdaterMojo
5561
{
56-
5762
private static final String END_RANGE_CHARS = "])";
5863

5964
private static final String START_RANGE_CHARS = "[(";
6065

66+
/**
67+
* Pattern to match snapshot versions
68+
*/
69+
protected static final Pattern SNAPSHOT_REGEX = Pattern.compile( "^(.+)-((SNAPSHOT)|(\\d{8}\\.\\d{6}-\\d+))$" );
70+
71+
6172
/**
6273
* A comma separated list of artifact patterns to include. Follows the pattern
6374
* "groupId:artifactId:type:classifier:version". Designed to allow specifying the set of includes from the command
@@ -104,15 +115,15 @@ public abstract class AbstractVersionsDependencyUpdaterMojo
104115
* @since 1.0-alpha-3
105116
*/
106117
@Parameter( property = "processDependencies", defaultValue = "true" )
107-
private boolean processDependencies;
118+
private boolean processDependencies = true;
108119

109120
/**
110121
* Whether to process the dependencyManagement section of the project.
111122
*
112123
* @since 1.0-alpha-3
113124
*/
114125
@Parameter( property = "processDependencyManagement", defaultValue = "true" )
115-
private boolean processDependencyManagement;
126+
private boolean processDependencyManagement = true;
116127

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

147158
@Inject
148159
protected AbstractVersionsDependencyUpdaterMojo( RepositorySystem repositorySystem,
@@ -266,6 +277,20 @@ protected Artifact toArtifact( Parent model )
266277
.build() );
267278
}
268279

280+
/**
281+
* Returns the {@link Dependency} instance for the parent project
282+
* @return {@link Dependency} object for the parent
283+
*/
284+
protected Dependency getParentDependency()
285+
{
286+
return DependencyBuilder.newBuilder()
287+
.withGroupId( getProject().getParent().getGroupId() )
288+
.withArtifactId( getProject().getParent().getArtifactId() )
289+
.withVersion( getProject().getParent().getVersion() )
290+
.withType( "pom" )
291+
.build();
292+
}
293+
269294
protected String toString( MavenProject project )
270295
{
271296
StringBuilder buf = new StringBuilder();
@@ -507,4 +532,58 @@ private int findFirstChar( final String includeString, final String chars )
507532
}
508533
return nextRangeStartDelimiterIndex;
509534
}
535+
536+
/**
537+
* Attempts to update the dependency {@code dep} to the given {@code newVersion}. The dependency can either
538+
* be the parent project or any given dependency.
539+
*
540+
* @param pom {@link ModifiedPomXMLEventReader} instance to update the POM XML document
541+
* @param dep dependency to be updated (can also be a dependency made from the parent)
542+
* @param newVersion new version to update the dependency to
543+
* @param changeRecorderTitle title for the {@link ChangeRecorder} log
544+
* @return {@code true} if an update has been made, {@code false} otherwise
545+
* @throws XMLStreamException thrown if updating the XML doesn't succeed
546+
*/
547+
protected boolean updateDependencyVersion( ModifiedPomXMLEventReader pom, Dependency dep,
548+
String newVersion, String changeRecorderTitle )
549+
throws XMLStreamException
550+
{
551+
boolean updated = false;
552+
if ( isProcessingParent()
553+
&& getProject().getParent() != null
554+
&& DependencyComparator.INSTANCE.compare( dep, DependencyBuilder.newBuilder()
555+
.withGroupId( getProject().getParentArtifact().getGroupId() )
556+
.withArtifactId( getProject().getParentArtifact().getArtifactId() )
557+
.withVersion( getProject().getParentArtifact().getVersion() )
558+
.build() ) == 0
559+
&& PomHelper.setProjectParentVersion( pom, newVersion ) )
560+
{
561+
if ( getLog().isDebugEnabled() )
562+
{
563+
getLog().debug( "Made parent update from " + dep.getVersion() + " to " + newVersion );
564+
}
565+
getChangeRecorder().recordUpdate( changeRecorderTitle,
566+
dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
567+
newVersion );
568+
updated = true;
569+
}
570+
571+
if ( PomHelper.setDependencyVersion( pom,
572+
dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
573+
newVersion, getProject().getModel() ) )
574+
{
575+
if ( getLog().isInfoEnabled() )
576+
{
577+
getLog().info( "Updated " + toString( dep ) + " to version " + newVersion );
578+
}
579+
getChangeRecorder().recordUpdate( changeRecorderTitle,
580+
dep.getGroupId(), dep.getArtifactId(), dep.getVersion(),
581+
newVersion );
582+
updated = true;
583+
}
584+
585+
return updated;
586+
}
587+
588+
// TODO: add an updatePropertyVersion as well??? (like in CompareDependenciesMojo)
510589
}

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/AbstractVersionsReport.java

+5-15
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,13 @@ protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange ver
301301
}
302302
}
303303

304-
/**
305-
* @see org.apache.maven.reporting.AbstractMavenReport#getProject()
306-
*/
304+
@Override
307305
protected MavenProject getProject()
308306
{
309307
return project;
310308
}
311309

312-
/**
313-
* @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
314-
*/
310+
@Override
315311
protected String getOutputDirectory()
316312
{
317313
if ( !outputDirectory.isAbsolute() )
@@ -322,25 +318,19 @@ protected String getOutputDirectory()
322318
return outputDirectory.getAbsolutePath();
323319
}
324320

325-
/**
326-
* @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
327-
*/
321+
@Override
328322
protected Renderer getSiteRenderer()
329323
{
330324
return siteRenderer;
331325
}
332326

333-
/**
334-
* @see org.apache.maven.reporting.MavenReport#getDescription(java.util.Locale)
335-
*/
327+
@Override
336328
public String getDescription( Locale locale )
337329
{
338330
return getText( locale, "report.description" );
339331
}
340332

341-
/**
342-
* @see org.apache.maven.reporting.MavenReport#getName(java.util.Locale)
343-
*/
333+
@Override
344334
public String getName( Locale locale )
345335
{
346336
return getText( locale, "report.title" );

0 commit comments

Comments
 (0)