Generate code from java and scala generator and compile them in Maven build - grant-guo/Ideas GitHub Wiki
Three Maven plugins help accomplish this:
org.codehaus.mojo/exec-maven-plugin
is used to run Java and Scala executables
org.apache.maven.plugins/maven-compiler-plugin
is used to compile the Java generator sources
net.alchim31.maven/scala-maven-plugin
is used to compile the Scala generator sources
The basic ideas are
- at
generate-sources
phase, runcompile
goal to compile the Java/Scala generators, also need to include all sources on which the generators depend - run Java/Scala generators at
process-sources
phase to generate the sources - run
add-sources
goal to add the directories where the sources are generated into the compiler path
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>generate-codes-java</id>
<goals>
<goal>exec</goal>
</goals>
<phase>process-sources</phase>
<configuration>
<executable>${env.JAVA_HOME}/bin/java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>${generator class full name}</argument>
</arguments>
<addResourcesToClasspath>true</addResourcesToClasspath>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>generate-codes-scala</id>
<goals>
<goal>exec</goal>
</goals>
<phase>process-sources</phase>
<configuration>
<executable>${env.JAVA_HOME}/bin/java</executable>
<arguments>
<argument>${if any arguments}</argument>
<argument>-classpath</argument>
<classpath/>
<argument>${scala generator class full name}</argument>
</arguments>
<addResourcesToClasspath>true</addResourcesToClasspath>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<executions>
<execution>
<id>compile-java-generator</id>
<phase>generate-sources</phase>
<goals><goal>compile</goal></goals>
<configuration>
<includes>
<include>${dependencies of generator class, }</include>
<include>com/grant/generator/**</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.8.1</version>
<executions>
<execution>
<id>compile-scala-generator</id>
<phase>generate-sources</phase>
<goals><goal>compile</goal></goals>
<configuration>
<includes>
<include>com/grant/guo/generator/**</include>
</includes>
</configuration>
</execution>
<execution>
<id>add-generated-sources-java</id>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sourceDir>${project.basedir}/target/generated-sources-java</sourceDir>
</configuration>
</execution>
<execution>
<id>add-generated-sources</id>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sourceDir>${project.basedir}/target/generated-sources-scala</sourceDir>
</configuration>
</execution>
</executions>
<configuration>
<args>
<arg>-unchecked</arg>
<arg>-deprecation</arg>
<arg>-explaintypes</arg>
</args>
<jvmArgs>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
<recompileMode>incremental</recompileMode>
</configuration>
</plugin>