Skip to content

Commit 6b297e5

Browse files
author
Adam Voss
committed
Respect processParent setting by introducing new base class
1 parent 086d126 commit 6b297e5

13 files changed

+244
-307
lines changed

ReleaseNotes.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,19 @@
2222
is not compatible with project minimum version, not really a proposed
2323
upgrade
2424

25-
* [Fixed Issue 237][issue-237]
25+
* [Fixed Issue 237][issue-237] and [Fixed Issue 288][issue-288]
2626

27-
Thanks to Julian Di Leonardo <[email protected]>
27+
Thanks to Julian Di Leonardo <[email protected]> and [Adam Voss](https://github.com/adamvoss)
2828

29-
Adding parent processing to UseLatestVersion/UseLatestSnapshot/UseLatestRelease
29+
Added parent processing to:
30+
- UseLatestVersion
31+
- UseLatestSnapshot
32+
- UseLatestRelease
33+
- ForceReleases
34+
- UseDepVersion
35+
- UseNextReleases
36+
- UseNextSnapshots
37+
- UseNextVersions
3038

3139
* [Fixed Issue 190][issue-190]
3240

@@ -153,6 +161,7 @@
153161
[issue-198]: https://github.com/mojohaus/versions-maven-plugin/issues/198
154162
[issue-237]: https://github.com/mojohaus/versions-maven-plugin/issues/237
155163
[issue-256]: https://github.com/mojohaus/versions-maven-plugin/issues/256
164+
[issue-288]: https://github.com/mojohaus/versions-maven-plugin/issues/288
156165

157166
[pull-189]: https://github.com/mojohaus/versions-maven-plugin/pull/189
158167
[pull-252]: https://github.com/mojohaus/versions-maven-plugin/pull/252
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:use-next-releases
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
<parent>
5+
<groupId>localhost</groupId>
6+
<artifactId>dummy-parent</artifactId>
7+
<version>1.0</version>
8+
</parent>
9+
10+
<groupId>localhost</groupId>
11+
<artifactId>it-use-next-releases-005</artifactId>
12+
<version>1.0</version>
13+
<packaging>pom</packaging>
14+
<name>Update a parent dependency</name>
15+
16+
<dependencies>
17+
18+
<dependency>
19+
<groupId>localhost</groupId>
20+
<artifactId>dummy-impl</artifactId>
21+
<version>1.0</version>
22+
</dependency>
23+
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>@project.groupId@</groupId>
30+
<artifactId>@project.artifactId@</artifactId>
31+
<version>@project.version@</version>
32+
<configuration>
33+
<processParent>true</processParent>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
39+
</project>
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.io.*;
2+
import java.util.regex.*;
3+
4+
try
5+
{
6+
File file = new File( basedir, "pom.xml" );
7+
8+
BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( file ), "UTF-8" ) );
9+
StringBuilder buf = new StringBuilder();
10+
String line = in.readLine();
11+
while ( line != null )
12+
{
13+
buf.append( line );
14+
buf.append( " " );
15+
line = in.readLine();
16+
}
17+
18+
Pattern p = Pattern.compile( "\\Q<parent>\\E.*\\Q<version>\\E\\s*2\\.0\\s*\\Q</version>\\E.*\\Q</parent>\\E" );
19+
Matcher m = p.matcher( buf.toString() );
20+
if ( !m.find() )
21+
{
22+
System.out.println( "Did not update parent to version 2.0" );
23+
return false;
24+
}
25+
System.out.println( m.group( 0 ) );
26+
}
27+
catch( Throwable t )
28+
{
29+
t.printStackTrace();
30+
return false;
31+
}
32+
33+
return true;

src/main/java/org/codehaus/mojo/versions/ForceReleasesMojo.java

+10-34
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.maven.artifact.Artifact;
2323
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
2424
import org.apache.maven.artifact.versioning.ArtifactVersion;
25+
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
2526
import org.apache.maven.model.Dependency;
2627
import org.apache.maven.plugin.MojoExecutionException;
2728
import org.apache.maven.plugin.MojoFailureException;
@@ -44,7 +45,7 @@
4445
*/
4546
@Mojo( name = "force-releases", requiresProject = true, requiresDirectInvocation = true, threadSafe = true )
4647
public class ForceReleasesMojo
47-
extends AbstractVersionsDependencyUpdaterMojo
48+
extends ParentUpdatingDependencyUpdateMojo
4849
{
4950

5051
// ------------------------------ FIELDS ------------------------------
@@ -56,31 +57,12 @@ public class ForceReleasesMojo
5657

5758
// ------------------------------ METHODS --------------------------
5859

59-
/**
60-
* @param pom the pom to update.
61-
* @throws org.apache.maven.plugin.MojoExecutionException when things go wrong
62-
* @throws org.apache.maven.plugin.MojoFailureException when things go wrong in a very bad way
63-
* @throws javax.xml.stream.XMLStreamException when things go wrong with XML streaming
64-
* @see AbstractVersionsUpdaterMojo#update(org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader)
65-
*/
66-
protected void update( ModifiedPomXMLEventReader pom )
67-
throws MojoExecutionException, MojoFailureException, XMLStreamException
60+
61+
@Override
62+
protected void setVersions(ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies)
63+
throws ArtifactMetadataRetrievalException, XMLStreamException, MojoExecutionException
6864
{
69-
try
70-
{
71-
if ( getProject().getDependencyManagement() != null && isProcessingDependencyManagement() )
72-
{
73-
useReleases( pom, getProject().getDependencyManagement().getDependencies() );
74-
}
75-
if ( getProject().getDependencies() != null && isProcessingDependencies() )
76-
{
77-
useReleases( pom, getProject().getDependencies() );
78-
}
79-
}
80-
catch ( ArtifactMetadataRetrievalException e )
81-
{
82-
throw new MojoExecutionException( e.getMessage(), e );
83-
}
65+
useReleases(pom, dependencies);
8466
}
8567

8668
private void useReleases( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
@@ -113,11 +95,7 @@ private void useReleases( ModifiedPomXMLEventReader pom, Collection<Dependency>
11395
ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
11496
if ( versions.containsVersion( releaseVersion ) )
11597
{
116-
if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
117-
releaseVersion, getProject().getModel() ) )
118-
{
119-
getLog().info( "Updated " + toString( dep ) + " to version " + releaseVersion );
120-
}
98+
setVersion(pom, dep, version, artifact, new DefaultArtifactVersion(releaseVersion));
12199
}
122100
else
123101
{
@@ -126,10 +104,8 @@ releaseVersion, getProject().getModel() ) )
126104
{
127105
getLog().info( "No release of " + toString( dep ) + " to force." );
128106
}
129-
else if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
130-
v[v.length - 1].toString(), getProject().getModel() ) )
131-
{
132-
getLog().info( "Reverted " + toString( dep ) + " to version " + v[v.length - 1].toString() );
107+
else {
108+
setVersion(pom, dep, version, artifact, v[v.length - 1]);
133109
}
134110
}
135111
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.codehaus.mojo.versions;
2+
3+
import org.apache.maven.artifact.Artifact;
4+
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
5+
import org.apache.maven.artifact.versioning.ArtifactVersion;
6+
import org.apache.maven.model.Dependency;
7+
import org.apache.maven.plugin.MojoExecutionException;
8+
import org.apache.maven.plugin.MojoFailureException;
9+
import org.codehaus.mojo.versions.api.PomHelper;
10+
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
11+
12+
import javax.xml.stream.XMLStreamException;
13+
import java.util.ArrayList;
14+
import java.util.Collection;
15+
import java.util.Collections;
16+
import java.util.List;
17+
18+
public abstract class ParentUpdatingDependencyUpdateMojo extends AbstractVersionsDependencyUpdaterMojo
19+
{
20+
/**
21+
* @param pom the pom to update.
22+
* @throws org.apache.maven.plugin.MojoExecutionException when things go wrong
23+
* @throws org.apache.maven.plugin.MojoFailureException when things go wrong in a very bad way
24+
* @throws javax.xml.stream.XMLStreamException when things go wrong with XML streaming
25+
* @see AbstractVersionsUpdaterMojo#update(org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader)
26+
*/
27+
protected void update( ModifiedPomXMLEventReader pom )
28+
throws MojoExecutionException, MojoFailureException, XMLStreamException
29+
{
30+
try
31+
{
32+
if ( getProject().getDependencyManagement() != null && isProcessingDependencyManagement() )
33+
{
34+
setVersions( pom, getProject().getDependencyManagement().getDependencies() );
35+
}
36+
if ( getProject().getDependencies() != null && isProcessingDependencies() )
37+
{
38+
setVersions( pom, getProject().getDependencies() );
39+
}
40+
if ( getProject().getParent() != null && isProcessingParent() )
41+
{
42+
final Dependency dependency = new Dependency();
43+
dependency.setArtifactId(getProject().getParent().getArtifactId());
44+
dependency.setGroupId(getProject().getParent().getGroupId());
45+
dependency.setVersion(getProject().getParent().getVersion());
46+
dependency.setType("pom");
47+
setVersions( pom, Collections.singleton(dependency));
48+
}
49+
}
50+
catch ( ArtifactMetadataRetrievalException e )
51+
{
52+
throw new MojoExecutionException( e.getMessage(), e );
53+
}
54+
}
55+
56+
protected abstract void setVersions(ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies)
57+
throws ArtifactMetadataRetrievalException, XMLStreamException, MojoExecutionException;
58+
59+
protected void setVersion(ModifiedPomXMLEventReader pom, Dependency dep, String version, Artifact artifact, ArtifactVersion artifactVersion) throws XMLStreamException
60+
{
61+
final String newVersion = artifactVersion.toString();
62+
if(getProject().getParent() != null){
63+
if(artifact.getId().equals(getProject().getParentArtifact().getId()) && isProcessingParent())
64+
{
65+
if ( PomHelper.setProjectParentVersion( pom, newVersion.toString() ) )
66+
{
67+
getLog().debug( "Made parent change from " + version + " to " + newVersion.toString() );
68+
}
69+
}
70+
}
71+
72+
if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
73+
newVersion, getProject().getModel() ) )
74+
{
75+
getLog().info( "Changed " + toString( dep ) + " to version " + newVersion );
76+
}
77+
}
78+
}

src/main/java/org/codehaus/mojo/versions/UseDepVersionMojo.java

+12-25
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@
2626

2727
import org.apache.maven.artifact.Artifact;
2828
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
29+
import org.apache.maven.artifact.versioning.ArtifactVersion;
30+
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
2931
import org.apache.maven.model.Dependency;
3032
import org.apache.maven.plugin.MojoExecutionException;
3133
import org.apache.maven.plugin.MojoFailureException;
3234
import org.apache.maven.plugins.annotations.Mojo;
3335
import org.apache.maven.plugins.annotations.Parameter;
3436
import org.codehaus.mojo.versions.api.ArtifactVersions;
35-
import org.codehaus.mojo.versions.api.PomHelper;
3637
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
3738

3839
/**
@@ -41,7 +42,7 @@
4142
*/
4243
@Mojo( name = "use-dep-version", requiresProject = true, requiresDirectInvocation = true, threadSafe = true )
4344
public class UseDepVersionMojo
44-
extends AbstractVersionsDependencyUpdaterMojo
45+
extends ParentUpdatingDependencyUpdateMojo
4546
{
4647

4748
/**
@@ -57,10 +58,9 @@ public class UseDepVersionMojo
5758
@Parameter( property = "forceVersion", defaultValue = "false" )
5859
protected boolean forceVersion;
5960

60-
@SuppressWarnings( "unchecked" )
6161
@Override
6262
protected void update( ModifiedPomXMLEventReader pom )
63-
throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException
63+
throws MojoExecutionException, MojoFailureException, XMLStreamException
6464
{
6565

6666
if ( depVersion == null || depVersion.equals( "" ) )
@@ -73,22 +73,14 @@ protected void update( ModifiedPomXMLEventReader pom )
7373
throw new IllegalArgumentException( "The use-specific-version goal is intended to be used with a single artifact. Please specify a value for the 'includes' parameter, or use -DforceVersion=true to override this check." );
7474
}
7575

76-
try
77-
{
78-
if ( getProject().getDependencyManagement() != null && isProcessingDependencyManagement() )
79-
{
80-
useDepVersion( pom, getProject().getDependencyManagement().getDependencies() );
81-
}
76+
super.update(pom);
77+
}
8278

83-
if ( getProject().getDependencies() != null && isProcessingDependencies() )
84-
{
85-
useDepVersion( pom, getProject().getDependencies() );
86-
}
87-
}
88-
catch ( ArtifactMetadataRetrievalException e )
89-
{
90-
throw new MojoExecutionException( e.getMessage(), e );
91-
}
79+
@Override
80+
protected void setVersions(ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies)
81+
throws ArtifactMetadataRetrievalException, XMLStreamException, MojoExecutionException
82+
{
83+
useDepVersion(pom, dependencies);
9284
}
9385

9486
private void useDepVersion( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
@@ -123,12 +115,7 @@ private void useDepVersion( ModifiedPomXMLEventReader pom, Collection<Dependency
123115
}
124116

125117
String version = dep.getVersion();
126-
127-
if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, depVersion,
128-
getProject().getModel() ) )
129-
{
130-
getLog().info( "Updated " + toString( dep ) + " to version " + depVersion );
131-
}
118+
setVersion(pom, dep, version, artifact, new DefaultArtifactVersion(version));
132119
}
133120
}
134121
}

0 commit comments

Comments
 (0)