In my maven project, I want all my datetime entries should be generated as java.util.date instead of XMLGregorianCalendar. As you might know XMLGregorianCalendar gets generated by default.
We can take example project provided here.
Here in the CustomersOrders.xsd, you can see attriute ShippedDate is of type dateTime.
<xs:attribute name='ShippedDate' type='xs:dateTime' />
To convert its data type into java.util.date, I'm following approach provided in documentation here. i.e. by using external binding file, like:
Customer.xjb
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<globalBindings>
<javaType name="java.util.date" xmlType="xs:datetime"
parseMethod="javax.xml.bind.DatatypeConverter.parseDate"
printMethod="javax.xml.bind.DatatypeConverter.printDate"
/>
</globalBindings>
</bindings>
Then I mapped Customer.xjb file in pom.xml like:
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<!-- the package for the generated java classes -->
<generatePackage>com.dimitrisli.jaxb.producedClasses</generatePackage>
<!-- If the following not specified all xsd in resources are included -->
<schemaIncludes>
<include>sampleJaxb/CustomersOrders.xsd</include>
</schemaIncludes>
<!-- if you don't want old output -->
<removeOldOutput>true</removeOldOutput>
<!-- if you want verbosity -->
<!-- verbose>true</verbose -->
<xjbSources>
<xjbSource>sampleJaxb/Customers.xjb</xjbSource>
</xjbSources>
</configuration>
</execution>
</executions>
But when I do mvn clean install, I'm still not able to see any difference in ShippedDate, which is still being generated as XMLGregorianCalendar.
Please suggest what am I missing.
Thank You
If you use the org.jvnet.jaxb2.maven2:maven-jaxb2-plugin then you should use bindingIncludes instead of xjbSources (it's for org.codehaus.mojo:jaxb2-maven-plugin).
<bindingIncludes>
<include>sampleJaxb/Customers.xjb</include>
</bindingIncludes>`
Also, you have to implement a custom adapter for java.util.Date like you have seen in the tutorial or convert to java.util.Calendar:
<javaType name="java.util.Calendar" xmlType="xsd:dateTime"
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />`
Hope it helps!
Related
I want to generate java classes with the jaxb2-maven-plugin. I am using the following configuration:
pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>SomeID</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<extension>true</extension>
<clearOutputDir>true</clearOutputDir>
<sources>
<source>src/main/xsd/schema.xsd</source>
</sources>
<noGeneratedHeaderComments>true</noGeneratedHeaderComments>
</configuration>
</execution>
</executions>
</plugin>
schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://my.target.namespace/uri"
xmlns="http://my.target.namespace/uri"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:h="http://my.uri.for.prefix.h"
xmlns:f="http://my.target.namespace/uri">
<xsd:import namespace="http://my.uri.for.prefix.h" schemaLocation="schema2.xsd"/>
<xsd:complexType name="FooType">
<xsd:sequence>
<xsd:element ref="h:something" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="FooType" type="FooType" />
</xsd:schema>
The Jaxb2 plugin is generating me the following package-info.java:
#javax.xml.bind.annotation.XmlSchema(namespace = "http://my.target.namespace/uri", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ...;
But, what I want to get is this:
#javax.xml.bind.annotation.XmlSchema(namespace = "http://my.target.namespace/uri", xmlns = {
#XmlNs(prefix="f", namespaceURI="http://my.target.namespace/uri"),
#XmlNs(prefix="h", namespaceURI="http://my.uri.for.prefix.h")
}, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ...;
import javax.xml.bind.annotation.XmlNs;
The prefixes are missing in my generated file. How to do that? I tried already to create a binding file but this didn't worked how I expected.
Please see this answer on how to solve this problem:
https://stackoverflow.com/a/10812236/1389219
The answer is very well written and easy to follow. Basically you will have to:
Drop jaxb2-maven-plugin in favour of maven-jaxb2-plugin.
Include the jaxb2-namespace-prefix dependency and provide the <arg>-Xnamespace-prefix</arg>.
Write a new bindings.xml file which is only a few lines long.
Your POM file will become more verbose, but it is worth it to have a package-info.java generated the way you require.
As a bonus, there are a heap of additional plugins and dependencies related to maven-jaxb2-plugin that provide extra features. One that I found helpful was jaxb2-rich-contract-plugin that gave the ability to generate builders and make the generated classes immutable*.
* Well, not strictly speaking immutable (as it just changes the setter methods to be package private), but enough to make them feel safer.
I'm using Maven to generate the implementation code for a web service. Originally, this code was generated from a WSDL and schema using something else (probably a wizard in Eclipse). The service implementation class name that jaxws-maven-plugin generates is MyService_MyServieSOAPImpl. When this was originally generated, the implementation class was named MyService_SOAPImplementation. I'm guessing that the wizard that Eclipse uses allows the user to choose the name of the implementation class. I tried using the sei element, but it does not work. Here's a snippet of the wsimport plugin in my POM:
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<id>generateMyServiceFromWSDL</id>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<genJWS>true</genJWS>
<bindingDirectory>${basedir}/src/main/bindings</bindingDirectory>
<bindingFiles>
<bindingFile>otherSchema.episode</bindingFile>
</bindingFiles>
<wsdlDirectory>${basedir}/src/main/resources/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>MyService.wsdl</wsdlFile>
</wsdlFiles>
<wsdlLocation>${project.basedir}/src/main/resources/wsdl/MyService.wsdl</wsdlLocation>
<sourceDestDir>${project.basedir}/src/main/java</sourceDestDir>
<sei>com.myCompany.MyService_SOAPImplementation</sei>
<xdonotoverwrite>true</xdonotoverwrite>
<xnocompile>true</xnocompile>
<xdebug>true</xdebug>
<verbose>true</verbose>
<target>2.0</target>
</configuration
</execution>
...
From some of the documents I've read, it looks like sei is only applicable for wsgen, not wsimport. If that is so, is there any way to force a name for the implementation class name?
UPDATE
Ok, so from my reading, it should be possible to do this with a JAX-WS binding file, not directly in the POM file.
I've created my binding file like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxws:bindings version="2.1" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
jaxws:wsdlLocation="src/main/webapp/wsdl/MyService.wsdl">
<jaxws:bindings node="wsdl:definitions/wsdl:portType[#name='MyService']">
<!-- change the generated SEI class -->
<jxb:class name="MyServiceSOAPImpl"></jxb:class>
</jaxws:bindings>
</jaxws:bindings>
But this still doesn't seem to have any effect - wsimport is still producing the implementation as MyService_MyServiceSOAPImpl.
Try changing your
<jaxws:bindings node="wsdl:definitions/wsdl:portType[#name='MyService']">
to
<jaxws:bindings node="wsdl:definitions/wsdl:service[#name='MyServiceService']">
Note that the second part of the node path is service and not portType. You would use portType to customize the service interface, not the implementation.
As an aside it's important to note that JAX-WS bindings (seemingly) need to be in their own XML file (per WSDL) and not included with JAXB bindings.
Someone may find the list of JAX-WS bindings here to be useful: http://docs.oracle.com/cd/E13222_01/wls/docs103/webserv/data_types.html#wp227312
In case this helps someone, two changes are required in OPs bindings.xml file to get it to work:
The first change was already suggested by MGE - changing the wsdl:portType in the Xpath to wsdl:service
The namespace for class name has to be 'jaxws', so
change to this:
<jaxws:class name="MyServiceSOAPImpl"/>
from
<jxb:class name="MyServiceSOAPImpl"></jxb:class>
I'm trying to build up some documentation for my Wicket Web Application. I have created a page to grab all of my mounted pages and display them in /sitemap.xml.
In the vein of documentation I've added a new tag to the file <siteMap:Description>
now I want to fill that description with the javadoc entry that describes the class file.
I know there is know direct way to access them at runtime. So Instead I'm hoping to copy them at compile time into a List where they will then be accessible from runtime. How would I do that?
I'm using Maven for my build.
EDIT
I should probably Also mention that I do have an AntTask Already defined as part of my build process to save the compile Dates/times to a property file.
It seems to me an Task to scan my Class and then put the information into a file is probably the way to go. Problem is I'm not sure how to proceed.
My Ant-Task is defined like in my pom.xml so:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-nodeps</artifactId>
<version>1.6.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>set-build-time</id>
<phase>process-sources</phase>
<configuration>
<tasks>
<tstamp>
<format property="build.timestamp" pattern="yyyy/MM/dd HH:mm:ss"/>
<format property="build.time" pattern="HH:mm:ss" />
<format property="build.date" pattern="MM/dd/yyyy" />
<format property="build.year" pattern="yyyy"/>
</tstamp>
<replaceregexp byline="true">
<regexp pattern="copyYear\=.*" />
<!--suppress MavenModelInspection -->
<substitution expression="copyYear=${build.year}" />
<fileset dir="src/main/java/" includes="**/*.properties" />
</replaceregexp>
<replaceregexp byline="true">
<regexp pattern="buildTime\=.*" />
<!--suppress MavenModelInspection -->
<substitution expression="buildTime=${build.date} ${build.time}" />
<fileset dir="src/main/java/" includes="**/*.properties" />
</replaceregexp>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
After doing more research I determined I was barking up the wrong tree.
I since I was trying to get Javadoc comments A Doclet was the better answer.
So I implemented a custom doclet and wired it up to run automatically as described in
the follow up question and answer below.
How can I compile and run my Custom Doclet class in my project?
Is there a tool which takes a Java File what describes a REST service as a parameter and generates a wadl file out of that.
I had the same problem: was using RESTeasy and wanted to find a way to generate the WADL automatically.
Did some research and came to the solution below.
1. Add this to your pom.xml:
<build>
<plugins>
<plugin>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>maven-wadl-plugin</artifactId>
<version>1.17</version>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<phase>${javadoc-phase}</phase>
</execution>
</executions>
<configuration>
<wadlFile>${project.build.outputDirectory}/application.wadl
</wadlFile>
<formatWadlFile>true</formatWadlFile>
<baseUri>http://example.com:8080/rest</baseUri>
<packagesResourceConfig>
<param>com.example.rs.resource</param>
</packagesResourceConfig>
<wadlGenerators>
<wadlGeneratorDescription>
<className>com.sun.jersey.server.wadl.generators.WadlGeneratorApplicationDoc
</className>
<properties>
<property>
<name>applicationDocsFile</name>
<value>${basedir}/src/main/doc/application-doc.xml</value>
</property>
</properties>
</wadlGeneratorDescription>
<wadlGeneratorDescription>
<className>com.sun.jersey.server.wadl.generators.WadlGeneratorGrammarsSupport
</className>
<properties>
<property>
<name>grammarsFile</name>
<value>${basedir}/src/main/doc/application-grammars.xml</value>
</property>
</properties>
</wadlGeneratorDescription>
</wadlGenerators>
</configuration>
</plugin>
</plugins>
</build>
Pay attention to the buildUri and packagesResourceConfig elements. You have to change them to reflect your project's configuration. You may also want to change the plugin's version (I used 1.17).
2. Create a /doc folder and add some files.
Create the src/main/doc/ folder and create the two files below.
File: application-doc.xml
Content:
<?xml version="1.0" encoding="UTF-8"?>
<applicationDocs targetNamespace="http://wadl.dev.java.net/2009/02">
<doc xml:lang="en" title="A message in the WADL">This is added to the start of the generated application.wadl</doc>
</applicationDocs>
File: application-grammars.xml
Content:
<?xml version="1.0" encoding="UTF-8" ?>
<grammars xmlns="http://wadl.dev.java.net/2009/02" />
3. Run the maven command.
Go to the project folder and run the following command:
$ mvn compile com.sun.jersey.contribs:maven-wadl-plugin:generate
The files \target\classes\application.wadl (the WADL itself) and \target\classes\xsd0.xsd (the schema of the resources - it's used by the application.wadl) should be generated.
Edit and use them as you wish.
PS.: Bear in mind that this is a very simple use of the maven-wadl-plugin. It can do a lot more. To know it better, please refer to the zip file mentioned in the other answer (by Pavel Bucek).
Yes, please see gerenate-wadl [1] sample from Jersey samples (look for maven-wadl-plugin).
[1] http://search.maven.org/remotecontent?filepath=com/sun/jersey/samples/generate-wadl/1.12/generate-wadl-1.12-project.zip
I don't seem to be able to work out how to get control the file name of the XSD file created by maven-jaxb-schemagen-plugin. The documentation is a bit sparse.
<groupId>com.sun.tools.jxc.maven2</groupId>
<artifactId>maven-jaxb-schemagen-plugin</artifactId>
<version>1.2</version>
<configuration>
<project>${project}</project>
<destdir>${project.build.directory}/generated-resources/schemas</destdir>
<srcdir>${project.build.sourceDirectory}/my/jaxb/bean/package</srcdir>
<verbose>true</verbose>
</configuration>
It always seems to create a file called schema1.xsd
You need to add schema elements which describe which file should contain the elements of each namespace you have:
<configuration>
[...]
<schemas>
<schema>
<namespace>http://www.example.invalid/2001/05/27/wibble</namespace>
<file>wibble.xsd</file>
</schema>
</schemas>
<configuration>
Assuming you have set the namespace of you components
#XmlRootElement(name = "wobble", namespace="http://www.example.invalid/2001/05/27/wibble")