Use annotation processor with maven-compile-plugin - java

I have an idea to include an immutable (performs compile-time code generation of POJO) library into a legacy project. The main issue: the legacy project is used widely in other applications, so I do not want to have any new dependency at all. I found that question where it said it is possible to use tag annotationProcessorPaths for that purpose. I try to rework my config that way:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>2.9.2</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
it does not work. I get an error: Could not find artifact org.immutables:value:jar:unknown in case I add the library in dependency section - it works. As figure out later, looks like annotationProcessorPaths tag is just order of using annotation processors, but does not actually provide dependencies for them. Is it correct understanding? Is there any work around to use that annotation process without including it in dependency section at all?

Related

How to use annotation processors like lombok while compiling an annotation processor

How do I make maven use lombok before it compiles my annotation processor? I am currently going crazy over the infinite amounts of options I have for maven, which all boil down to my Test-Class not using my annotation processor.
My Project has an annotation processor sub-project and a test-sub-project, which uses the annotation processor.
The Pom of my AP currently looks like that (only the build part):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<!--<compilerArgs>-proc:none</compilerArgs>-->
<annotationProcessors>
<annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor>
<annotationProcessor>lombok.launch.AnnotationProcessorHider$ClaimingProcessor</annotationProcessor>
</annotationProcessors>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok-version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
Nothing really works here. When I trigger a full rebuild, I get the message
package de.derteufelqwe.AutoPlugin does not exist which is the class that should have been generated.
When I reducy my pom to this
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<compilerArgs>-proc:none</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
all files are properly generated and everything seems to work fine.
But when I try to export my project using mavens 'package' method, I get the error
java: constructor Cache in class de.derteufelqwe.AutoPluginProcessor.cache.Cache cannot be applied to given types on the line new Cache(). The Cache class gets it's constructor from lombok, therefore I suspect that lombok didn't do it's lombok things before maven tried to compile it.
Tl;Dr: How do I make the compiler use lombok before compiling in my IDE and in maven.

Share common configuration between different plugins in the same Maven module

I'm working on a Maven project which uses both the Surefire and Failsafe plugins in the same module. The configurations for both plugins are pretty much identical, except for one element (classpathDependencyExcludes), as you can see.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<skipTests>${skipTests}</skipTests>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<argLine>-javaagent:"${project.build.directory}/openejb-javaagent-${tomee.version}.jar"</argLine>
<workingDirectory>${project.build.directory}</workingDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<skipTests>${skipTests}</skipTests>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<argLine>-javaagent:"${project.build.directory}/openejb-javaagent-${tomee.version}.jar"</argLine>
<workingDirectory>${project.build.directory}</workingDirectory>
<classpathDependencyExcludes>
<classpathDependencyExclude>javax:javaee-api</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
I was wondering if there was a way to share the common section of the configuration between the two plugins by writing it down once instead of multiple times. If not, in the hypothesis that both configurations were identical, would it be possible?
First of all, you don't need to set all the properties explicitly. skipTests has already the value of ${skipTests}, no need to repeat that. forkCount has already the default value 1.
Some of the other properties can be set in the <properties> section, like reuseForks and argLine.
Then there is very little left to worry about.

Different ways to override java version specified in parent pom

I'm using a parent pom which establishes java version to 1.5. In my concrete project I use 1.6 so I was changing the compiler version in eclipse each time I did a Maven Update.
Looking for a solution to this I found some solutions involving overriding the behavior of the parent pom in the child one.
My question is if there are any differences between them and if so, which option should I use. The options I found (perhaps there are more) are:
In properties tag: <app.java.version>1.6</app.java.version>
In properties tag: <jdk.version>1.6</jdk.version>
In configuration tag: <source>${jdk.version}</source>
I'm very new to Maven. Thanks in advance.
You definitely want to go with:
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
It is the de facto standard Maven property for setting up Java version and it's used not only by maven-compiler-plugin but also by other standard Maven plugins (including reporting ones), so this applies your Java version globally, not only for compiling classes.
Properties are just properties, which do not mean much unless you use them somewhere.
The important thing is that you set the version in the maven-compiler-pluginconfiguration:
<properties>
<jdk.version>1.6</jdk.version>
</properties>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
If your parent pom uses 1.7 jdk and you want child pom to use 1.6 java version. combine.self="override" is important use following code:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration combine.self="override">
<verbose>true</verbose>
<fork>true</fork>
<executable>/usr/lib/jvm/java-1.6.0-openjdk-amd64/bin/javac</executable>
<compilerVersion>1.6</compilerVersion>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

How to create a new classes for each compilation using maven plugin

In case of my project I need to create new classes after each compilation. For compilation I'm using maven compiler plugin 3.1. I tried to use compilerReuseStrategy = alwaysNew option but it didn't make any affect, it always compile only changed classes. Here is plugin declaration in pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerReuseStrategy>alwaysNew</compilerReuseStrategy>
</configuration>
</plugin>
An I doing something wrong or that's a bug and this option really doesn't work?
If you are talking about the incremental feature fo the maven-compiler-plugin you can change this behaviour by the following configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
The compileReuseStrategy in contradiction is intended to define the behaviour in relationship with multi-threaded running of the compiler.
What about using mvn clean install?

How do i force the cxf-codegen-plugin (CXF wsdltojava) Maven plugin to generate for Javaee 5?

whilst using Maven and the cxf-codegen-plugin from apache it seems to generate code for Java ee 6. I can tell by the error message ../generated-sources/cxf/.../cxf/gen/prod/IProd.java: cannot find symbol
symbol : class Action
location: interface ....IProd
Action is only in jee6++
How can i force it to generate for Javaee 5? Are there any flags - cant find any.
Everything is set to Java 1.5 on computer,Java_Home and java -version gives 1.5.. i have the java EE 5 api on compilepath. The plugin in maven is set to 1.5.
Still...
EDIT: Solved, see comments.
I generate my JAX-WS classes with another plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<target>2.0</target>
<packageName>some.pack.age</packageName> <!-- The name of your generated source package -->
</configuration>
<!--
if you want to use a specific version of JAX-WS, you can do so like
this
-->
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-tools</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
</plugin>
You should add your wsdl's files in
/src/wsdl/
You need to specify the java version in you maven compiler plugin like this:
(and include the "bootclasspath" if it still generates wrong code)
<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>
<encoding>UTF-8</encoding>
<bootclasspath>${java.home}\lib\rt.jar</bootclasspath>
</configuration>
</plugin>

Categories

Resources