diff --git a/src/main/java/net/thauvin/erik/semver/Constants.java b/src/main/java/net/thauvin/erik/semver/Constants.java index b14b090..6dee004 100644 --- a/src/main/java/net/thauvin/erik/semver/Constants.java +++ b/src/main/java/net/thauvin/erik/semver/Constants.java @@ -79,6 +79,10 @@ public final class Constants { * The empty string. */ public static final String EMPTY = ""; + /** + * The generated source files directory kotlin option. + */ + public static final String KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"; /** * The build metadata property key. */ @@ -108,6 +112,7 @@ public final class Constants { */ public static final String KOTLIN_TYPE = "kt"; + /** * Disables the default constructor. * @@ -117,4 +122,4 @@ public final class Constants { throws UnsupportedOperationException { throw new UnsupportedOperationException("Illegal constructor call."); } -} \ 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 0c15a6a..421ece7 100644 --- a/src/main/java/net/thauvin/erik/semver/VersionProcessor.java +++ b/src/main/java/net/thauvin/erik/semver/VersionProcessor.java @@ -44,10 +44,10 @@ import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic; import javax.tools.FileObject; -import javax.tools.StandardLocation; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.HashSet; +import java.util.Map; import java.util.Properties; import java.util.Set; @@ -68,7 +68,7 @@ public class VersionProcessor extends AbstractProcessor { } private void error(final String s, final Throwable t) { - messager.printMessage(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s)); + log(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s)); } @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN") @@ -108,7 +108,7 @@ public class VersionProcessor extends AbstractProcessor { findOrRead = "read"; } error("Could not " + findOrRead + ": " + propsFile); - throw new FileNotFoundException("The system cannot " + findOrRead + " the specified file: `" + throw new FileNotFoundException("Could not " + findOrRead + " the specified file: `" + propsFile.getAbsolutePath() + '`'); } } @@ -188,6 +188,7 @@ public class VersionProcessor extends AbstractProcessor { } else { template = version.template(); } + writeTemplate(version.type(), versionInfo, template); } catch (IOException e) { error("IOException occurred while running the annotation processor: " + e.getMessage(), e); @@ -203,7 +204,9 @@ public class VersionProcessor extends AbstractProcessor { } @SuppressFBWarnings(value = "PATH_TRAVERSAL_IN") - private void writeTemplate(final String type, final VersionInfo versionInfo, final String template) + private void writeTemplate(final String type, + final VersionInfo versionInfo, + final String template) throws IOException { final MustacheFactory mf = new DefaultMustacheFactory(); final Mustache mustache = mf.compile(template); @@ -222,20 +225,28 @@ public class VersionProcessor extends AbstractProcessor { } note("Loaded template: " + templateName); - final FileObject jfo; final String fileName = versionInfo.getClassName() + '.' + type; if (type.equalsIgnoreCase(Constants.KOTLIN_TYPE)) { - jfo = filer.createResource(StandardLocation.SOURCE_OUTPUT, versionInfo.getPackageName(), - fileName); + final Map options = processingEnv.getOptions(); + final String kaptGenDir = options.get(Constants.KAPT_KOTLIN_GENERATED_OPTION_NAME); + if (kaptGenDir == null) { + throw new IOException("Could not find the target directory for generated Kotlin files."); + } + final File versionFile = new File(kaptGenDir, fileName); + versionFile.getParentFile().mkdirs(); + try (final FileWriter fw = new FileWriter(versionFile);) { + mustache.execute(fw, versionInfo).flush(); + } + note("Generated source: " + fileName + " (" + versionFile.getParentFile().getAbsolutePath() + ')'); } else { - jfo = filer.createSourceFile(versionInfo.getPackageName() + '.' + versionInfo.getClassName()); + final FileObject jfo = filer.createSourceFile(versionInfo.getPackageName() + '.' + + versionInfo.getClassName()); + try (final Writer writer = jfo.openWriter()) { + mustache.execute(writer, versionInfo).flush(); + } + note("Generated source: " + fileName + " (" + + new File(jfo.getName()).getAbsoluteFile().getParent() + ')'); } - - try (final Writer writer = jfo.openWriter()) { - mustache.execute(writer, versionInfo).flush(); - } - - note("Generated source: " + fileName + " (" + new File(jfo.getName()).getAbsoluteFile().getParent() + ')'); } }