Skip to content

Auto add the maven plugin #743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
@GenerateUtils
@GenerateAPContext
@GenerateModuleInfoReader
@SupportedOptions("mergeServices")
@SupportedOptions({"mergeServices", "buildPlugin"})
@SupportedAnnotationTypes({
AspectImportPrism.PRISM_TYPE,
AssistFactoryPrism.PRISM_TYPE,
Expand Down Expand Up @@ -88,6 +88,8 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
addition.append("avaje-inject-generator");
}
Files.writeString(file, addition.toString(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);

PomPluginWriter.addPlugin2Pom();
} catch (IOException e) {
// not an issue worth failing over
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.avaje.inject.generator;

import javax.lang.model.element.ModuleElement;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;

final class PomPluginWriter {

private static final String plugin =
" <!-- generated by avaje inject -->\n"
+ " <plugin>\n"
+ " <groupId>io.avaje</groupId>\n"
+ " <artifactId>avaje-inject-maven-plugin</artifactId>\n"
+ " <version>%s</version>\n"
+ " <executions>\n"
+ " <execution>\n"
+ " <phase>process-sources</phase>\n"
+ " <goals>\n"
+ " <goal>provides</goal>\n"
+ " </goals>\n"
+ " </execution>\n"
+ " </executions>\n"
+ " </plugin>\n"
+ " ";

static void addPlugin2Pom() throws IOException {
var module = APContext.getProjectModuleElement();
// don't need mvn plugin with JPMS
if (disabledOrUsingModulePath(module)) {
return;
}

var pomPath = APContext.getBuildResource("").getParent().resolve("pom.xml");
if (!pomPath.toFile().exists()) {
return;
}

var pomContent = Files.readString(pomPath);
// don't need mvn plugin if not using mvn compiler annotationProcessors
if (pomContent.contains("avaje-inject-maven-plugin")
|| !pomContent.contains("<annotationProcessors>")
&& !pomContent.contains("<annotationProcessorPaths>")) {
return;
}
APContext.logNote("Adding avaje-inject-maven-plugin Plugin to pom");
var pluginsIndex = pomContent.indexOf("</plugins>");
var builder = new StringBuilder(pomContent);
if (pluginsIndex != -1) {
builder.insert(
pluginsIndex,
String.format(plugin, PomPluginWriter.class.getPackage().getImplementationVersion()));

Files.writeString(
pomPath, builder.toString(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
}
}

private static boolean disabledOrUsingModulePath(ModuleElement module) {
return !APContext.getOption("buildPlugin").map(Boolean::valueOf).orElse(true)
|| module != null && !module.isUnnamed();
}
}
4 changes: 3 additions & 1 deletion inject-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-AbuildPlugin=false</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>io.avaje</groupId>
Expand All @@ -140,7 +143,6 @@
</annotationProcessorPaths>
</configuration>
</plugin>

</plugins>
</build>
</project>
Loading