|
| 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 | +} |
0 commit comments