Skip to content

Commit

Permalink
auto add maven plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
SentryMan committed Dec 18, 2024
1 parent 27ba651 commit 6566e9f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,18 @@
</profile>
</profiles>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-AbuildPlugin=false</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.ebean.querybean.generator;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;

final class PomPluginWriter {

private static final String PLUGIN =
" <!-- generated by ebean query generator -->\n"
+ " <plugin>\n"
+ " <groupId>io.ebean</groupId>\n"
+ " <artifactId>ebean-maven-plugin</artifactId>\n"
+ " <version>%s</version>\n"
+ " <executions>\n"
+ " <execution>\n"
+ " <!-- Will enhance entity classes to add missing spi provides -->"
+ " <goals>\n"
+ " <goal>enhance</goal>\n"
+ " <goal>testEnhance</goal>\n"
+ " </goals>\n"
+ " </execution>\n"
+ " </executions>\n"
+ " </plugin>\n"
+ " ";

static void addPlugin2Pom() throws IOException {

if (disabled()) {
return;
}

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

var pomContent = Files.readString(pomPath);
// if not already present in pom.xml
if (pomContent.contains("ebean-maven-plugin")) {
return;
}
APContext.logNote("Adding ebean-maven-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 disabled() {
return !APContext.getOption("buildPlugin").map(Boolean::valueOf).orElse(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import static io.ebean.querybean.generator.APContext.logError;
import static io.ebean.querybean.generator.APContext.logNote;
import static io.ebean.querybean.generator.APContext.typeElement;
import static java.util.stream.Collectors.joining;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
Expand All @@ -24,6 +30,7 @@
@GenerateUtils
@GenerateAPContext
@GenerateModuleInfoReader
@SupportedOptions("buildPlugin")
@SupportedAnnotationTypes({
ConverterPrism.PRISM_TYPE,
EbeanComponentPrism.PRISM_TYPE,
Expand All @@ -39,6 +46,26 @@ public class Processor extends AbstractProcessor implements Constants {
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
ProcessingContext.init(processingEnv);
try {

// write a note in target so that other apts can know spi is running
var file = APContext.getBuildResource("avaje-processors.txt");
var addition = new StringBuilder();
//if file exists, dedup and append current processor
if (file.toFile().exists()) {
var result =
Stream.concat(Files.lines(file), Stream.of("ebean-querybean-generator"))
.distinct()
.collect(joining("\n"));
addition.append(result);
} else {
addition.append("ebean-querybean-generator");
}
Files.writeString(file, addition.toString(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
PomPluginWriter.addPlugin2Pom();
} catch (IOException e) {
// not an issue worth failing over
}
}

@Override
Expand Down

0 comments on commit 6566e9f

Please sign in to comment.