diff --git a/README.md b/README.md index 5213926..c0ac051 100644 --- a/README.md +++ b/README.md @@ -250,8 +250,15 @@ dependencies { kapt(semverProcessor) compileOnly(semverProcessor) } + +kapt { + arguments { + arg("semver.properties", "$projectDir/version.properties") + } +} ``` -As of [Kotlin 1.2.30](https://blog.jetbrains.com/kotlin/2019/04/kotlin-1-3-30-released/#more-6991), when using `kapt` you must include the following in `gradle.properties`: + +The arguments block is not required if `kapt` is configured to use the Gradle Worker API in `gradle.properties`: ```ini kapt.use.worker.api=true diff --git a/examples/kotlin/build.gradle.kts b/examples/kotlin/build.gradle.kts index 6bd170e..03418b4 100644 --- a/examples/kotlin/build.gradle.kts +++ b/examples/kotlin/build.gradle.kts @@ -20,6 +20,12 @@ dependencies { implementation(kotlin("stdlib")) } +kapt { + arguments { + arg("semver.properties", "$projectDir/version.properties") + } +} + repositories { mavenLocal() jcenter() diff --git a/src/main/java/net/thauvin/erik/semver/Constants.java b/src/main/java/net/thauvin/erik/semver/Constants.java index affc701..5571ff7 100644 --- a/src/main/java/net/thauvin/erik/semver/Constants.java +++ b/src/main/java/net/thauvin/erik/semver/Constants.java @@ -138,6 +138,11 @@ public final class Constants { */ public static final String KOTLIN_TYPE = "kt"; + /** + * The argument used to specify the properties file location. + */ + public static final String SEMVER_PROPERTIES_ARG = "semver.properties"; + /** * Disables the default constructor. diff --git a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java index 75e403b..4a4a0a7 100644 --- a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java +++ b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java @@ -42,6 +42,7 @@ import javax.annotation.processing.Filer; import javax.annotation.processing.Messager; import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedOptions; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; @@ -69,6 +70,7 @@ import java.util.Set; * @created 2016-01-13 * @since 1.0 */ +@SupportedOptions({Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME, Constants.SEMVER_PROPERTIES_ARG}) public class VersionProcessor extends AbstractProcessor { private Filer filer; @@ -82,13 +84,23 @@ public class VersionProcessor extends AbstractProcessor { log(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s)); } + private String getEnv(String envOption, String defaultValue) { + if (processingEnv != null) { // null when testing. + final String prop = processingEnv.getOptions().get(envOption); + if (prop != null) { + return prop; + } + } + return defaultValue; + } + @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN") private VersionInfo findValues(final Version version) throws IOException { final VersionInfo versionInfo = new VersionInfo(version); if (version.properties().length() > 0) { - final File propsFile = new File(version.properties()); + final File propsFile = new File(getEnv(Constants.SEMVER_PROPERTIES_ARG, version.properties())); if (propsFile.isFile() && propsFile.canRead()) { note("Found properties: " + propsFile + " (" + propsFile.getAbsoluteFile().getParent() + ')'); @@ -248,19 +260,19 @@ public class VersionProcessor extends AbstractProcessor { final String fileName = versionInfo.getClassName() + '.' + type; if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) { - final String kaptGenDir = processingEnv.getOptions().get(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME); + final String kaptGenDir = getEnv(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME, null); if (kaptGenDir == null) { throw new IOException("Could not find the target directory for generated Kotlin files."); } - final File versionFile = new File(kaptGenDir, fileName); - if (!versionFile.getParentFile().exists() && !versionFile.getParentFile().mkdirs()) { - note("Could not create target directory: " + versionFile.getParentFile().getAbsolutePath()); + final File ktFile = new File(kaptGenDir, fileName); + if (!ktFile.getParentFile().exists() && !ktFile.getParentFile().mkdirs()) { + note("Could not create target directory: " + ktFile.getParentFile().getAbsolutePath()); } - try (final OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(versionFile), + try (final OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(ktFile), StandardCharsets.UTF_8)) { mustache.execute(osw, versionInfo).flush(); } - note("Generated source: " + fileName + " (" + versionFile.getParentFile().getAbsolutePath() + ')'); + note("Generated source: " + fileName + " (" + ktFile.getParentFile().getAbsolutePath() + ')'); } else { final FileObject jfo = filer.createSourceFile(versionInfo.getPackageName() + '.' + versionInfo.getClassName());