Generate hashCode and equals methods with JAXB2 - java

I have a Maven project with XJC goals for model generation from XSD schema. I would like that in the generated schemas there are the equals and hashCode methods that are also generated.
I tried that:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<id>xjc_bonjour</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/main/resources/xsd/bonjour</source>
</sources>
<packageName>xxx.jobs.bonjour.schema</packageName>
</configuration>
</execution>
</executions>
</plugin>
and
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<extension>true</extension>
<generatePackage>xxx.jobs.bonjour.schema</generatePackage>
<args>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.4</version>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>0.6.4</version>
</dependency>
</dependencies>
I don't really know what is the problem here, when I do a maven generated ressources, the equals and the hashCode methods are not in bonjour model.

Related

How to generate toString method with jaxb2?

I would like to overload the toString methode from my wsdl.
In my pom.xml
I add that in the dependencies :
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>1.11.1</version>
</dependency>
I added this plugin in the build/plugins
Blockquote
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.7.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.build.directory}/wsdl/META-INF/wsdl/</schemaDirectory>
<schemaIncludes>
<include>FrameworkGedServiceMetier.wsdl</include>
</schemaIncludes>
<args>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-Xcopyable</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>1.11.1</version>
</plugin>
</plugins>
</configuration>
</plugin>
I build maven is successful but I don't have my method toString in the class.
To inspirate me i use this documentation
I think there is a problem with that version. Please try:
<version>0.12.0</version>
(It looks like 1.11.1 was published by mistake.)

Appengine cloud endpoints v2: specify datastore in devserver

I migrated to cloud endpoints v2 following the migration document and v2 example from git repo (https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/appengine/endpoints-frameworks-v2). However, I am not sure on how to set jvmFlag(s) to specify additional properties like backend_store, port, enable debug etc like the it was in v1.
Here is the build tag from my pom.xml.
<build>
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<!-- <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId>
<version>2.1</version> <executions> <execution> <phase>compile</phase> <goals>
<goal>display-dependency-updates</goal> <goal>display-plugin-updates</goal>
</goals> </execution> </executions> </plugin> -->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<devserver.storagePath>/Users/user/Documents/development/health/local_db.bin</devserver.storagePath>
</configuration>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>endpoints-framework-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<!-- plugin configuration -->
<hostname>amplified-lamp-688.appspot.com</hostname>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>3.6.2</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
<!--<enableJarClasses>false</enableJarClasses>-->
<!--<!– Comment in the below snippet to bind to all IPs instead of just-->
<!--localhost –>-->
<!--<!– address>0.0.0.0</address> –>-->
<!--<port>8080</port>-->
<!--<!– Comment in the below snippet to enable local debugging with a remove-->
<!--debugger like those included with Eclipse or IntelliJ –>-->
<!--<jvmFlags>-->
<!--<jvmFlag>-Xdebug</jvmFlag>-->
<!--<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>-->
<!--<jvmFlag>-Ddatastore.backing_store=/Users/user/Documents/development/health/local_db.bin</jvmFlag>-->
<!--<jvmFlag>-Ddatastore.default_high_rep_job_policy_unapplied_job_pct=10</jvmFlag>-->
<!--</jvmFlags>-->
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>display-dependency-updates</goal>
<goal>display-plugin-updates</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Please help with configuring the jvmFlags.
This is one of the solution to specify jvmFlags
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<!--<devserver.storagePath>/Users/user/Documents/development/health/local_db.bin</devserver.storagePath>-->
<devserver:port>8181</devserver:port>
<devserver.jvmFlags>-Xdebug</devserver.jvmFlags>
<devserver.jvmFlags>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</devserver.jvmFlags>
<devserver.jvmFlags>-Ddatastore.backing_store=/Users/user/Documents/development/health/local_db.bin</devserver.jvmFlags>
</configuration>
</plugin>

Forcing jaxb2-maven-plugin to generate setters or constructors

I have a following build configuration. It is working properly, however the problem is in case of generating constructor with all args or generating setters for list.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
<sources>
<source>src/main/resources/xsd</source>
</sources>
</configuration>
</plugin>
</plugins>
</build>
Can you tell me how to force xjc to generate setters for lists or args-constructors ?
This thing works for me
<configuration>
<args>
<arg>-Xvalue-constructor</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-value-constructor</artifactId>
<version>3.0</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.11.1</version>
</plugin>
</plugins>
</configuration>
Let me know if you find any issues with this.
For generating setter for the collection I added the dependency to org.andromda.thirdparty.jaxb2_commons but it' works only for the new version of jaxb2-maven-plugin. I tried with 2.5.0 and it's works, using the version 2.3.1 doesn't work.
Here an example:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<dependencies>
<dependency>
<groupId>org.andromda.thirdparty.jaxb2_commons</groupId>
<artifactId>collection-setter-injector</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<executions>
<execution>
......
</execution>
</executions>
<configuration>
<sources>
......
</sources>
<arguments>-Xcollection-setter-injector</arguments>
<clearOutputDir>false</clearOutputDir>
<extension>true</extension>
</configuration>
</plugin>
To generate collection setters you have to add collection-setter-injector to jaxb plugin dependencies and for value constuctor generation - jaxb2-value-constructor. You also have to add proper args to argument list (-Xcollection-setter-injector and -Xvalue-constructor. Example plugin configuration in pom.xml:
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<dependencies>
<dependency>
<groupId>org.andromda.thirdparty.jaxb2_commons</groupId>
<artifactId>collection-setter-injector</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-value-constructor</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<executions>
...
</executions>
<configuration>
<sources>
...
</sources>
<clearOutputDir>true</clearOutputDir>
<arguments>
<argument>-Xcollection-setter-injector</argument>
<argument>-Xvalue-constructor</argument>
</arguments>
<extension>true</extension>
</configuration>
</plugin>
</plugins>
</build>
...

Maven Jaxb plugin can't generate classes from target folder

I try to copy an xsd file from a maven dependency and put it into target folder with others xsd files of my project and after that i wante to generate the jaxb classes but it can't generate them in the same time.
When i make only the code of xsd file of the dependency, it can generate the jaxb classes but for the xsd files of my project it can't.
<plugin>
<!-- copy the xsdl files of my current project into the target folder-->
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources-xsd</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/tmp/schemas</outputDirectory>
<encoding>UTF-8</encoding>
<resources>
<resource>
<directory>${basedir}/src/main/schemas</directory>
<includes>
<include>**/*.xsd</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--copy the xsd files of the dependency into the target folder of my current project-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-libraries</id>
<phase>generate-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.my.dependency</groupId>
<artifactId>res-communes</artifactId>
<version>${res-communes.version}</version>
<type>xsd</type>
<classifier>typesFy</classifier>
<destFileName>typesFy.xsd</destFileName>
<outputDirectory>${project.build.directory}/tmp/schemas</outputDirectory>
</artifactItem>
</artifactItems>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>${maven-jaxb2-plugin.version}</version>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<extension>true</extension>
<enableIntrospection>true</enableIntrospection>
<markGenerated>true</markGenerated>
<schemas>
<schema>
<fileset>
<directory>${project.build.directory}/tmp/schemas</directory>
</fileset>
</schema>
</schemas>
<args>
<arg>-Xts:style:org.apache.commons.lang.builder.ToStringStyle.MULTI_LINE_STYLE</arg>
<arg>-Xbg</arg>
<arg>-Xfluent-api</arg>
<arg>-Xinheritance</arg>
<arg>-Xannotate</arg>
<arg>-XJsr303Annotations</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>${jaxb2-basics.version}</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>${jaxb2-basics-annotate.version}</version>
</plugin>
<plugin>
<groupId>org.apache.cxf.xjcplugins</groupId>
<artifactId>cxf-xjc-ts</artifactId>
<version>${cxf-xjc-ts.version}</version>
</plugin>
<plugin>
<groupId>org.apache.cxf.xjcplugins</groupId>
<artifactId>cxf-xjc-boolean</artifactId>
<version>${cxf-xjc-boolean.version}</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-fluent-api</artifactId>
<version>${jaxb2-fluent-api.version}</version>
</plugin>
<plugin>
<groupId>com.github.krasa</groupId>
<artifactId>krasa-jaxb-tools</artifactId>
<version>${krasa-jaxb-tools.version}</version>
</plugin>
</plugins>
</configuration>
</plugin>
maven-jaxb2-plugin runs in generate-sources phase by default. You unpack your schemas in generate-resources. Schemas are just not yet there when you try to compile them.
To debug such problems run mvn -X clean install and check the logs.

My mixed Scala/Java Maven project doesn't compile

I have a mixed Scala/Java project that doesn't compile well.
The problem arises when Java code is trying to call Scala code in the same package.
Of course, I have the standard layout:
src/main/java
src/main/scala
src/test/java
src/test/scala
I've looked at other similar Stackoverflow questions but this question is a little outdated. This other question doesn't help either.
I have also followed the scala-maven-plugin documentation page.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.6</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I have tried unsuccessfully to follow this blog post.
IDEA project with Scala plugin imported from the pom.xml can compile and run my project successfully.
What is the right way of doing it?
Does the Java code gets compiled twice? First by the Scala plugin and the by the Java plugin?.
Here is a working example of pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>stackoverflow</groupId>
<artifactId>q24448582</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<scala.version>2.10.3</scala.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>test-compile</phase>
</execution>
<execution>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Problem 1
At first I thought the problem was due to using a modern version of the maven-compiler-plugin. I was using version 3.0 instead of 2.0.2.
The solution has been:
- Remove the maven-compiler-plugin
- Add this setting: incremental
Finally my pom.xml is this:
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.6</version>
<configuration>
<recompileMode>incremental</recompileMode>
<args>
<arg>-deprecation</arg>
<arg>-explaintypes</arg>
<arg>-target:jvm-1.7</arg>
</args>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>add-source</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
Problem 2
Another problem I was experiencing is this:
<dependencies>
<dependency>
<groupId>org.json4s</groupId>
<artifactId>json4s-native_${scala.version}</artifactId>
<version>3.2.9</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}.4</version>
</dependency>
</dependencies>
<properties>
<scala.version>2.10</scala.version>
</properties>
This make this solved problem arise:
https://issues.scala-lang.org/browse/SI-5733
Probably it was using an old version of the Scala compiler.
The solution has been to rename scala.version to scala.major, as this property has a special meaning.
In order to get the scala-maven-plugin to respect Java 1.8, but still allow mixed compilation of java and scala, we use this:
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<recompileMode>incremental</recompileMode>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
...
I add blow code at pom.xml file <build> block and working good!
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- SCALA AND JAVA MIX COMPILATION -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<executions>
<execution>
<id>scala-compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${project.artifactId}-shaded-${project.version}</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
...
</pluginManagement>
<resources>
...
</resources>
</build>

Categories