Skip to content

Commit

Permalink
Respect processParent setting by introducing new base class
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Voss committed Jul 2, 2018
1 parent 086d126 commit 6b297e5
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 307 deletions.
15 changes: 12 additions & 3 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@
is not compatible with project minimum version, not really a proposed
upgrade

* [Fixed Issue 237][issue-237]
* [Fixed Issue 237][issue-237] and [Fixed Issue 288][issue-288]

Thanks to Julian Di Leonardo <DiJu519@users.noreply.github.com>
Thanks to Julian Di Leonardo <DiJu519@users.noreply.github.com> and [Adam Voss](https://github.com/adamvoss)

Adding parent processing to UseLatestVersion/UseLatestSnapshot/UseLatestRelease
Added parent processing to:
- UseLatestVersion
- UseLatestSnapshot
- UseLatestRelease
- ForceReleases
- UseDepVersion
- UseNextReleases
- UseNextSnapshots
- UseNextVersions

* [Fixed Issue 190][issue-190]

Expand Down Expand Up @@ -153,6 +161,7 @@
[issue-198]: https://github.com/mojohaus/versions-maven-plugin/issues/198
[issue-237]: https://github.com/mojohaus/versions-maven-plugin/issues/237
[issue-256]: https://github.com/mojohaus/versions-maven-plugin/issues/256
[issue-288]: https://github.com/mojohaus/versions-maven-plugin/issues/288

[pull-189]: https://github.com/mojohaus/versions-maven-plugin/pull/189
[pull-252]: https://github.com/mojohaus/versions-maven-plugin/pull/252
1 change: 1 addition & 0 deletions src/it/it-use-next-releases-005/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:use-next-releases
39 changes: 39 additions & 0 deletions src/it/it-use-next-releases-005/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<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-parent</artifactId>
<version>1.0</version>
</parent>

<groupId>localhost</groupId>
<artifactId>it-use-next-releases-005</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>Update a parent dependency</name>

<dependencies>

<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-impl</artifactId>
<version>1.0</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<configuration>
<processParent>true</processParent>
</configuration>
</plugin>
</plugins>
</build>

</project>
33 changes: 33 additions & 0 deletions src/it/it-use-next-releases-005/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.io.*;
import java.util.regex.*;

try
{
File file = new File( basedir, "pom.xml" );

BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( file ), "UTF-8" ) );
StringBuilder buf = new StringBuilder();
String line = in.readLine();
while ( line != null )
{
buf.append( line );
buf.append( " " );
line = in.readLine();
}

Pattern p = Pattern.compile( "\\Q<parent>\\E.*\\Q<version>\\E\\s*2\\.0\\s*\\Q</version>\\E.*\\Q</parent>\\E" );
Matcher m = p.matcher( buf.toString() );
if ( !m.find() )
{
System.out.println( "Did not update parent to version 2.0" );
return false;
}
System.out.println( m.group( 0 ) );
}
catch( Throwable t )
{
t.printStackTrace();
return false;
}

return true;
44 changes: 10 additions & 34 deletions src/main/java/org/codehaus/mojo/versions/ForceReleasesMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -44,7 +45,7 @@
*/
@Mojo( name = "force-releases", requiresProject = true, requiresDirectInvocation = true, threadSafe = true )
public class ForceReleasesMojo
extends AbstractVersionsDependencyUpdaterMojo
extends ParentUpdatingDependencyUpdateMojo
{

// ------------------------------ FIELDS ------------------------------
Expand All @@ -56,31 +57,12 @@ public class ForceReleasesMojo

// ------------------------------ METHODS --------------------------

/**
* @param pom the pom to update.
* @throws org.apache.maven.plugin.MojoExecutionException when things go wrong
* @throws org.apache.maven.plugin.MojoFailureException when things go wrong in a very bad way
* @throws javax.xml.stream.XMLStreamException when things go wrong with XML streaming
* @see AbstractVersionsUpdaterMojo#update(org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader)
*/
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException

@Override
protected void setVersions(ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies)
throws ArtifactMetadataRetrievalException, XMLStreamException, MojoExecutionException
{
try
{
if ( getProject().getDependencyManagement() != null && isProcessingDependencyManagement() )
{
useReleases( pom, getProject().getDependencyManagement().getDependencies() );
}
if ( getProject().getDependencies() != null && isProcessingDependencies() )
{
useReleases( pom, getProject().getDependencies() );
}
}
catch ( ArtifactMetadataRetrievalException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
useReleases(pom, dependencies);
}

private void useReleases( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
Expand Down Expand Up @@ -113,11 +95,7 @@ private void useReleases( ModifiedPomXMLEventReader pom, Collection<Dependency>
ArtifactVersions versions = getHelper().lookupArtifactVersions( artifact, false );
if ( versions.containsVersion( releaseVersion ) )
{
if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
releaseVersion, getProject().getModel() ) )
{
getLog().info( "Updated " + toString( dep ) + " to version " + releaseVersion );
}
setVersion(pom, dep, version, artifact, new DefaultArtifactVersion(releaseVersion));
}
else
{
Expand All @@ -126,10 +104,8 @@ releaseVersion, getProject().getModel() ) )
{
getLog().info( "No release of " + toString( dep ) + " to force." );
}
else if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
v[v.length - 1].toString(), getProject().getModel() ) )
{
getLog().info( "Reverted " + toString( dep ) + " to version " + v[v.length - 1].toString() );
else {
setVersion(pom, dep, version, artifact, v[v.length - 1]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.codehaus.mojo.versions;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

import javax.xml.stream.XMLStreamException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public abstract class ParentUpdatingDependencyUpdateMojo extends AbstractVersionsDependencyUpdaterMojo
{
/**
* @param pom the pom to update.
* @throws org.apache.maven.plugin.MojoExecutionException when things go wrong
* @throws org.apache.maven.plugin.MojoFailureException when things go wrong in a very bad way
* @throws javax.xml.stream.XMLStreamException when things go wrong with XML streaming
* @see AbstractVersionsUpdaterMojo#update(org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader)
*/
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException
{
try
{
if ( getProject().getDependencyManagement() != null && isProcessingDependencyManagement() )
{
setVersions( pom, getProject().getDependencyManagement().getDependencies() );
}
if ( getProject().getDependencies() != null && isProcessingDependencies() )
{
setVersions( pom, getProject().getDependencies() );
}
if ( getProject().getParent() != null && isProcessingParent() )
{
final Dependency dependency = new Dependency();
dependency.setArtifactId(getProject().getParent().getArtifactId());
dependency.setGroupId(getProject().getParent().getGroupId());
dependency.setVersion(getProject().getParent().getVersion());
dependency.setType("pom");
setVersions( pom, Collections.singleton(dependency));
}
}
catch ( ArtifactMetadataRetrievalException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}

protected abstract void setVersions(ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies)
throws ArtifactMetadataRetrievalException, XMLStreamException, MojoExecutionException;

protected void setVersion(ModifiedPomXMLEventReader pom, Dependency dep, String version, Artifact artifact, ArtifactVersion artifactVersion) throws XMLStreamException
{
final String newVersion = artifactVersion.toString();
if(getProject().getParent() != null){
if(artifact.getId().equals(getProject().getParentArtifact().getId()) && isProcessingParent())
{
if ( PomHelper.setProjectParentVersion( pom, newVersion.toString() ) )
{
getLog().debug( "Made parent change from " + version + " to " + newVersion.toString() );
}
}
}

if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version,
newVersion, getProject().getModel() ) )
{
getLog().info( "Changed " + toString( dep ) + " to version " + newVersion );
}
}
}
37 changes: 12 additions & 25 deletions src/main/java/org/codehaus/mojo/versions/UseDepVersionMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@

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.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

/**
Expand All @@ -41,7 +42,7 @@
*/
@Mojo( name = "use-dep-version", requiresProject = true, requiresDirectInvocation = true, threadSafe = true )
public class UseDepVersionMojo
extends AbstractVersionsDependencyUpdaterMojo
extends ParentUpdatingDependencyUpdateMojo
{

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

@SuppressWarnings( "unchecked" )
@Override
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException
throws MojoExecutionException, MojoFailureException, XMLStreamException
{

if ( depVersion == null || depVersion.equals( "" ) )
Expand All @@ -73,22 +73,14 @@ protected void update( ModifiedPomXMLEventReader pom )
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." );
}

try
{
if ( getProject().getDependencyManagement() != null && isProcessingDependencyManagement() )
{
useDepVersion( pom, getProject().getDependencyManagement().getDependencies() );
}
super.update(pom);
}

if ( getProject().getDependencies() != null && isProcessingDependencies() )
{
useDepVersion( pom, getProject().getDependencies() );
}
}
catch ( ArtifactMetadataRetrievalException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
@Override
protected void setVersions(ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies)
throws ArtifactMetadataRetrievalException, XMLStreamException, MojoExecutionException
{
useDepVersion(pom, dependencies);
}

private void useDepVersion( ModifiedPomXMLEventReader pom, Collection<Dependency> dependencies )
Expand Down Expand Up @@ -123,12 +115,7 @@ private void useDepVersion( ModifiedPomXMLEventReader pom, Collection<Dependency
}

String version = dep.getVersion();

if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, depVersion,
getProject().getModel() ) )
{
getLog().info( "Updated " + toString( dep ) + " to version " + depVersion );
}
setVersion(pom, dep, version, artifact, new DefaultArtifactVersion(version));
}
}
}
Expand Down
Loading

0 comments on commit 6b297e5

Please sign in to comment.