From 1ded2a869e8c763f9553931d1f5ab84c1b777b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20=C3=89pardaud?= Date: Sat, 23 Sep 2023 16:47:11 +0200 Subject: [PATCH] Support annotationProcessors in DevMojo --- .../deployment/dev/CompilationProvider.java | 9 ++- .../quarkus/deployment/dev/CompilerFlags.java | 10 +++- .../deployment/dev/DevModeContext.java | 9 +++ .../dev/JavaCompilationProvider.java | 5 +- .../deployment/dev/QuarkusCompiler.java | 1 + .../dev/QuarkusDevModeLauncher.java | 10 ++++ .../dev/filesystem/QuarkusFileManager.java | 4 +- .../deployment/dev/CompilerFlagsTest.java | 56 +++++++++---------- .../main/java/io/quarkus/maven/DevMojo.java | 21 +++++++ 9 files changed, 91 insertions(+), 34 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/CompilationProvider.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/CompilationProvider.java index 9aced27b49bf6f..552e52fce65795 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/CompilationProvider.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/CompilationProvider.java @@ -53,6 +53,7 @@ class Context { private final boolean ignoreModuleInfo; private final File generatedSourcesDirectory; private final Set annotationProcessorPaths; + private final List annotationProcessors; public Context( String name, @@ -70,6 +71,7 @@ public Context( List compilerPluginOptions, File generatedSourcesDirectory, Set annotationProcessorPaths, + List annotationProcessors, String ignoreModuleInfo) { this.name = name; this.classpath = classpath; @@ -87,6 +89,7 @@ public Context( this.ignoreModuleInfo = Boolean.parseBoolean(ignoreModuleInfo); this.generatedSourcesDirectory = generatedSourcesDirectory; this.annotationProcessorPaths = annotationProcessorPaths; + this.annotationProcessors = annotationProcessors; } public String getName() { @@ -105,6 +108,10 @@ public Set getAnnotationProcessorPaths() { return annotationProcessorPaths; } + public List getAnnotationProcessors() { + return annotationProcessors; + } + public File getProjectDirectory() { return projectDirectory; } @@ -148,7 +155,7 @@ public List getCompilerPluginOptions() { public boolean ignoreModuleInfo() { return ignoreModuleInfo; } - + public File getGeneratedSourcesDirectory() { return generatedSourcesDirectory; } diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/CompilerFlags.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/CompilerFlags.java index 13b0212b66353c..94c94fb10225dd 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/CompilerFlags.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/CompilerFlags.java @@ -19,19 +19,22 @@ public class CompilerFlags { private final String releaseJavaVersion; //can be null private final String sourceJavaVersion; //can be null private final String targetJavaVersion; //can be null + private final List annotationProcessors; //can be null public CompilerFlags( Set defaultFlags, Collection userFlags, String releaseJavaVersion, String sourceJavaVersion, - String targetJavaVersion) { + String targetJavaVersion, + List annotationProcessors) { this.defaultFlags = defaultFlags == null ? new LinkedHashSet<>() : new LinkedHashSet<>(defaultFlags); this.userFlags = userFlags == null ? new ArrayList<>() : new ArrayList<>(userFlags); this.releaseJavaVersion = releaseJavaVersion; this.sourceJavaVersion = sourceJavaVersion; this.targetJavaVersion = targetJavaVersion; + this.annotationProcessors = annotationProcessors; } public List toList() { @@ -61,6 +64,11 @@ public List toList() { } } + if (annotationProcessors != null && !annotationProcessors.isEmpty()) { + flagList.add("-processor"); + flagList.add(String.join(",", annotationProcessors)); + } + flagList.addAll(userFlags); return flagList; diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/DevModeContext.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/DevModeContext.java index 4ffc65bc847c02..6d0f4c107d9a6d 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/DevModeContext.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/DevModeContext.java @@ -65,6 +65,7 @@ public class DevModeContext implements Serializable { private final Set localArtifacts = new HashSet<>(); private Set processorPaths; + private List processors; public boolean isLocalProjectDiscovery() { return localProjectDiscovery; @@ -249,6 +250,14 @@ public Set getAnnotationProcessorPaths() { return processorPaths; } + public void setAnnotationProcessors(List processors) { + this.processors = processors; + } + + public List getAnnotationProcessors() { + return processors; + } + public static class ModuleInfo implements Serializable { private static final long serialVersionUID = -1376678003747618410L; diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/JavaCompilationProvider.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/JavaCompilationProvider.java index c828c485db482b..2e0f16e6b73daa 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/JavaCompilationProvider.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/JavaCompilationProvider.java @@ -63,7 +63,8 @@ public void compile(Set filesToCompile, CompilationProvider.Context contex context.getCompilerOptions(PROVIDER_KEY), context.getReleaseJavaVersion(), context.getSourceJavaVersion(), - context.getTargetJvmVersion()).toList(); + context.getTargetJvmVersion(), + context.getAnnotationProcessors()).toList(); } final JavaCompiler compiler = this.compiler; @@ -74,7 +75,7 @@ public void compile(Set filesToCompile, CompilationProvider.Context contex final QuarkusFileManager.Context sourcesContext = new QuarkusFileManager.Context( context.getClasspath(), context.getReloadableClasspath(), - context.getOutputDirectory(), context.getGeneratedSourcesDirectory(), + context.getOutputDirectory(), context.getGeneratedSourcesDirectory(), context.getAnnotationProcessorPaths(), context.getSourceEncoding(), context.ignoreModuleInfo()); diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusCompiler.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusCompiler.java index 94b79a900e24ea..d21664b38385c2 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusCompiler.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusCompiler.java @@ -213,6 +213,7 @@ public void setupSourceCompilationContext(DevModeContext context, compilationUnit.getGeneratedSourcesPath() == null ? null : new File(compilationUnit.getGeneratedSourcesPath()), context.getAnnotationProcessorPaths(), + context.getAnnotationProcessors(), context.getBuildSystemProperties().getOrDefault("quarkus.live-reload.ignore-module-info", "true"))); } diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java index ff5a562a71fa74..580506a736255f 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/QuarkusDevModeLauncher.java @@ -187,6 +187,12 @@ public B annotationProcessorPaths(Set processorPaths) { return (B) this; } + @SuppressWarnings("unchecked") + public B annotationProcessors(List processors) { + QuarkusDevModeLauncher.this.processors = processors; + return (B) this; + } + @SuppressWarnings("unchecked") public B releaseJavaVersion(String releaseJavaVersion) { QuarkusDevModeLauncher.this.releaseJavaVersion = releaseJavaVersion; @@ -324,6 +330,7 @@ public R build() throws Exception { private List dependencies = new ArrayList<>(0); private LinkedHashMap classpath = new LinkedHashMap<>(); private Set processorPaths; + private List processors; protected QuarkusDevModeLauncher() { } @@ -452,6 +459,9 @@ protected void prepare() throws Exception { if (processorPaths != null) { devModeContext.setAnnotationProcessorPaths(processorPaths); } + if (processors != null) { + devModeContext.setAnnotationProcessors(processors); + } devModeContext.setReleaseJavaVersion(releaseJavaVersion); devModeContext.setSourceJavaVersion(sourceJavaVersion); diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/filesystem/QuarkusFileManager.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/filesystem/QuarkusFileManager.java index 75b624df295a08..4ac5314804078e 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/filesystem/QuarkusFileManager.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/filesystem/QuarkusFileManager.java @@ -61,7 +61,7 @@ public static class Context { private final Set annotationProcessorPaths; public Context(Set classPath, Set reloadableClassPath, - File outputDirectory, File generatedSourcesDirectory, Set annotationProcessorPaths, + File outputDirectory, File generatedSourcesDirectory, Set annotationProcessorPaths, Charset sourceEncoding, boolean ignoreModuleInfo) { this.classPath = classPath; this.reloadableClassPath = reloadableClassPath; @@ -95,7 +95,7 @@ public Charset getSourceEncoding() { public boolean ignoreModuleInfo() { return ignoreModuleInfo; } - + public File getGeneratedSourcesDirectory() { return generatedSourcesDirectory; } diff --git a/core/deployment/src/test/java/io/quarkus/deployment/dev/CompilerFlagsTest.java b/core/deployment/src/test/java/io/quarkus/deployment/dev/CompilerFlagsTest.java index 47e85927228d28..833dd1655976e7 100644 --- a/core/deployment/src/test/java/io/quarkus/deployment/dev/CompilerFlagsTest.java +++ b/core/deployment/src/test/java/io/quarkus/deployment/dev/CompilerFlagsTest.java @@ -16,77 +16,77 @@ public class CompilerFlagsTest { void nullHandling() { assertAll( () -> assertEquals( - new CompilerFlags(null, null, null, null, null), - new CompilerFlags(setOf(), listOf(), null, null, null))); + new CompilerFlags(null, null, null, null, null, null), + new CompilerFlags(setOf(), listOf(), null, null, null, null))); } @Test void defaulting() { assertAll( () -> assertEquals( - new CompilerFlags(setOf("-a", "-b"), listOf(), null, null, null), - new CompilerFlags(setOf(), listOf("-a", "-b"), null, null, null)), + new CompilerFlags(setOf("-a", "-b"), listOf(), null, null, null, null), + new CompilerFlags(setOf(), listOf("-a", "-b"), null, null, null, null)), () -> assertEquals( - new CompilerFlags(setOf("-a", "-b"), listOf("-c", "-d"), null, null, null), - new CompilerFlags(setOf(), listOf("-a", "-b", "-c", "-d"), null, null, null))); + new CompilerFlags(setOf("-a", "-b"), listOf("-c", "-d"), null, null, null, null), + new CompilerFlags(setOf(), listOf("-a", "-b", "-c", "-d"), null, null, null, null))); } @Test void redundancyReduction() { assertAll( () -> assertEquals( - new CompilerFlags(setOf("-a", "-b"), listOf(), null, null, null), - new CompilerFlags(setOf(), listOf("-a", "-b"), null, null, null)), + new CompilerFlags(setOf("-a", "-b"), listOf(), null, null, null, null), + new CompilerFlags(setOf(), listOf("-a", "-b"), null, null, null, null)), () -> assertEquals( - new CompilerFlags(setOf("-a", "-b", "-c"), listOf("-a", "-b"), null, null, null), - new CompilerFlags(setOf("-c"), listOf("-a", "-b"), null, null, null))); + new CompilerFlags(setOf("-a", "-b", "-c"), listOf("-a", "-b"), null, null, null, null), + new CompilerFlags(setOf("-c"), listOf("-a", "-b"), null, null, null, null))); } @Test void sourceAndTarget() { assertAll( () -> assertEquals( - new CompilerFlags(setOf(), listOf(), "1", null, null), - new CompilerFlags(setOf(), listOf("--release", "1"), null, null, null)), + new CompilerFlags(setOf(), listOf(), "1", null, null, null), + new CompilerFlags(setOf(), listOf("--release", "1"), null, null, null, null)), () -> assertEquals( - new CompilerFlags(setOf(), listOf(), null, "2", null), - new CompilerFlags(setOf(), listOf("-source", "2"), null, null, null)), + new CompilerFlags(setOf(), listOf(), null, "2", null, null), + new CompilerFlags(setOf(), listOf("-source", "2"), null, null, null, null)), () -> assertEquals( - new CompilerFlags(setOf(), listOf(), null, null, "3"), - new CompilerFlags(setOf(), listOf("-target", "3"), null, null, null)), + new CompilerFlags(setOf(), listOf(), null, null, "3", null), + new CompilerFlags(setOf(), listOf("-target", "3"), null, null, null, null)), () -> assertEquals( - new CompilerFlags(setOf(), listOf(), "1", "2", "3"), - new CompilerFlags(setOf(), listOf("--release", "1"), null, null, null)), + new CompilerFlags(setOf(), listOf(), "1", "2", "3", null), + new CompilerFlags(setOf(), listOf("--release", "1"), null, null, null, null)), () -> assertEquals( - new CompilerFlags(setOf(), listOf(), null, "2", "3"), - new CompilerFlags(setOf(), listOf("-source", "2", "-target", "3"), null, null, null)), + new CompilerFlags(setOf(), listOf(), null, "2", "3", null), + new CompilerFlags(setOf(), listOf("-source", "2", "-target", "3"), null, null, null, null)), () -> assertEquals( - new CompilerFlags(setOf(), listOf("-source", "5", "-target", "6"), null, "2", "3"), + new CompilerFlags(setOf(), listOf("-source", "5", "-target", "6"), null, "2", "3", null), new CompilerFlags(setOf(), listOf("-source", "2", "-target", "3", "-source", "5", "-target", "6"), - null, null, null))); + null, null, null, null))); } @Test void allFeatures() { assertAll( () -> assertEquals( - new CompilerFlags(setOf("-b", "-c", "-d"), listOf("-a", "-b", "-c"), "1", "2", "3"), - new CompilerFlags(setOf(), listOf("-d", "--release", "1", "-a", "-b", "-c"), null, null, null))); + new CompilerFlags(setOf("-b", "-c", "-d"), listOf("-a", "-b", "-c"), "1", "2", "3", null), + new CompilerFlags(setOf(), listOf("-d", "--release", "1", "-a", "-b", "-c"), null, null, null, null))); assertAll( () -> assertEquals( - new CompilerFlags(setOf("-b", "-c", "-d"), listOf("-a", "-b", "-c"), null, "2", "3"), + new CompilerFlags(setOf("-b", "-c", "-d"), listOf("-a", "-b", "-c"), null, "2", "3", null), new CompilerFlags(setOf(), listOf("-d", "-source", "2", "-target", "3", "-a", "-b", "-c"), - null, null, null))); + null, null, null, null))); } @Test void listConversion() { assertAll( () -> assertEquals( - new CompilerFlags(null, null, null, null, null).toList(), + new CompilerFlags(null, null, null, null, null, null).toList(), listOf()), () -> assertEquals( - new CompilerFlags(setOf(), listOf("-a", "-b", "-c", "-d"), null, null, null).toList(), + new CompilerFlags(setOf(), listOf("-a", "-b", "-c", "-d"), null, null, null, null).toList(), listOf("-a", "-b", "-c", "-d"))); } diff --git a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java index d1efd65832f2db..03d37ba83a75f1 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java @@ -714,6 +714,22 @@ private void executeGoal(PluginExec pluginExec, String goal, Map pluginManager)); } + private List readAnnotationProcessors(Xpp3Dom pluginConfig) { + Xpp3Dom annotationProcessors = pluginConfig.getChild("annotationProcessors"); + if (annotationProcessors == null) { + return Collections.emptyList(); + } + Xpp3Dom[] processors = annotationProcessors.getChildren("annotationProcessor"); + if (processors.length == 0) { + return Collections.emptyList(); + } + List ret = new ArrayList<>(processors.length); + for (Xpp3Dom processor : processors) { + ret.add(processor.getValue()); + } + return ret; + } + private Set readAnnotationProcessorPaths(Xpp3Dom pluginConfig) throws MojoExecutionException { Xpp3Dom annotationProcessorPaths = pluginConfig.getChild("annotationProcessorPaths"); if (annotationProcessorPaths == null) { @@ -1507,6 +1523,11 @@ private void setAnnotationProcessorFlags(MavenDevModeLauncher.Builder builder) { } catch (MojoExecutionException e) { throw new RuntimeException(e); } + List processors = this.readAnnotationProcessors(compilerPluginConfiguration); + getLog().debug("Found processors: " + processors); + if (!processors.isEmpty()) { + builder.annotationProcessors(processors); + } builder.compilerPluginOptions(options); }