From 26e43b09d40a265cddc7d17c8c0166c04dca676d Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 30 Jun 2018 18:13:22 -0700 Subject: [PATCH] Added keysPrefix property. --- README.md | 68 +++++++------------ .../net/thauvin/erik/semver/Constants.java | 18 +++-- .../java/net/thauvin/erik/semver/Version.java | 2 + .../thauvin/erik/semver/VersionProcessor.java | 18 +++-- 4 files changed, 49 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 6f5abe0..e514584 100644 --- a/README.md +++ b/README.md @@ -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). -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 @@ -128,18 +128,20 @@ Element | Property | Description | Defau `properties` | | The properties file. | `template` | | The template file. | `version.mustache` `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: ```java @Version( properties = "example.properties", - majorKey = "example.major", - minorKey = "example.minor", - patchKey = "example.patch", - preReleaseKey = "example.prerelease", - buildMetaKey = "example.buildmeta", - projectKey = "example.project" + keysPrefix = "example." + majorKey = "maj", + minorKey = "min", + patchKey = "build", + preReleaseKey = "rel", + buildMetaKey = "meta", + projectKey = "project" ) public class Example { // ... @@ -148,11 +150,17 @@ public class Example { ```ini # example.properties example.project=Example -example.major=1 -example.minor=0 -example.patch=0 +example.maj=1 +example.min=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 ### Maven @@ -175,6 +183,7 @@ To install and run from [Gradle](https://gradle.org/), add the following to the ```gradle dependencies { + annotationProcessor '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 -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 -plugins { - id "com.ewerk.gradle.plugins.annotation-processor" version "1.0.4" -} +compileJava.options.annotationProcessorGeneratedSourcesDirectory = file("${projectDir}/src/generated") ``` -Then add the following to the `build.gradle` file: - -```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. +The `GeneratedVersion.java` file will now be located in `src/generated`. 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)). -### Auto-Increment +## Auto-Increment -Incrementing the version is best left to your favorite build system. - -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. \ No newline at end of file +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). \ No newline at end of file diff --git a/src/main/java/net/thauvin/erik/semver/Constants.java b/src/main/java/net/thauvin/erik/semver/Constants.java index 47461cd..d26a65c 100644 --- a/src/main/java/net/thauvin/erik/semver/Constants.java +++ b/src/main/java/net/thauvin/erik/semver/Constants.java @@ -51,6 +51,10 @@ public final class Constants { * The default java type. **/ 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. */ @@ -70,7 +74,7 @@ public final class Constants { /** * The default mustache template. */ - public static final String DEFAULT_TEMPLATE_NAME = "version.mustache"; + public static final String DEFAULT_TEMPLATE_NAME = "mustache"; /** * The empty string. */ @@ -78,27 +82,27 @@ public final class Constants { /** * 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. */ - public static final String KEY_VERSION_MAJOR = "version.major"; + public static final String KEY_VERSION_MAJOR = "major"; /** * 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. */ - public static final String KEY_VERSION_PATCH = "version.patch"; + public static final String KEY_VERSION_PATCH = "patch"; /** * 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. */ - public static final String KEY_VERSION_PROJECT = "version.project"; + public static final String KEY_VERSION_PROJECT = "project"; /** * The kotlin type. */ diff --git a/src/main/java/net/thauvin/erik/semver/Version.java b/src/main/java/net/thauvin/erik/semver/Version.java index 3d025c3..7795675 100644 --- a/src/main/java/net/thauvin/erik/semver/Version.java +++ b/src/main/java/net/thauvin/erik/semver/Version.java @@ -79,4 +79,6 @@ public @interface Version { String template() default Constants.DEFAULT_JAVA_TEMPLATE; String type() default Constants.DEFAULT_JAVA_TYPE; + + String keysPefix() default Constants.DEFAULT_KEYS_PREFIX; } \ No newline at end of file diff --git a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java index c9826fd..43757d1 100644 --- a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java +++ b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java @@ -82,12 +82,18 @@ public class VersionProcessor extends AbstractProcessor { try (FileReader reader = new FileReader(propsFile)) { p.load(reader); - versionInfo.setProject(p.getProperty(version.projectKey(), version.project())); - versionInfo.setMajor(parseIntProperty(p, version.majorKey(), version.major())); - versionInfo.setMinor(parseIntProperty(p, version.minorKey(), version.minor())); - versionInfo.setPatch(parseIntProperty(p, version.patchKey(), version.patch())); - versionInfo.setBuildMeta(p.getProperty(version.buildMetaKey(), version.buildMeta())); - versionInfo.setPreRelease(p.getProperty(version.preReleaseKey(), version.preRelease())); + versionInfo.setProject( + p.getProperty(version.keysPefix() + version.projectKey(), version.project())); + versionInfo.setMajor( + parseIntProperty(p, version.keysPefix() + version.majorKey(), version.major())); + versionInfo.setMinor( + 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 { error("Could not find: " + propsFile);