From 482ab34e4de6c4486a979d24c388596fbec9b440 Mon Sep 17 00:00:00 2001 From: Rune Flobakk Date: Tue, 30 Jan 2024 18:26:25 +0100 Subject: [PATCH] Add generated code to test sources in build --- .../record/GenerateRecordMatcherMojo.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/maven-plugin/src/main/java/no/rune/record/GenerateRecordMatcherMojo.java b/maven-plugin/src/main/java/no/rune/record/GenerateRecordMatcherMojo.java index d533f87..d515dde 100644 --- a/maven-plugin/src/main/java/no/rune/record/GenerateRecordMatcherMojo.java +++ b/maven-plugin/src/main/java/no/rune/record/GenerateRecordMatcherMojo.java @@ -28,14 +28,36 @@ public class GenerateRecordMatcherMojo extends AbstractMojo { private static final Logger LOG = LoggerFactory.getLogger(GenerateRecordMatcherMojo.class); + private static final String PLUGIN_CONF_PROP_PREFIX = "recordmatcher."; + /** + * Specifies the fully qualified class names of the records to + * generate Hamcrest matchers for. + */ @Parameter private Set includes; - @Parameter(defaultValue = "${project.build.directory}/generated-test-sources/record-matchers", required = true) + + /** + * The directory where the generated Hamcrest matchers will be written to. + */ + @Parameter(required = true, + defaultValue = "${project.build.directory}/generated-test-sources/record-matchers", + property = PLUGIN_CONF_PROP_PREFIX + "outputDirectory") private File outputDirectory; - @Parameter(defaultValue = "${project}", required = true, readonly = true) + + /** + * Whether the {@link #outputDirectory} should be included as a test sources root. + */ + @Parameter(required = true, + defaultValue = "true", + property = PLUGIN_CONF_PROP_PREFIX + "includeGeneratedCodeAsTestSources") + private boolean includeGeneratedCodeAsTestSources; + + + @Parameter(required = true, readonly = true, + defaultValue = "${project}") private MavenProject mavenProject; @@ -43,6 +65,10 @@ public class GenerateRecordMatcherMojo extends AbstractMojo { public void execute() throws MojoExecutionException, MojoFailureException { if (includes != null && !includes.isEmpty()) { Path outputDirectory = resolveOutputDirectory(); + if (includeGeneratedCodeAsTestSources) { + mavenProject.addTestCompileSourceRoot(outputDirectory.toString()); + LOG.debug("{} has been added as a compiler test sources directory", outputDirectory); + } try { Files.createDirectories(outputDirectory); LOG.info("Generating Hamcrest matchers in {}", outputDirectory);