Skip to content

Commit 1109acf

Browse files
authored
Merge pull request #37 from avaje/maven-plugin
Generate Provides Maven Plugin
2 parents d0101af + 63e3462 commit 1109acf

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

avaje-spi-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<tag>HEAD</tag>
1414
</scm>
1515
<properties>
16-
<avaje.prisms.version>1.34</avaje.prisms.version>
16+
<avaje.prisms.version>1.36</avaje.prisms.version>
1717
</properties>
1818

1919
<dependencies>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.avaje.spi.internal;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.StandardOpenOption;
6+
7+
import javax.lang.model.element.ModuleElement;
8+
9+
final class PomPluginWriter {
10+
11+
private static final String PLUGIN =
12+
" <!-- generated by avaje spi service -->\n"
13+
+ " <plugin>\n"
14+
+ " <groupId>io.avaje</groupId>\n"
15+
+ " <artifactId>avaje-provides-maven-plugin</artifactId>\n"
16+
+ " <version>%s</version>\n"
17+
+ " <executions>\n"
18+
+ " <execution>\n"
19+
+ " <!-- Will transform compiled module-info to add missing spi provides -->"
20+
+ " <goals>\n"
21+
+ " <goal>disable-apt-validation</goal>\n"
22+
+ " <goal>add-module-spi</goal>\n"
23+
+ " </goals>\n"
24+
+ " </execution>\n"
25+
+ " </executions>\n"
26+
+ " </plugin>\n"
27+
+ " ";
28+
29+
static void addPlugin2Pom() throws IOException {
30+
var module = APContext.getProjectModuleElement();
31+
// don't need mvn plugin if not using JPMS
32+
if (disabledOrNotUsingModulePath(module)) {
33+
return;
34+
}
35+
36+
var pomPath = APContext.getBuildResource("").getParent().resolve("pom.xml");
37+
if (!pomPath.toFile().exists()) {
38+
return;
39+
}
40+
41+
var pomContent = Files.readString(pomPath);
42+
// if not already present in pom.xml
43+
if (pomContent.contains("avaje-provides-maven-plugin")) {
44+
return;
45+
}
46+
APContext.logNote("Adding avaje-provides-maven-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, "2.0"));
53+
54+
Files.writeString(
55+
pomPath, builder.toString(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
56+
}
57+
}
58+
59+
private static boolean disabledOrNotUsingModulePath(ModuleElement module) {
60+
return APContext.jdkVersion() < 24
61+
|| !APContext.getOption("buildPlugin").map(Boolean::valueOf).orElse(true)
62+
|| module == null || module.isUnnamed();
63+
}
64+
}

avaje-spi-core/src/main/java/io/avaje/spi/internal/ServiceProcessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import javax.annotation.processing.ProcessingEnvironment;
3232
import javax.annotation.processing.RoundEnvironment;
3333
import javax.annotation.processing.SupportedAnnotationTypes;
34+
import javax.annotation.processing.SupportedOptions;
3435
import javax.lang.model.SourceVersion;
3536
import javax.lang.model.element.Element;
3637
import javax.lang.model.element.ElementKind;
@@ -53,6 +54,7 @@
5354
@GenerateAPContext
5455
@SuppressWarnings("exports")
5556
@GenerateModuleInfoReader
57+
@SupportedOptions("buildPlugin")
5658
@SupportedAnnotationTypes({
5759
ServiceProviderPrism.PRISM_TYPE,
5860
// makes the processor automatically run if any of the other avaje processors are active
@@ -64,6 +66,7 @@
6466
"io.avaje.recordbuilder.Generated",
6567
"io.avaje.prism.GenerateAPContext",
6668
"io.avaje.validation.spi.Generated",
69+
"io.ebean.typequery.Generated",
6770
"javax.annotation.processing.Generated",
6871
"javax.annotation.processing.SupportedAnnotationTypes",
6972
"javax.annotation.processing.SupportedOptions",
@@ -143,6 +146,7 @@ public synchronized void init(ProcessingEnvironment env) {
143146
addition.append("avaje-spi-core");
144147
}
145148
Files.writeString(file, addition.toString(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
149+
PomPluginWriter.addPlugin2Pom();
146150
} catch (IOException e) {
147151
// not an issue worth failing over
148152
}

0 commit comments

Comments
 (0)