Added keysPrefix property.

This commit is contained in:
Erik C. Thauvin 2018-06-30 18:13:22 -07:00
parent 9e899b2d88
commit 26e43b09d4
4 changed files with 49 additions and 57 deletions

View file

@ -5,7 +5,7 @@
An [annotation processor](https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/Processor.html) that automatically generates a `GeneratedVersion` class based on a [Mustache](https://mustache.github.io/) template and containing the [semantic version](http://semver.org/) (major, minor, patch, etc.) that is read from a `Properties` file or defined in the [annotation](https://docs.oracle.com/javase/tutorial/java/annotations/basics.html). An [annotation processor](https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/Processor.html) that automatically generates a `GeneratedVersion` class based on a [Mustache](https://mustache.github.io/) template and containing the [semantic version](http://semver.org/) (major, minor, patch, etc.) that is read from a `Properties` file or defined in the [annotation](https://docs.oracle.com/javase/tutorial/java/annotations/basics.html).
This processor was inspired by Cédric Beust's [version-processor](https://github.com/cbeust/version-processor). This processor was inspired by Cédric Beust's [version-processor](https://github.com/cbeust/version-processor) and works well in conjunction with the [Semantic Version Plugin for Gralde](https://github.com/ethauvin/semver-gradle).
## Examples ## Examples
@ -128,18 +128,20 @@ Element | Property | Description | Defau
`properties` | | The properties file. | `properties` | | The properties file. |
`template` | | The template file. | `version.mustache` `template` | | The template file. | `version.mustache`
`type` | | Either `java` or `kt` for Kotlin. | `java` `type` | | Either `java` or `kt` for Kotlin. | `java`
`keysPrefix` | | The prefix for all property keys. | `version.`
In order to easily incorporate with existing projects, the property keys may be assigned custom values: In order to easily incorporate with existing projects, the property keys may be assigned custom values:
```java ```java
@Version( @Version(
properties = "example.properties", properties = "example.properties",
majorKey = "example.major", keysPrefix = "example."
minorKey = "example.minor", majorKey = "maj",
patchKey = "example.patch", minorKey = "min",
preReleaseKey = "example.prerelease", patchKey = "build",
buildMetaKey = "example.buildmeta", preReleaseKey = "rel",
projectKey = "example.project" buildMetaKey = "meta",
projectKey = "project"
) )
public class Example { public class Example {
// ... // ...
@ -148,11 +150,17 @@ public class Example {
```ini ```ini
# example.properties # example.properties
example.project=Example example.project=Example
example.major=1 example.maj=1
example.minor=0 example.min=0
example.patch=0 example.build=0
example.rel=beta
example.meta=
# ... # ...
``` ```
> :warning: `keysPrefix` is a new element in `1.0.0` and may break older versions when using custom property keys.
> :zap: A quick fix is to include `keysPrefix=""` in the annotation to remove the default `version.` prefix.
## Usage with Maven, Grail, Kobalt and Kotlin ## Usage with Maven, Grail, Kobalt and Kotlin
### Maven ### Maven
@ -175,6 +183,7 @@ To install and run from [Gradle](https://gradle.org/), add the following to the
```gradle ```gradle
dependencies { dependencies {
annotationProcessor 'net.thauvin.erik:semver:1.0.1'
compileOnly 'net.thauvin.erik:semver:1.0.1' compileOnly 'net.thauvin.erik:semver:1.0.1'
} }
``` ```
@ -183,34 +192,13 @@ The `GeneratedVersion` class will be automatically created in the `build/generat
#### Class & Source Generation #### Class & Source Generation
In order to also incorporate the generated source code into the `source tree`, use the [EWERK Annotation Processor Plugin](https://github.com/ewerk/gradle-plugins/tree/master/annotation-processor-plugin). Start by adding the following to the very top of the `build.gradle` file: In order to also incorporate the generated source code into the `source tree`, add the following to the very top of the `build.gradle` file:
```gradle ```gradle
plugins { compileJava.options.annotationProcessorGeneratedSourcesDirectory = file("${projectDir}/src/generated")
id "com.ewerk.gradle.plugins.annotation-processor" version "1.0.4"
}
``` ```
Then add the following to the `build.gradle` file: The `GeneratedVersion.java` file will now be located in `src/generated`.
```gradle
dependencies {
compileOnly 'net.thauvin.erik:semver:1.0.1'
}
annotationProcessor {
library 'net.thauvin.erik:semver:1.0.1'
processor 'net.thauvin.erik.semver.VersionProcessor'
// sourcesDir 'src/generated/java'
}
compileJava {
// Disable the classpath processor
options.compilerArgs << '-proc:none'
}
```
The plugin implements a separate compile task that only runs the annotation processor and is executed during the build phase.
Please look at the [build.gradle](https://github.com/ethauvin/semver/blob/master/example/build.gradle) file in the [example](https://github.com/ethauvin/semver/tree/master/example) module directory for a sample. Please look at the [build.gradle](https://github.com/ethauvin/semver/blob/master/example/build.gradle) file in the [example](https://github.com/ethauvin/semver/tree/master/example) module directory for a sample.
@ -244,14 +232,6 @@ The [Kotlin default template](https://github.com/ethauvin/semver/blob/master/src
Please look at the [Example for Kotlin](https://github.com/ethauvin/semver-example-kotlin) project for samples on using Gradle ([build.gradle](https://github.com/ethauvin/semver-example-kotlin/blob/master/build.gradle)) and Kobalt ([Build.kt](https://github.com/ethauvin/semver-example-kotlin/blob/master/kobalt/src/Build.kt)). Please look at the [Example for Kotlin](https://github.com/ethauvin/semver-example-kotlin) project for samples on using Gradle ([build.gradle](https://github.com/ethauvin/semver-example-kotlin/blob/master/build.gradle)) and Kobalt ([Build.kt](https://github.com/ethauvin/semver-example-kotlin/blob/master/kobalt/src/Build.kt)).
### Auto-Increment ## Auto-Increment
Incrementing the version is best left to your favorite build system. Incrementing the version is best left to your favorite build system. For a solution using Gradle, please have a look at the [Semver Version Plugin for Gradle](https://github.com/ethauvin/semver-gradle).
For a solution using [Gradle](https://gradle.org/), please have a look at the [build.gradle](https://github.com/ethauvin/semver/blob/master/example/build.gradle) file in the [example](https://github.com/ethauvin/semver/tree/master/example) module directory. To run the example with patch version auto-incrementing, issue the following command:
```
gradle release run
```
For a solution using [Kobalt](http://beust.com/kobalt/) look at my [Property File Editor](https://github.com/ethauvin/kobalt-property-file) plug-in.

View file

@ -51,6 +51,10 @@ public final class Constants {
* The default java type. * The default java type.
**/ **/
public static final String DEFAULT_JAVA_TYPE = "java"; public static final String DEFAULT_JAVA_TYPE = "java";
/**
* The default keys prefix.
*/
public static final String DEFAULT_KEYS_PREFIX = "version." ;
/** /**
* The default Kotlin mustache template. * The default Kotlin mustache template.
*/ */
@ -70,7 +74,7 @@ public final class Constants {
/** /**
* The default mustache template. * The default mustache template.
*/ */
public static final String DEFAULT_TEMPLATE_NAME = "version.mustache"; public static final String DEFAULT_TEMPLATE_NAME = "mustache";
/** /**
* The empty string. * The empty string.
*/ */
@ -78,27 +82,27 @@ public final class Constants {
/** /**
* The build metadata property key. * The build metadata property key.
*/ */
public static final String KEY_VERSION_BUILDMETA = "version.buildmeta"; public static final String KEY_VERSION_BUILDMETA = "buildmeta";
/** /**
* The major version property key. * The major version property key.
*/ */
public static final String KEY_VERSION_MAJOR = "version.major"; public static final String KEY_VERSION_MAJOR = "major";
/** /**
* The minor version property key. * The minor version property key.
*/ */
public static final String KEY_VERSION_MINOR = "version.minor"; public static final String KEY_VERSION_MINOR = "minor";
/** /**
* The patch version property key. * The patch version property key.
*/ */
public static final String KEY_VERSION_PATCH = "version.patch"; public static final String KEY_VERSION_PATCH = "patch";
/** /**
* The pre-release version property key. * The pre-release version property key.
*/ */
public static final String KEY_VERSION_PRERELEASE = "version.prerelease"; public static final String KEY_VERSION_PRERELEASE = "prerelease";
/** /**
* The project property key. * The project property key.
*/ */
public static final String KEY_VERSION_PROJECT = "version.project"; public static final String KEY_VERSION_PROJECT = "project";
/** /**
* The kotlin type. * The kotlin type.
*/ */

View file

@ -79,4 +79,6 @@ public @interface Version {
String template() default Constants.DEFAULT_JAVA_TEMPLATE; String template() default Constants.DEFAULT_JAVA_TEMPLATE;
String type() default Constants.DEFAULT_JAVA_TYPE; String type() default Constants.DEFAULT_JAVA_TYPE;
String keysPefix() default Constants.DEFAULT_KEYS_PREFIX;
} }

View file

@ -82,12 +82,18 @@ public class VersionProcessor extends AbstractProcessor {
try (FileReader reader = new FileReader(propsFile)) { try (FileReader reader = new FileReader(propsFile)) {
p.load(reader); p.load(reader);
versionInfo.setProject(p.getProperty(version.projectKey(), version.project())); versionInfo.setProject(
versionInfo.setMajor(parseIntProperty(p, version.majorKey(), version.major())); p.getProperty(version.keysPefix() + version.projectKey(), version.project()));
versionInfo.setMinor(parseIntProperty(p, version.minorKey(), version.minor())); versionInfo.setMajor(
versionInfo.setPatch(parseIntProperty(p, version.patchKey(), version.patch())); parseIntProperty(p, version.keysPefix() + version.majorKey(), version.major()));
versionInfo.setBuildMeta(p.getProperty(version.buildMetaKey(), version.buildMeta())); versionInfo.setMinor(
versionInfo.setPreRelease(p.getProperty(version.preReleaseKey(), version.preRelease())); parseIntProperty(p, version.keysPefix() + version.minorKey(), version.minor()));
versionInfo.setPatch(
parseIntProperty(p, version.keysPefix() + version.patchKey(), version.patch()));
versionInfo.setBuildMeta(
p.getProperty(version.keysPefix() + version.buildMetaKey(), version.buildMeta()));
versionInfo.setPreRelease(
p.getProperty(version.keysPefix() + version.preReleaseKey(), version.preRelease()));
} }
} else { } else {
error("Could not find: " + propsFile); error("Could not find: " + propsFile);