Maven project with JAXB2 plugin does not work without maven clean - java

My Maven project with the JAXB2 plugin works without errors if I run
mvn clean install
but it always fails if I skip the clean and run
mvn install
In this case the generated classes are not generated again which is correct:
[INFO] No changes detected in schema or binding files - skipping JAXB generation.
But then I get an compilation error that the generated classes and packages can not be found when the rest of the static Java sources in this Maven project are compiled:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project [...]: Compilation failure: Compilation failure:
[ERROR] [...] package [...] does not exist
[ERROR] [... ]cannot find symbol
Here is the relevant part of my pom.xml (the rest is only dependencies):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>xjc-core</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/main/xsd/core</source>
</sources>
<packageName>com.example.core</packageName>
<clearOutputDir>false</clearOutputDir>
</configuration>
</execution>
<!-- ... and more <execution> -->
</executions>
Am I correct that the only solution is to separate the static sources and generated sources into different Maven modules? Or is there any other way?

There is a bug in jaxb2-maven-plugin v2.2 https://github.com/mojohaus/jaxb2-maven-plugin/issues/35.
This bug has been fixed in v2.3

Related

How to build a module separately in multimodular maven project?

Following maven-ci-friendly article in official Maven documentation, this multimodule project (minimal example) was created.
There are three modules and a root project (inception):
/inception
/modules
/base (common parent of 'core' and 'facade')
/core (child of 'base')
/facade (child of 'base' having 'core' as a dependency)
Executing mvn package from inception works as expected - all 3 *.jar artifacts are being created in the corresponding target forlders.
I would like to have an option of building facade module separately.
Unfortunately, mvn package from modules/facade fails to collect dependencies and fails with error
[ERROR] Failed to execute goal on project sample-facade:
Could not resolve dependencies for project sample.group:sample-facade:jar:0.0.1:
Failed to collect dependencies at sample.group:sample-core:jar:0.0.1:
Failed to read artifact descriptor for sample.group:sample-core:jar:0.0.1:
Could not transfer artifact sample.group:sample-base:pom:${revision}
The surface problem is that ${revision} is not being resolved into 0.0.1.
Could you help me workaround this issue?
The flatten-maven-plugin solves the problem.
Thanks to #khmarbaise, who adviced in the comments reading the docs to the end.
Adding the plugin to /modules/base/pom.xml solved the problem with building a submodule separately:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>1.2.5</version>
<configuration>
<updatePomFile>true</updatePomFile>
<flattenMode>resolveCiFriendliesOnly</flattenMode>
</configuration>
<executions>
<execution>
<id>flatten</id>
<phase>process-resources</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
<execution>
<id>flatten.clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
Before starting a phase on facade module, it is required to have base and core in the local repository, so maven could find the artifacts. Hence, here is the sequence of actions in root:
mvn install -pl modules/base,modules/core (or just mvn install)
mvn package -pl modules/facade

Maven doesn't find imported class from another module but Intellij does

I have multi-module maven project. The acceptance-tests module has dependency from api module in pom.xml (Replacing real company name by xxx to keep confidentiality). I am trying to import some classes from api module in my acceptance-tests.
Here is my pom.xml dependency of acceptance-tests module:
<dependency>
<artifactId>xxx-api</artifactId>
<groupId>com.xxx</groupId>
<version>${xxx.api.version}</version>
</dependency>
The api module separately is being installed and packaged (mvn install, mvn package) by maven without any issue. The jar file is being created in my local .m2.
However, when I try to compile the acceptance-tests module, I get a compilation error saying that the the classes cannot be imported because the package is not found.
Here is the actual error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project xxx-acceptance-tests: Compilation failure: Compilation failure:
[ERROR] /Users/andranik_chorokhyan/mydisk/Projects/XXX/automation/xxx-project-test-automation/xxx-acceptance-tests/src/main/java/com/xxx/xxx/utilities/api/ApiPayloadUtils.java:[17,38] package com.xxx.domain.dto does not exist
[ERROR] /Users/andranik_chorokhyan/mydisk/Projects/XXX/automation/xxx-project-test-automation/xxx-acceptance-tests/src/main/java/com/xxx/xxx/utilities/api/ApiPayloadUtils.java:[18,38] package com.xxx.domain.dto does not exist
[ERROR] symbol: class MappingData
[ERROR] location: class com.xxx.utilities.api.ApiPayloadUtils
One more interesting fact is that there is no error visible in Intellij IDEA. No red underline, no compilation error, no problem with navigating to the appropriate imported file.
And in reality, the com.xxx.domain.dto package does exist and the MappingData class as well.
I removed whole xxx directory from my local .m2 repository and executed mvn clean dependency:resolve command. It succeeded as well.
Does anybody know what's the problem here and how it can be solved?
Thanks in advance!
Finally I have found the solution. Thanks JF Meier and khmarbaise for hints.
It appeared Maven doesn't allow dependency from executable jar. This was my case. My api module was an executable Spring Boot application and not reusable library.
So, the solution was the following:
It was necessary to find the Application.java file in api module.
Add maven-jar-plugin with exclusion of the Application.java file and specification of some classifier
Making dependency in acceptance-tests module from the above specified classifier instead of standard jar
Plugin specification in api module below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>qa</classifier>
<excludes>
<exclude>**/Application*</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
Dependency in acceptance-tests module below:
<dependency>
<artifactId>xxx-api</artifactId>
<groupId>com.xxx</groupId>
<version>${api.version}</version>
<classifier>qa</classifier>
</dependency>
I was also getting symbol not found errors while compiling with maven, and the solution is for spring boot 2 you need to configure plugin as below, classifier exec
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
If you are working with spring boot 1
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>

GWT compilation error with Maven: No instance of Presenter is defined. Forget to annotate #Presenter or #EventHandler?

Migrate from Ant Ivy to Maven build tool.
Little info about project:
Multi-module project with few deploying artifacts.
On Ant Ivy everything fine, compilation and deploy passes normal.
Migrate on Maven pass successfully with 2.7.0 GWT version. After increasing version to 2.8.1, stared to get gwt compilation error.
[ERROR]com.example.gwt.client.module.user.GWTEventBus:
Method createInfoPage: No instance of com.example.gwt.client.module.user.presenter.
info.InfoPresenter is defined. Have you forgotten to annotate your event handler
with #Presenter or #EventHandler?
[INFO] at com.mvp4g.util.config.loader.annotation.EventsAnnotationsLoader.
buildPresentersAndEventHandlers (EventsAnnotationsLoader.java:425)
...long stack trace ...
at com.google.gwt.dev.Compiler.main(Compiler.java:125)
[INFO][ERROR] Errors in 'com.example.gwt.GWTModule'
[INFO][ERROR] Line 137: Failed to resolve 'com.mvp4g.client.Mvp4gModule' via deferred binding
Line 137 contain Mvp4gModule module = GWT.create(Mvp4gModule.class);
Attempts to fix:
1. Compared classpath mapped dependencies from Ant Ivy with Maven classpath, everything the same. All versions of dependencies and plugins are fine, without any conflicts.
2. Tried with two different gwt maven plugins, but get the same error with any configurations:
<plugin>
<groupId>net.ltgt.gwt.maven</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>1.0-rc-6</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>import-sources</goal>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<moduleName>com.example.gwt.GWTModule</moduleName>
<jvmArgs>
<jvmArg>-Xmx768M</jvmArg>
<jvmArg>-Xss10M</jvmArg>
</jvmArgs>
<failOnError>false</failOnError>
<sourceLevel>1.8</sourceLevel>
<compilerArgs>
<arg>-compileReport</arg>
<arg>-XcompilerMetrics</arg>
</compilerArgs>
<warDir>${project.build.directory}/${project.build.finalName}</warDir>
<classpathScope>compile+runtime</classpathScope>
<startupUrls>
<startupUrl>Validation.html</startupUrl>
</startupUrls>
</configuration>
</plugin>
Main strangeness is that Ant compile successfully. Ant's compile-gwt target has the same configuration and arguments as Maven's gwt-plugin.

Using property file in maven

I don't quite understand how it can be used. There is a property defined in the file. I try to use maven property plugin to read it and save. The property is used in the liquibase plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/properties/app.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>2.0.5</version>
<configuration>
<propertyFile>src/main/resources/db/config/${env}-data-access.properties</propertyFile>
<changeLogFile>src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>
<migrationSqlOutputFile>src/main/resources/db/gen/migrate.sql</migrationSqlOutputFile>
<!--<logging>debug</logging>-->
<logging>info</logging>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
<!--<verbose>false</verbose>-->
<dropFirst>true</dropFirst>
</configuration>
</plugin>
According to the documentation in order to read property and save it I have to run: mvn properties:read-project-properties. But I'm getting the following error in this case:
[ERROR] Failed to execute goal org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties (default-cli) on project SpringWebFlow:
The parameters 'files' for goal org.codehaus.mojo:properties-maven-plugin:1.0-alpha-2:read-project-properties are missing or invalid -> [Help 1]
I've changed pom.xml, removed the <execution> section and moved the <configuration> section:
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<configuration>
<files>
<file>src/main/resources/properties/app.properties</file>
</files>
</configuration>
Ok. now, when I run mvn properties:read-project-properties the error disappeared. But where in this case the property is saved? Cause when I start the following maven goal:
mvn liquibase:update
I can see that the ${env} property is not defined. Liquibase tries to use the src/main/resources/db/config/${env}-data-access.properties file.
What am I doing wrong? How to read a property from the file, so it could be accessible from different maven plugins?
The problem is that "mvn liquibase:update" is a special plugin goal and is not part of the maven life cycle. So it never passes the initialize phase and so the property plugin is not executed.
The following will work
mvn initialize liquibase:update
One solution would be to call liquibase:update in one of the maven lifecylce phases like compile, package ..., but then it would be executed on every build.
Or you use the maven-exec plugin to call "initialize liquibase:update" from maven. Or you create a profile were you bind the liquibase:update to the lifecylce phase initialize and the udate is executed when you call
mvn initialize -Pliquibase
I do not know a better solution to this problem and I could not find a suitable solution for this.
For reference:
Maven lifecycle

Adding Grails command to Maven phases -- what am I doing wrong?

I need to build my Grails project with Maven, and it is necessary to add an additional grails command. I'm using the grails-maven-plugin to create the pom file, and I can build the war file with $ mvn package
While building this application, I will need to execute another grails command, one the does not correspond directly to any of the maven build phases. Referring to the docs, I'm adding a second execution element to the grails-maven plugin, as follows:
<plugin>
<groupId>org.grails</groupId>
<artifactId>grails-maven-plugin</artifactId>
<version>${grails.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>init</goal>
<goal>maven-clean</goal>
<goal>validate</goal>
<goal>config-directories</goal>
<goal>maven-compile</goal>
<goal>maven-test</goal>
<goal>maven-war</goal>
<goal>maven-functional-test</goal>
</goals>
</execution>
<execution>
<id>stats</id>
<phase>init</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<command>stats</command>
</configuration>
</execution>
</executions>
</plugin>
For this example I'm trying to execute grails stats in the maven init phase. (Eventually, stats will be replaced by something more useful.) when I run:
$ mvn package
the ouput includes:
[INFO] [grails:validate {execution: default}]
[INFO] [grails:init {execution: default}]
[INFO] [grails:config-directories {execution: default}]
[INFO] [resources:resources {execution: default-resources}]
which evidently doesn't contain the execution of grails stats. I am able to execute the stats command through Maven directly, in the following way:
$ mvn grails:exec -Dcommand=stats
it only fails to execute when added as a goal in the pom.
I'm using Java 1.5.0_22, Grails 1.3.7, and Maven 2.2.1.
Based on the information I could find about this plugin, you should add the grails: prefix to your goals.
I have no experience with this plugin, so I could be wrong =)
References:
http://grails.1312388.n4.nabble.com/grails-maven-plugin-version-td2284532.html
http://www.grails.org/Maven+Integration
so for example: <goal>grails:init</goal>
OK, my problem was confusing goals and Maven phases. Notice I was trying
<phase>init</phase>
and I had tried other items from the list of goals, but these are not Maven phases.
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

Categories

Resources