Writing an annotation processor for maven-processor-plugin - java

I am interested in writing an annotation processor for the maven-processor-plugin. I am relatively new to Maven.
Where in the project path should the processor Java source code go (e.g.: src/main/java/...) so that it gets compiled appropriately, but does not end up as part of my artifact JAR file?

The easiest way is to keep your annotation processor in a separate project that you include as dependency.
If that doesn't work for you, use this config
Compiler Plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
<inherited>true</inherited>
<executions>
<execution>
<id>default-compile</id>
<inherited>true</inherited>
<configuration>
<!-- limit first compilation run to processor -->
<includes>path/to/processor</includes>
</configuration>
</execution>
<execution>
<id>after-processing</id>
<phase>process-classes</phase>
<goals>
<goal>compile</goal>
</goals>
<inherited>false</inherited>
<configuration>
<excludes>path/to/processor</excludes>
</configuration>
</execution>
</executions>
</plugin>
Processor Plugin:
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>compile</phase>
<configuration>
<processors>
<processor>com.yourcompany.YourProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>
(Note that this must be executed between the two compile runs, so it is essential that you place this code in the pom.xml after the above maven-compiler-plugin configuration)
Jar Plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<excludes>path/to/processor</excludes>
</configuration>
<inherited>true</inherited>
</plugin>

Related

How to exclude from Maven plugin in pom.xml file, a particular dependency?

I have to exclude from Versions Maven Plugin command (use-latest-versions), a dependency that I don't want to automatically update.
Here is the doc:
https://www.mojohaus.org/versions-maven-plugin/use-latest-releases-mojo.html#excludes
This is how I have implemented the excludes optional parameter in the pom.xml file:
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>use-latest-versions</goal>
</goals>
<configuration>
<excludes>["org.mongodb:mongodb-driver-sync:jar:4.2.3"]</excludes>
</configuration>
</execution>
</executions>
</plugin>
...
Anyway during maven validate, the mongodb dependency is updated, ignoring the excludes parameters. Where is the mistake?
EDIT:
Now it works. Below the correct configuration:
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>use-latest-versions</goal>
</goals>
<configuration>
<excludes>
<exclude>org.mongodb:mongodb-driver-sync:jar:4.2.3</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
...

Kotlin 1.5.0 in Maven Multi-Module: java.lang.UnsupportedClassVersionError

I have a Multi-module Maven project. I had to import a maven module Vader (which uses a mix of Kotlin 1.5.0 and Java 11) and use it as a dependency for one of my modules which purely uses java 11. The code compiles well but at runtime I get this error:
java.lang.UnsupportedClassVersionError: org/revcloud/vader/matchers/AnyMatchers has been compiled by a more recent version of the Java Runtime (class file version 59.0), this version of the Java Runtime only recognizes class file versions up to 55.0
The file for which I get this error is a .kt file in my dependency module. I don’t get this error for java files. On the dependency module (vader) I made sure jvmTarget is set to java 11 and jdkHome points to java 11. I even confirmed by running javap -verbose AnyMatchers.class | grep “major” which gives me major version: 55
I am clueless as to what am I missing, where is this 59.0 coming from? Even my system only has JDK 11 and JDK 15 is not installed.
PFB the <build> section of pom.xml for dependency project(vader). (This has lombok too. So I delombok first into target/delombok and kotlin-maven-plugin picks up sources from this place. However none of the lombok annotations are used in Kotlin classes)
<build>
<sourceDirectory>${project.build.directory}/delombok</sourceDirectory>
<testSourceDirectory>${project.build.directory}/delombok-test</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<configuration>
<jvmTarget>${kotlin.compiler.jvmTarget}</jvmTarget>
<jdkHome>${kotlin.compiler.jdkHome}</jdkHome>
</configuration>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.build.directory}/delombok</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>${project.build.directory}/delombok-test</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>${lombok.version}.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>delombok</id>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
<configuration>
<sourceDirectory>src/main/java</sourceDirectory>
<outputDirectory>${project.build.directory}/delombok</outputDirectory>
<addOutputDirectory>false</addOutputDirectory>
</configuration>
</execution>
<execution>
<id>delombok-test</id>
<phase>generate-test-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
<configuration>
<sourceDirectory>src/test/java</sourceDirectory>
<outputDirectory>${project.build.directory}/delombok-test</outputDirectory>
<addOutputDirectory>false</addOutputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${kotlin.compiler.jvmTarget}</source>
<target>${kotlin.compiler.jvmTarget}</target>
</configuration>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>
Figured it out, had to change the Target JVM version for the Kotlin Compiler in Intellij

Java Log4j2 with pax logging not creating the Log4j2Plugins.dat file in maven project

I want to implement a custom plugin with Log4j2 with ops4j-pax-logging but the Log4j2Plugins.dat is not getting created. When I use just Log4j2 it gets created with no issue.
This was fixed by adding the plugin-processor in the pom.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>log4j-plugin-processor</id>
<goals>
<goal>compile</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<proc>only</proc>
<annotationProcessors>
<annotationProcessor>org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
</execution>
</executions>
</plugin>

How to execute a plugin explicitly after a certain phase in maven?

I'm using javafx-maven plugin to create a javafx webstart application. I had some issues signing the jar files with the javafx-maven plugin. what I want to do is, package(jar) the application with javafx-maven plugin and then sign the jar files using maven-jarsigner-plugin .
How do i execute the maven-jarsigner-plugin to sign my files after the application is packaged?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<archiveDirectory>target/jfx/app/</archiveDirectory>
<includes>
<include>**/*.jar</include>
</includes>
<keystore>path tp keystore</keystore>
<alias>alias</alias>
<storepass>password</storepass>
<keypass>password</keypass>
</configuration>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.3.0</version>
<configuration>
<bundler>jnlp</bundler>
<mainClass>com.myorg.myapp.launcher.myappLauncher</mainClass>
<bundleArguments>
<jnlp.allPermisions>true</jnlp.allPermisions>
<jnlp.includeDT>true</jnlp.includeDT>
<jnlp.outfile>myapp</jnlp.outfile>
</bundleArguments>
</configuration>
</plugin>
To workaround this I moved signing to verify phase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>signing</id>
<goals>
<goal>sign</goal>
<goal>verify</goal>
</goals>
<phase>verify</phase>
<inherited>true</inherited>
<configuration>
...
</configuration>
</execution>
</executions>
</plugin>
Then I invoke maven like this:
mvn verify
Or make verify your default goal
Alternatively you can move javafx-maven plugin to the "prepare-package" phase:
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.1.0</version>
<configuration>
<bundler>jnlp</bundler>
<mainClass>com.myorg.myapp.launcher.myappLauncher</mainClass>
<bundleArguments>
<jnlp.allPermisions>true</jnlp.allPermisions>
<jnlp.includeDT>true</jnlp.includeDT>
<jnlp.outfile>myapp</jnlp.outfile>
</bundleArguments>
</configuration>
<executions>
<execution>
<id>create-jfxjar</id>
<phase>prepare-package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>

Place all libraries and files for a project in one folder

I use maven in my java build process. The following is a snippet of code that creates an single jar with all dependencies. In order to reduce the data transfer on small changes to the build I'd like to place all project files (including dependencies) in the folder target/build . I plan to rsync the folder with the remote machine running the app and run the app with:
java -cp target/build/* <classname>
How do I modify this snippet to achieve this? I've read the documentation here but don't know how to piece the fix together:
http://maven.apache.org/plugins/maven-assembly-plugin/
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
http://maven.apache.org/plugins/maven-assembly-plugin/descriptor-refs.html
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Are you asking how to get maven to copy your dependencies to the target folder when you build?
I think you want the maven dependency plugin. It copies the dependencies of your project to an output folder you specify.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${targetDirectory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
It sounds like you may also need to maven jar plugin to tell it where to package your jar to.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>${dir}</outputDirectory>
</configuration>
</plugin>
Use the maven dependency plugin
It has the gole: copy-dependencies. This should do what you want.
Example (take from the documentation)
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>

Categories

Resources