Skip to content

Commit c036018

Browse files
authored
Auto add the maven plugin (#743)
* Will automatically add the maven plugin * Update PomPluginWriter.java
1 parent 53b56a7 commit c036018

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

inject-generator/src/main/java/io/avaje/inject/generator/InjectProcessor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
@GenerateUtils
2929
@GenerateAPContext
3030
@GenerateModuleInfoReader
31-
@SupportedOptions("mergeServices")
31+
@SupportedOptions({"mergeServices", "buildPlugin"})
3232
@SupportedAnnotationTypes({
3333
AspectImportPrism.PRISM_TYPE,
3434
AssistFactoryPrism.PRISM_TYPE,
@@ -88,6 +88,8 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
8888
addition.append("avaje-inject-generator");
8989
}
9090
Files.writeString(file, addition.toString(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
91+
92+
PomPluginWriter.addPlugin2Pom();
9193
} catch (IOException e) {
9294
// not an issue worth failing over
9395
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.avaje.inject.generator;
2+
3+
import javax.lang.model.element.ModuleElement;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.StandardOpenOption;
7+
8+
final class PomPluginWriter {
9+
10+
private static final String plugin =
11+
" <!-- generated by avaje inject -->\n"
12+
+ " <plugin>\n"
13+
+ " <groupId>io.avaje</groupId>\n"
14+
+ " <artifactId>avaje-inject-maven-plugin</artifactId>\n"
15+
+ " <version>%s</version>\n"
16+
+ " <executions>\n"
17+
+ " <execution>\n"
18+
+ " <phase>process-sources</phase>\n"
19+
+ " <goals>\n"
20+
+ " <goal>provides</goal>\n"
21+
+ " </goals>\n"
22+
+ " </execution>\n"
23+
+ " </executions>\n"
24+
+ " </plugin>\n"
25+
+ " ";
26+
27+
static void addPlugin2Pom() throws IOException {
28+
var module = APContext.getProjectModuleElement();
29+
// don't need mvn plugin with JPMS
30+
if (disabledOrUsingModulePath(module)) {
31+
return;
32+
}
33+
34+
var pomPath = APContext.getBuildResource("").getParent().resolve("pom.xml");
35+
if (!pomPath.toFile().exists()) {
36+
return;
37+
}
38+
39+
var pomContent = Files.readString(pomPath);
40+
// don't need mvn plugin if not using mvn compiler annotationProcessors
41+
if (pomContent.contains("avaje-inject-maven-plugin")
42+
|| !pomContent.contains("<annotationProcessors>")
43+
&& !pomContent.contains("<annotationProcessorPaths>")) {
44+
return;
45+
}
46+
APContext.logNote("Adding avaje-inject-maven-plugin Plugin to pom");
47+
var pluginsIndex = pomContent.indexOf("</plugins>");
48+
var builder = new StringBuilder(pomContent);
49+
if (pluginsIndex != -1) {
50+
builder.insert(
51+
pluginsIndex,
52+
String.format(plugin, PomPluginWriter.class.getPackage().getImplementationVersion()));
53+
54+
Files.writeString(
55+
pomPath, builder.toString(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
56+
}
57+
}
58+
59+
private static boolean disabledOrUsingModulePath(ModuleElement module) {
60+
return !APContext.getOption("buildPlugin").map(Boolean::valueOf).orElse(true)
61+
|| module != null && !module.isUnnamed();
62+
}
63+
}

inject-test/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@
131131
<groupId>org.apache.maven.plugins</groupId>
132132
<artifactId>maven-compiler-plugin</artifactId>
133133
<configuration>
134+
<compilerArgs>
135+
<arg>-AbuildPlugin=false</arg>
136+
</compilerArgs>
134137
<annotationProcessorPaths>
135138
<path>
136139
<groupId>io.avaje</groupId>
@@ -140,7 +143,6 @@
140143
</annotationProcessorPaths>
141144
</configuration>
142145
</plugin>
143-
144146
</plugins>
145147
</build>
146148
</project>

0 commit comments

Comments
 (0)