Skip to content

Commit

Permalink
Add notion of 'version qualifier to SystemPropertyStrategy
Browse files Browse the repository at this point in the history
Added back the writing out temporary pom.xml.new-version files (outside of /target so clean will not remove them)
  • Loading branch information
bdemers committed Feb 1, 2015
1 parent b11e827 commit d848f32
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
**/pom.xml.new-version
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,60 @@ branch/feature name to the version, e.g. for branch `everything`, you want to en

Need to add real example here, when I'm more awake. Until then, look at [this](https://github.com/bdemers/maven-external-version/blob/master/maven-external-version-plugin/src/it/simple-module/pom.xml#L54-L68).

Quick and Dirty Example
------------------------


In your pom add:

```xml

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-external-version-plugin</artifactId>
<version>0.1.0-SNAPSHOT</version>
<extensions>true</extensions>
<configuration>
<strategy hint="sysprop"/>
</configuration>
</plugin>

```

To replaced the whole version you can run:

```
mvn install -Dexternal.version=1.1.42
```

To add just a qualifier to the version:

```
mvn install -Dexternal.version-qualifier=rc1
# if the original version was 1.1.1 the new version would be 1.1.1-rc1
```

Add a version qualifier to all non-master branches

```
mvn install -Dexternal.version-qualifier=$(git symbolic-ref --short HEAD| sed s_^master$__)
```

Or how about a short git hash?

```
mvn install -Dexternal.version-qualifier=$(git rev-parse --short HEAD)
```


TODO:
-----

* Test with multi-module
* Add unit tests (Initial implementation was a bit exploratory hacking, hence why this is temporarily on github)
* Finalize strategy API
* Add APT for doc/site
* find out if anyone else cares about this.
* filter new versions into MavenProject and model ( potential changes to parent, dependency, and plugin versions)

Other Thoughts
---------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.StringUtils;

/**
* Loads version from the System Property 'external.version'.
Expand All @@ -33,15 +34,19 @@ public class SystemPropertyStrategy
{
private static final String EXTERNAL_VERSION = "external.version";

private static final String EXTERNAL_VERSION_QUALIFIER = "external.version-qualifier";

@Override
public String getVersion( MavenProject mavenProject )
throws ExternalVersionException
{
String newVersion = System.getProperty( EXTERNAL_VERSION );
String newVersion = System.getProperty( EXTERNAL_VERSION, mavenProject.getVersion() );
String qualifier = StringUtils.trim( System.getProperty( EXTERNAL_VERSION_QUALIFIER ) );

if ( newVersion == null )
if ( StringUtils.isNotBlank( qualifier ) )
{
throw new ExternalVersionException( "System Property '" + EXTERNAL_VERSION + "' was not set." );
// TODO: this needs to be cleaned up, the calling method will re-add the -SNAPSHOT if needed, but this is dirty
newVersion = newVersion.replaceFirst( "-SNAPSHOT", "" ) + "-" + qualifier;
}

return newVersion;
Expand Down
19 changes: 0 additions & 19 deletions maven-external-version-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,6 @@
</executions>
</plugin>

<plugin>
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>

<executions>
<execution>
<id>rat-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<excludes>
<exclude>DEPENDENCIES</exclude>
<exclude>src/it/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import org.apache.maven.MavenExecutionException;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.project.MavenProject;
import org.apache.maven.version.strategy.ExternalVersionException;
import org.apache.maven.version.strategy.ExternalVersionStrategy;
Expand All @@ -36,9 +39,16 @@
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -106,12 +116,51 @@ public void afterProjectsRead( MavenSession session )
String newFinalName = oldFinalName.replaceFirst( Pattern.quote( oldVersion ), newVersion );
logger.info( "Updating project.build.finalName: " + newFinalName );
mavenProject.getBuild().setFinalName( newFinalName );

// write processed new pom out
createNewVersionPom( mavenProject );

}
catch ( ExternalVersionException e )
{
throw new MavenExecutionException( e.getMessage(), e );
}
catch ( XmlPullParserException e )
{
throw new MavenExecutionException( e.getMessage(), e );
}
catch ( IOException e )
{
throw new MavenExecutionException( e.getMessage(), e );
}
}
}

private void createNewVersionPom( MavenProject mavenProject )
throws IOException, XmlPullParserException
{
Reader fileReader = null;
Writer fileWriter = null;
try
{
fileReader = new FileReader( mavenProject.getFile() );
Model model = new MavenXpp3Reader().read( fileReader );
model.setVersion( mavenProject.getVersion() );


File newPom = new File( mavenProject.getBasedir(), "pom.xml.new-version" );
fileWriter = new FileWriter( newPom );
new MavenXpp3Writer().write( fileWriter, model );

mavenProject.setFile( newPom );

}
finally
{
IOUtil.close( fileReader );
}


}

private String getNewVersion( ExternalVersionStrategy strategy, MavenProject mavenProject )
Expand Down
27 changes: 27 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,31 @@
</dependencies>
</dependencyManagement>

<build>
<!--<pluginManagement>-->
<plugins>
<plugin>
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>

<executions>
<execution>
<id>rat-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<excludes>
<exclude>DEPENDENCIES</exclude>
<exclude>**/src/it/**</exclude>
<exclude>README.md</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!--</pluginManagement>-->
</build>

</project>

0 comments on commit d848f32

Please sign in to comment.