Skip to content

Commit

Permalink
Merge pull request bdemers#2 from McFoggy/add-cleanup-parameters
Browse files Browse the repository at this point in the history
documentation & small properties enhancements
  • Loading branch information
bdemers committed Apr 20, 2016
2 parents 3e8c06c + 63370a9 commit fdbece0
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 17 deletions.
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
.idea
**/pom.xml.new-version
.idea
**/pom.xml.new-version
.project
.classpath
.settings/
target/
**/.checkstyle
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,106 @@ Or how about a short git hash?
mvn install -Dexternal.version-qualifier=$(git rev-parse --short HEAD)
```

Configuration & parameters
-----

```xml

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-external-version-plugin</artifactId>
<version>X.Y.Z</version>
<extensions>true</extensions>
<configuration>
<strategy hint="STRATEGY"/>
<generateTemporaryFile>true/false</generateTemporaryFile>
<deleteTemporaryFile>true/false</deleteTemporaryFile>
</configuration>
</plugin>

```

- `strategy#hint` key defining which strategy implementation will be used, one of
- file: read the version from the first line of a given file
- script: version is given by the first line of the output execution of a given command
- sysprop: allows to define project version & qualifier from system properties
- `deleteTemporaryFile` will the generated pom files created by this extension will be deleted after execution. Set this parameter to _true_ to activate automatic deletion. Value is optional and defaults to _false_
- `generateTemporaryFile` if _true_ generated pom files will be created as temporary files inside the directory pointed by system property `java.io.tmpdir`. If omitted it defaults to _false_. When false, a file called `pom.xml.new-version` will be generated in the root project directory.

## Strategy : file

This strategy reads the first line of a given file to extract the version to use.

### Usage

```xml

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-external-version-plugin</artifactId>
<version>X.Y.Z</version>
<extensions>true</extensions>
<configuration>
<strategy hint="file">
<versionFilePath>SOME_FILE</versionFilePath>
</strategy>
</configuration>
</plugin>

```

### Parameters

- `versionFilePath`: denotes the file which first line will be read to extract the version from. Can be a fully qualified path or a path relative to the project directory. The parameter is optional, it defaults to `VERSION`, meaning that if not provided, a file called `VERSION` will be read from the project root.

## Strategy : script

This strategy allows to execute a given command ; the first line of stdout output will be used as version.

### Usage

```xml

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-external-version-plugin</artifactId>
<version>X.Y.Z</version>
<extensions>true</extensions>
<configuration>
<strategy hint="script">
<script>SOME_COMMAND</script>
</strategy>
</configuration>
</plugin>

```

### Parameters

- `script`: a command to execute. The parameter is optional and defaults to `./version.sh`, meaning that if not provided a file called `version.sh` in the project root will be executed.

## Strategy : sysprop

This strategy uses 2 system properties to define the new project version:

- `external.version`: the main version to use. If omitted, then it defaults to the current `project.version`.
- `external.version-qualifier`: an optional qualifier that will be appended to the given version

### Usage

``` xml

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

```

TODO:
-----
Expand Down
4 changes: 2 additions & 2 deletions external-version-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
<parent>
<groupId>org.apache.maven.version</groupId>
<artifactId>maven-external-version</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
</parent>

<artifactId>external-version-api</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>

<dependencies>

Expand Down
6 changes: 3 additions & 3 deletions external-version-strategies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
<parent>
<groupId>org.apache.maven.version</groupId>
<artifactId>maven-external-version</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
</parent>

<artifactId>external-version-strategies</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>

<dependencies>

<dependency>
<groupId>org.apache.maven.version</groupId>
<artifactId>external-version-api</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
</dependency>

</dependencies>
Expand Down
8 changes: 4 additions & 4 deletions maven-external-version-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
<parent>
<groupId>org.apache.maven.version</groupId>
<artifactId>maven-external-version</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
</parent>

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-external-version-plugin</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>

<packaging>maven-plugin</packaging>

Expand Down Expand Up @@ -61,12 +61,12 @@
<dependency>
<groupId>org.apache.maven.version</groupId>
<artifactId>external-version-api</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven.version</groupId>
<artifactId>external-version-strategies</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
</dependency>

</dependencies>
Expand Down
4 changes: 3 additions & 1 deletion maven-external-version-plugin/src/it/multi-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-external-version-plugin</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
<extensions>true</extensions>
<configuration>
<!--<strategy hint="file">-->
Expand All @@ -36,6 +36,8 @@
<!--<script>./version.sh</script>-->
<!--</strategy>-->
<strategy hint="sysprop"/>
<generateTemporaryFile>true</generateTemporaryFile>
<deleteTemporaryFile>true</deleteTemporaryFile>
</configuration>
</plugin>
</plugins>
Expand Down
2 changes: 1 addition & 1 deletion maven-external-version-plugin/src/it/simple-module/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ under the License.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-external-version-plugin</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>
<extensions>true</extensions>
<configuration>
<!--<strategy hint="file">-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,17 @@ private void createNewVersionPom( MavenProject mavenProject, Map<String, String>
model.getParent().setVersion( newVersionForParent );
}
}

File newPom = new File( mavenProject.getBasedir(), "pom.xml.new-version" );

Plugin plugin = mavenProject.getPlugin( "org.apache.maven.plugins:maven-external-version-plugin" );
// now we are going to wedge in the config
Xpp3Dom pluginConfiguration = (Xpp3Dom) plugin.getConfiguration();

File newPom = createFileFromConfiguration( mavenProject, pluginConfiguration );
logger.debug( ExternalVersionExtension.class.getSimpleName() + ": using new pom file => " + newPom );
fileWriter = new FileWriter( newPom );
new MavenXpp3Writer().write( fileWriter, model );

mavenProject.setFile( newPom );

}
finally
{
Expand All @@ -231,6 +235,50 @@ private void createNewVersionPom( MavenProject mavenProject, Map<String, String>

}

private File createFileFromConfiguration( MavenProject mavenProject, Xpp3Dom pluginConfig ) throws IOException
{
boolean deleteTemporaryFile = shouldDeleteTemporaryFile( pluginConfig );
boolean generateTemporaryFile = shouldGenerateTemporaryFile( pluginConfig );

// let's keep the default file as a default
File f = new File( mavenProject.getBasedir(), "pom.xml.new-version" );

if ( generateTemporaryFile )
{
f = File.createTempFile( "pom", ".maven-external-version" );
}

if ( deleteTemporaryFile )
{
f.deleteOnExit();
}
return f;
}

/*
* Looks for generateTemporaryFile child configuration node.
* If not present then no deletion occurs, otherwise return true if value is true, false otherwise
*/
private boolean shouldGenerateTemporaryFile( Xpp3Dom pluginConfiguration )
{
return evaluateBooleanNodeInConfiguration( pluginConfiguration, "generateTemporaryFile" );
}

/*
* Looks for deleteTemporaryFile child configuration node.
* If not present then no deletion occurs, otherwise return true if value is true, false otherwise
*/
private boolean shouldDeleteTemporaryFile( Xpp3Dom pluginConfiguration )
{
return evaluateBooleanNodeInConfiguration( pluginConfiguration, "deleteTemporaryFile" );
}

private boolean evaluateBooleanNodeInConfiguration( Xpp3Dom pluginConfiguration, String nodeName )
{
Xpp3Dom n = pluginConfiguration.getChild( nodeName );
return n != null && Boolean.parseBoolean( n.getValue() );
}

private String getNewVersion( ExternalVersionStrategy strategy, MavenProject mavenProject )
throws ExternalVersionException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public class ExternalVersionMojo

@Parameter( property = "strategy", required = true )
private String strategy;

@Parameter( property = "external-version.deleteTemporaryFile" , defaultValue = "false" )
private Boolean deleteTemporaryFile;

@Parameter( property = "external-version.generateTemporaryFile" , defaultValue = "false" )
private Boolean generateTemporaryFile;

@Override
public void execute()
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<groupId>org.apache.maven.version</groupId>
<artifactId>maven-external-version</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.1.1-SNAPSHOT</version>

<packaging>pom</packaging>

Expand Down Expand Up @@ -71,6 +71,7 @@
</goals>
<configuration>
<excludes>
<exclude>.checkstyle</exclude>
<exclude>DEPENDENCIES</exclude>
<exclude>**/src/it/**</exclude>
<exclude>README.md</exclude>
Expand Down

0 comments on commit fdbece0

Please sign in to comment.