How to process annotations with Maven? - java

I'm trying to process annotations with my own annotations processor in a maven project. Using org.bsc.maven:maven-processor-plugin I had some success, but many problems.
I wonder if there is a demo project comparable to my usage scenario I could compare to.

Surprisingly all what was needed is adding a dependency on the project with the annotation processor.
dp4jmaventest is a sample project for dp4j annotations processor.
My problem was that of trying to process annotations in classes within the same annotation processing project. The solution is to compile on two phases.
</configuration>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
<includes>
<include>com/dp4j/**</include>
<include>com/dp4j/processors/core/**</include>
<include>com/dp4j/processors/**</include>
</includes>
</configuration>
</execution>
<execution>
<id>compile-everything-else</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<!--http://cdivilly.wordpress.com/2010/03/16/maven-and-jsr-269-annotation-processors/-->
dp4j maven project is a working example project.

Related

flyway-maven-plugin: how to execute multiple database configurations together?

I am integrating Flyway in to an existing legacy project, which consists of multiple databases for the same application. The project uses Maven and I want to use the maven-flyway-plugin to integrate with Flyway.
My working configuration so far looks like this:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<executions>
<execution>
<id>db1</id>
<goals>
<goal>migrate</goal>
</goals>
<configuration>
...
</configuration>
</execution>
<execution>
<id>db2</id>
<goals>
<goal>migrate</goal>
</goals>
<configuration>
...
</configuration>
</execution>
<execution>
<id>db2</id>
<goals>
<goal>migrate</goal>
</goals>
<configuration>
...
</configuration>
</execution>
</executions>
</plugin>
With this I can migrate each database separately like this:
mvn flyway:migrate#db1 flyway:migrate#db2 flyway:migrate#db3
Unfortunately this is not very user friendly. I want to be able to simply execute mvn flyway:migrate and execute all three migration configurations.
How can I achieve that?
maven works this way. You can either extends flyway plugin with your needs or you can migrate to gradle which can gather multiple 'job'
Eric

Maven won't recognize certain Kotlin classes in Java project

I have a Java project that incorporates some classes in Kotlin code. There are actually two Kotlin files, each in different folders and each a different package.
When doing a mvn clean package, Kotlin file A is recognized and compiled into the project, but Kotlin file B is not.
Strangely, B was compiled in earlier, but I converted some Groovy files to Java to avoid Babel. Those new Java files reference the classes in B, and it is their compilation that is producing the errors.
I've checked and double-checked package names. To see if B's folder was being noticed by Maven, I even converted one of the data classes in B to Java and left it in the same folder. Suddenly that class was recognized.
What's going on here? Do I have a POM problem?
I needed to add the following to my POM under the section for the Kotlin plug in:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<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>
This was according to the the documentation here.

How to fail maven build if annotation used?

I want to forbid usage of some annotations in a project. Specially #SneakyThrows annotation from lombok.
One way to do this is to fail while building maven artifact.
Are there some existing plugins to reach this?
I think it is possible to achive using static code analyzers like checkstyle. But I don't want to write new rule for it.
Also it is interesting if exists such plugins for gradle?
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<basedir>${basedir}</basedir>
<includes>
<include>target/**/*.java</include>
</includes>
<regex>false</regex>
<replacements>
<replacement>
<token>#SneakyThrows</token>
<value>#### I-DONT-THINK-SO ####</value>
</replacement>
</replacements>
</configuration>
</plugin>

How to do integration tests which do not ends with **/**IT

I have test class which ends with IT i.e SampleClassIT.java.
I have added this class in testng.xml file and used maven-failsafe plugin in pom.xml file.
Plugin used :-
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Is there any way ,so that all the classes which are not extended with IT can also run as a part of integration tests because i have more that 100 classes that i need to refactor them.I dont need to extend my classes with **/*IT.java and make them run as integration tests using failsafe plugin.
You can explicitly specify what classes to include by
<configuration>
<includes>
<include>**some_pattern*.java</include>
</includes>
</configuration>
make your class at-least to satisfy one pattern

Generating sources by running a project's java class in Maven

I'm converting a largish Ant build to Maven. As part of the Ant build, we have several steps which created Java classes by invoking one of the project's classes, simplified as:
javac SomeGenerator.java
java SomeGenerator generated # generate classes in generated/
javac generated/*.java
I've split each generator in its own Maven module, but I have the problem of not being able to run the generator since it's not yet compiled in the generate-sources phase.
I've tried something similar to
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>generate-model</id>
<goals>
<goal>java</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<mainClass>DTOGenerator</mainClass>
<arguments>
<argument>${model.generated.dir}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
Which sadly does not work, for the reasons outlined above. Splitting the code generators into two projects each, one for compiling the generator and another for generating the DTOs seems overkill.
What alternatives are there?
Using Maven 2.2.1.
You can execute the maven-compile-plugin in the generate-sources phase. Just add another execution before the existing execution and configure it so that it just picks up the sources for the generator.
Or split the project in two: build the generator with a separate POM and include the generator library as a dependency to the POM that's generating the sources.
Personally I would split the project. Keeps the build files cleaner and easier to maintain.
I didn't want to have 2 different projects, so I tried to setup Maven for adding the generated compiled code to the final jar package.
This is the working solution I've used:
In process-classes phase (executed just after the compile phase):
exec-maven-plugin for executing a main class able to generate my source files in target/generated-sources/java folder (in my specific case I used the Roaster library for source code generation);
build-helper-maven-plugin for adding the generated sources in the correct location
In prepare-package phase:
maven-compiler-plugin, in order to detect the changes and recompile the module
maven-jar-plugin for producing the jar package
This is my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.example.MyClassWriter</mainClass>
<arguments>
<argument>${project.basedir}</argument>
<argument>${project.build.directory}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>prepare-package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
In order to do this in one project, there are 3 steps:
Compile generator code
We can do it in generate-sources phase, using maven-compiler-plugin. You can also exclude other source files.
Run generator to generate code
We can do it in process-sources phase, using exec-maven-plugin.
Compile project
Below is the key part of pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<id>compile-generator</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<includes>
<include>source/file/of/generator/*.java</include>
</includes>
<excludes>
<exclude>other/source/files/*.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>generate-codes</id>
<goals>
<goal>java</goal>
</goals>
<phase>process-sources</phase>
<configuration>
<mainClass>your.main.class.of.generator</mainClass>
</configuration>
</execution>
</executions>
</plugin>
We faced the same problem. We wanted to respect Maven's behavior as closely as possible, to have no problems with plugins and so on... Fighting Maven is just too expensive!
We realized that the update frequency of the generated code was usually very different from the one of the code that we manually write, so separating the code had very good performance characteristics for the build. So we accepted to have our generated classes as a dependency of the manually written.
We adopted the following structure, that had just one little change from a regular maven config, a change in the source directory.
Parent project : Generations
We created a parent project for all our generations.
It has a JAR type if it contains code to be compiled, otherwise POM.
There we have our generating code, in /src.
It can compile in /target as usual.
It runs the generation, each generator producing code in a sub-directory of /target.
Note: if you want several generated results in the same jar, just put them in the same sub-directory.
Child jar projects : Generateds
It is a subdirectory of the Generations project.
It has a JAR type.
The source directory points to the sub-directory in the parent's target.
<sourceDirectory>../target/generated1</sourceDirectory>
It compiles normally in its own /target directory.
That structure allows us to :
have as little modification to the standard maven layout as possible, so every maven command and plugin keeps working nicely.
scale nicely if you have several generators,
scale nicely if you want to generate several jars (we had a case wsdl2java where one generator produced code that should be split into several jars ; each child generated project would have the same source directory, but would be configured with an <includes> to handle only some of the classes).
I posted a minimal working setup here https://github.com/baloise/inlinesourcecodegenerator
It uses build-helper compiler and exec plugins and has all code in the same project.

Categories