Skip to content

Commit

Permalink
Redefine NextRevisionMojo as ReleaseVersionMojo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-Glatz committed Mar 31, 2022
1 parent e585a6d commit 7eea90f
Show file tree
Hide file tree
Showing 12 changed files with 341 additions and 323 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,35 @@ producing an expanded version performing the following actions on the template:
* updates the values of `revision`, `sha1`, and `changelist` defined in the `<properties>` element
* writes the expanded pom file to `target/generated-poms/pom-ci.xml` and sets it as the project's `pom.xml` file

### `ci:next-revision`
By default, this aggregator goal is bound to the `validate` phase and will read the top-level project's `revision`
property and increment it according to the desired version component.
### `ci:increment-pom`
By default, this aggregator goal is bound to the `process-resources` phase and will update the project's top-level
`pom.xml` ci `revision` property with the next selected component to increment. Use to prepare the `pom.xml`
file for the next development snapshot.

Without customization, the goal will attempt to resolve the version component to increment by starting with the `build`
and working it's way up to the `major` component. The following incrementors are available:
and working it's way up to the `major` component. The following standard incrementors are available:
* `auto` (default)
* `major`
* `minor`
* `patch`
* `build`

It will write the results into `target/ci/next-revision.txt`
To use a specific incrementor:
```shell
mvn -q ci:increment-pom -Dincrementor=minor
```

### `ci:release-version`
By default, this aggregator goal is bound to the `validate` phase and will read the top-level project's `revision`
property and remove the `-SNAPSHOT` qualifier. This goal can be used if the release process is not event driven.

It will write the results into `target/ci/release-version.txt`

#### Writing to `stdout`
The goal can be executed from the command line to capture and assign the output to a variable:

```shell
next_revision=$(mvn -q ci:next-revision -Dforce-stdout=true)
```
or using a specific incrementor:
```shell
next_revision=$(mvn -q ci:next-revision -Dforce-stdout=true -incrementor=patch)
release_revision=$(mvn -q ci:release-version -Dscriptable=true)
```

### `ci:clean`
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/tools/bestquality/maven/ci/CiVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,18 @@ public CiVersion next(Incrementor incrementor)
throws MojoFailureException {
return new CiVersion()
.withRevision(nextRevision(incrementor))
.withSha1(this.sha1)
.withChangelist(this.changelist);
.withSha1(sha1)
.withChangelist(changelist);
}

public CiVersion release() {
return changelist.filter(value -> "-SNAPSHOT".equalsIgnoreCase(value))
.map(value -> withChangelist(ofNullable(null)))
.orElseGet(() ->
revision.filter(value -> value.endsWith("-SNAPSHOT"))
.map(value -> value.substring(0, value.length() - 9))
.map(value -> withRevision(value))
.orElse(CiVersion.this));
}

public String toExternalForm() {
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/tools/bestquality/maven/ci/ExportVersionMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package tools.bestquality.maven.ci;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import tools.bestquality.io.Content;

import java.io.File;
import java.nio.file.Path;

import static java.lang.String.format;
import static java.lang.System.out;
import static java.nio.file.Files.createDirectories;
import static tools.bestquality.maven.ci.CiVersion.versionFrom;

public abstract class ExportVersionMojo<M extends ExportVersionMojo<M>>
extends CiMojo {
protected final Content content;

@Parameter(defaultValue = "${project}", readonly = true, required = true)
protected MavenProject project;

@Parameter(defaultValue = "${project.build.directory}/ci")
protected File outputDirectory;

@Parameter(alias = "scriptable", property = "scriptable", defaultValue = "false")
protected boolean scriptable;

public ExportVersionMojo(Content content) {
this.content = content;
}

@SuppressWarnings("unchecked")
public M withProject(MavenProject project) {
this.project = project;
return (M) this;
}

@SuppressWarnings("unchecked")
public M withOutputDirectory(File outputDirectory) {
this.outputDirectory = outputDirectory;
return (M) this;
}

@SuppressWarnings("unchecked")
public M withScriptable(boolean forceStdout) {
this.scriptable = forceStdout;
return (M) this;
}

protected CiVersion current() {
return versionFrom(project.getProperties());
}

protected void exportVersion(String filename, String version)
throws MojoExecutionException {
if (scriptable) {
out.print(version);
out.flush();
} else {
Path directory = outputDirectory.toPath();
Path file = directory.resolve(filename);
info(format("Exporting version to %s", file.toAbsolutePath()));
try {
createDirectories(directory);
content.write(file, version);
} catch (Exception e) {
error(format("Failure exporting version to: %s", file.toAbsolutePath()), e);
throw new MojoExecutionException(e.getLocalizedMessage(), e);
}
}
}
}
33 changes: 29 additions & 4 deletions src/main/java/tools/bestquality/maven/ci/IncrementPomMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
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 tools.bestquality.io.Content;

import java.io.File;
import java.nio.file.Path;

import static java.lang.String.format;
import static org.apache.maven.plugins.annotations.LifecyclePhase.PROCESS_RESOURCES;
import static tools.bestquality.maven.versioning.StandardIncrementor.incrementor;

@Mojo(name = "increment-pom",
aggregator = true,
threadSafe = true,
defaultPhase = PROCESS_RESOURCES)
public class IncrementPomMojo
extends IncrementingMojo<IncrementPomMojo> {
extends ExportVersionMojo<IncrementPomMojo> {
@Parameter(alias = "incrementor", property = "incrementor", defaultValue = "auto")
private String incrementor;

@Parameter(defaultValue = "next-revision.txt")
private String filename;


IncrementPomMojo(Content content) {
super(content);
Expand All @@ -26,12 +34,30 @@ public IncrementPomMojo() {
this(new Content());
}

public IncrementPomMojo withIncrementor(String incrementor) {
this.incrementor = incrementor;
return this;
}

public IncrementPomMojo withFilename(String filename) {
this.filename = filename;
return this;
}

@Override
public void execute()
throws MojoExecutionException, MojoFailureException {
CiVersion next = next();
writeIncrementedPom(next.replace(readProjectPom()));
outputNextRevision(next);
exportVersion(filename, next.toExternalForm());
}

CiVersion next()
throws MojoFailureException {
CiVersion current = current();
CiVersion next = current.next(incrementor(incrementor));
info(format("Next revision is: %s", next.toExternalForm()));
return next;
}

private String readProjectPom()
Expand All @@ -46,13 +72,12 @@ private String readProjectPom()
}
}

private Path writeIncrementedPom(String pom)
private void writeIncrementedPom(String pom)
throws MojoExecutionException {
Path pomPath = project.getFile().toPath();
info(format("Writing incremented POM file to %s", pomPath.toAbsolutePath()));
try {
content.write(pomPath, pom);
return pomPath;
} catch (Exception e) {
error(format("Failure writing incremented POM file: %s", pomPath.toAbsolutePath()), e);
throw new MojoExecutionException(e.getLocalizedMessage(), e);
Expand Down
110 changes: 0 additions & 110 deletions src/main/java/tools/bestquality/maven/ci/IncrementingMojo.java

This file was deleted.

30 changes: 0 additions & 30 deletions src/main/java/tools/bestquality/maven/ci/NextRevisionMojo.java

This file was deleted.

Loading

0 comments on commit 7eea90f

Please sign in to comment.