Annotation processor output in maven - java

I'm using JSR 269 as a way to analyze code during compilation and to fail it if needed.
I'm having troubles with displaying output of my annotation processor in maven (Ant does show the output)
I'm using javax.annotation.processing.Messager to display warnings and errors, but in maven I don't see it's output. (I do know it runs though, because it generates code like it should).
Any ideas?

I think you are running into a Maven bug or better a bug in the compiler plugin - MCOMPILER-66. When it comes to annotation processing the compiler plugin has several problems, eg also MCOMPILER-62. Really the best option imo is to disable annotation processing for the compiler plugin and use the maven-processor-plugin. In this blog post you can see how to use it. It looks like this:
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>1.3.7</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>1.1.0.Final</version>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>
Note also how the annotation processor dependency is nicely scoped to the plugin only.

You can do this by enabling showWarnings flag in the maven-compiler-plugin configuration:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>
See also https://github.com/Cosium/annotation-processor-logger#enable-all-logging-levels

Related

how to apply semantic versioning to the java maven project to automatically increment the version inside pom.xml

Can anyone please tell me how to apply the semver to the java maven project? I tried many ways, but I didn't find any useful resources to automatically increase the version when I push the code to the branch. I'm using Github action workflow to deploy the project into GitHub.
Thank you.
My first approach is to use the command line but you have to configuration the following in your pom file before. You can of course directly use the command line and put everything on the plain command without this setup but it's very inconvenient
<pluginManagement>
<plugins>
<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-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.9.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<executions>
<execution>
<id>major</id>
<goals>
<goal>set</goal>
</goals>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
<newVersion>${parsedVersion.nextMajorVersion}.0.0-SNAPSHOT</newVersion>
</configuration>
</execution>
<execution>
<id>minor</id>
<goals>
<goal>set</goal>
</goals>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
<newVersion>${parsedVersion.majorVersion}.${parsedVersion.nextMinorVersion}.0-SNAPSHOT</newVersion>
</configuration>
</execution>
<execution>
<id>patch</id>
<goals>
<goal>set</goal>
</goals>
<configuration>
<generateBackupPoms>false</generateBackupPoms>
<newVersion>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion}-SNAPSHOT</newVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>parse-version</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
By using the above configuration you can change/update the version of your project like this:
mvn build-helper:parse-version versions:set#major
This will increment the major version and set minor and patch version to 0.
mvn build-helper:parse-version versions:set#minor
This will increment the minor version and set patch version to zero.
mvn build-helper:parse-version versions:set#patch
this will increment the patch version. Afterwards you have to commit your changed back into your version control system (for example git).
I recommend to define this kind of setup into a parent pom and reuse it for multiple projects. A detail explanation why and how this works can be found here https://blog.soebes.de/blog/2021/04/05/maven-plugin-configuration/
Using the maven-release-plugin is also an option. It will make also the tags in your version control.

AspectJ weaving from dependency not applying to project

I have a Java 8 Maven project that defines a custom annotation and an aspect. When running test code in that project itself, it is applying the aspect to the annotated classes. I am then packaging and installing project.
I then bring in that dependency into a new project (non-Spring). The new project is then not having the aspect applied to it's classes, though it does bring in the new annotation.
How do I have a single JAR to define an annotation and aspect and have it applied to all of my projects with Maven?
You need to specify your aspect project dependency as an aspect library in your aspectj-maven-plugin configuration in your pom.xml. Let's suppose your aspect module has the groupid:artifactid groupid:aspect-module. Your pom.xml should look similar to this:
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>groupid</groupId>
<artifactId>aspect-module</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.9</version>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>groupid</groupId>
<artifactId>aspect-module</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note that I'm switching off the maven-compiler-plugin because they tend do overwrite each other's output with the aspectj-maven-plugin, and the AspectJ compiler should be able to compile normal java files and weave them in the same step anyway, so using the maven-compiler-plugin is redundant. If you are using Eclipse + AJDT, this maven configuration will much better reflect what happens in your IDE while you're developing.

How can I run both xml config driven and xml-less Spring webapps together with Maven?

I have a couple of webapps I'm trying to run up together using Maven tomcat7 plugin.
I have the first project which is an XML driven Spring application and another application I'm trying to run alongside it which is fetched as a war from our Maven repository.
When I enable (uncomment) the ... block the way attempts to start but I get errors about a bean within the project (that this pom belongs to) not being autowired in.
I suspect somehow the configs are confusing each other... is that possible when running like this?
Ultimately I think I'm going to end up running them as separate apps in different IDE windows, but would rather have them run together, by running 1 tomcat7:run target, from a single version control project.
ie
localhost:8090/MainApp
localhost:8090/Stubs
I hope that makes sense!
<build>
<finalName>MainProjectApplication</finalName>
<!-- this project is XML driven Spring 3 -->
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8090</port>
<path>/MainApp</path>
<contextReloadable>true</contextReloadable>
<contextFile>${project.basedir}/environment/localhost/context/mainApp.xml</contextFile>
<webapps>
<webapp>
<!-- this is Spring4 xml-less config, not even web.xml -->
<contextPath>/stubs</contextPath>
<groupId>uk.my.package</groupId>
<artifactId>included-project</artifactId>
<version>1.1-SNAPSHOT</version>
<type>war</type>
<asWebapp>true</asWebapp>
</webapp>
</webapps>
</configuration>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<classifier>localhost-config</classifier>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptors>
<descriptor>src/assembly/localhost-config.xml</descriptor>
</descriptors>
<classifier>localhost-config</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${maven-enforcer-plugin.version}</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>[3.0.0,)</version>
</requireMavenVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
Turns out the componant scanning was setup to scan the same package names.
ie. uk.me.organisation
I changed in respective config to:
uk.me.organisation.mainapp and uk.me.organisation.stubs
Now there is no overlap they seem to play nicely together.

Maven doesn't weave aspectj code

I'm facing problems to build an aspect project in eclipse with maven. When I run maven through eclipse "Run As > Maven build" I obtain this message: <...>/Clazz.java:[5,32] error: cannot find symbol. So, it looks like aspectj is not weaving the code through maven.
I distilled the problem until have class and an aspect that defines an intertype attribute in the mentioned class, as follows:
public class Clazz {
public static void main(String[] args) {
System.out.println(new Clazz().string);
}
}
public aspect Aspect {
public String Clazz.string = "string";
}
The pom.xml looks like this:
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.3</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The problem appears to be that the maven-compiler-plugin doesn't know to get out of the way when you have an AspectJ compile and throws errors that kill the build before ajc gets a chance to pull in the ITDs. My solution has been to disable maven-compiler-plugin entirely and let ajc handle compiling the .java files:
<!-- disable compiler because compiler chokes on ITDs -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
Actually you do not need to deactivate the Maven Compiler Plugin, but you need to do two things according to what I found out for someone who had a similar problem here:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<aspectj.version>1.8.1</aspectj.version>
</properties>
<!-- (...) -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<!-- IMPORTANT -->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>1.7</source>
<target>1.7</target>
<Xlint>ignore</Xlint>
<complianceLevel>1.7</complianceLevel>
<encoding>UTF-8</encoding>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<!-- IMPORTANT -->
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
I.e. you need to
use incremental compilation in Maven Compiler Plugin 3.1 (attention, the switch is reversed, which is probably a bug) and
assign execution phase "process-sources" to AspectJ Maven Plugin 1.6.
That should do it.

How to configure Maven to build two versions of an artifact, each one for a different target JRE

I have a maven module that I need to use in the J2ME client and in the EJB server. In the client I need to compile it for target 1.1 and in the server for target 1.6 .
I also need to deploy the 1.6 version to a Nexus repository, so the members working on the server project can include this dependency without needing to download the source code.
I've read at http://java.dzone.com/articles/maven-profile-best-practices that using profiles is not the best way of doing this, but the author didn't say what's the best way.
Here is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>proj-parent</artifactId>
<groupId>br.com.comp.proj</groupId>
<version>0.0.4-SNAPSHOT</version>
</parent>
<artifactId>proj-cryptolib</artifactId>
<name>proj - Cryto Lib</name>
<dependencies>
<dependency>
<groupId>br.com.comp</groupId>
<artifactId>comp-proj-mobile-messages</artifactId>
<version>0.0.2-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.3</source>
<target>1.1</target>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
As Haylem suggests thought you'll need to do it in two steps, one for the compile and one for the jars.
For the compiler
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<configuration>
<source>1.3</source>
<target>1.5</target>
<fork>true</fork>
<outputDirectory>${project.build.outputDirectory}_jdk5</outputDirectory>
</configuration>
</execution>
<execution>
<configuration>
<source>1.3</source>
<target>1.6</target>
<fork>true</fork>
<outputDirectory>${project.build.outputDirectory}_jdk6</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
And then for the jar plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${project.build.outputDirectory}_jdk5</classesDirectory>
<classifier>jdk5</classifier>
</configuration>
</execution>
<execution>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${project.build.outputDirectory}_jdk6</classesDirectory>
<classifier>jdk6</classifier>
</configuration>
</execution>
</executions>
</plugin>
you can then refer to the required jar by adding a <classifier> element to your dependency. e.g.
<dependency>
<groupId>br.com.comp.proj</groupId>
<artifactId>proj-cryptolib</artifactId>
<version>0.0.4-SNAPSHOT</version>
<classifier>jdk5</classifier>
</dependency>
You can configure this via the Maven compiler plugin.
Take a look at the Maven compiler plugin documentation.
You could enable this via different profiles for instance.
If you only want to have different target versions you could simply use a variable target. Something like this:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.3</source>
<target>${TARGET_VERSION}</target>
<fork>true</fork>
</configuration>
</plugin>
To complement my comment to wjans' answer, as you requested more details.
The following would have the compiler plugin executed twice to produce two different sets of classfiles, identified by what is called a classifier (basically, a marker for Maven to know what you refer to when a single project can produce multiple artifacts).
Roughly, something like:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<configuration>
<source>1.3</source>
<target>1.5</target>
<fork>true</fork>
<classifier>jdk5</classifier>
</configuration>
</execution>
<execution>
<configuration>
<source>1.3</source>
<target>1.6</target>
<fork>true</fork>
<classifier>jdk6</classifier>
</configuration>
</execution>
</executions>
</plugin>
Note that people sometimes frown on using classifiers, as they on using profiles, as they can possibly mean that your project should be scinded in multiple projects or that you are harming your build's portability.

Categories

Resources