I have a JSON File and I want to convert it to POJO, for this I am using the plugin of org.jsonschema2pojo in maven. I am not able to generate the resultant pojo.Here's the snippet from pom.xml
<build>
<plugins>
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.4.23</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>${basedir}/src/main/resources/result</targetPackage>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I am using the generate sources goal in maven. My expectation is that it should give me pojo files at ${basedir}/src/main/resources/result location. However I am getting so. Please help me out.
Thanks,
Rajit
You want to use <outputDirectory> instead of <targetPackage>. More details here:
http://joelittlejohn.github.io/jsonschema2pojo/site/0.4.23/generate-mojo.html#outputDirectory
http://joelittlejohn.github.io/jsonschema2pojo/site/0.4.23/generate-mojo.html#targetPackage
Target package is the Java package you want your types to use, e.g. com.youcompany.model.
Also, typically you want the generated output to go into the target directory, not src. Derived files usually go there since anything inside target is usually omitted from source control. You don't need to specify outputDirectory if you don't want to, by default the generated output will go into /target/java-gen.
Below code works for me.
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<executions>
<execution>
<id>1</id>
<configuration>
<annotationStyle>jackson2</annotationStyle>
<includeAdditionalProperties>false</includeAdditionalProperties>
<sourcePaths>
<sourcePath>${project.basedir}/src/main/resource/jsd/your_schema.json</sourcePath>
</sourcePaths>
<targetPackage>your target package</targetPackage>
</configuration>
<goals>
<goal>generate</goal>
</goals>
</execution>
<execution>
<id>2</id>
<configuration>
<annotationStyle>jackson2</annotationStyle>
<includeAdditionalProperties>false</includeAdditionalProperties>
<sourcePaths>
<sourcePath>${project.basedir}/src/main/resource/jsd/your_schema2.json</sourcePath>
</sourcePaths>
<targetPackage>your target package</targetPackage>
</configuration>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
Use both targetPackage and outputDirectory.
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<outputDirectory>src/main/java</outputDirectory>
<targetPackage>com.your.package</targetPackage>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
Related
I have a project where we have code implemented using JPA and we use metamodels. To generate hibernate metamodels we use following as a maven plaugin
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>process-sources</phase>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/annotations/</outputDirectory>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>
Now we like to try out QueryDSL, and I added the maven configuration to generate QueryDSL-models. It looks like following.
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
But now when I run maven clean test it complains that JPA Hibernate metamodels are missing. But if I switch the order then it complaints that other metamodels are missing. So is there a way to use both of them in same porject?
I guess your problem is not the generation itself, but that you have classes in your codebase that depend on the generated classes.
Have you tried to add the generation output directories as source directories like this?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/annotations</source>
<source>${project.build.directory}/generated-sources/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Should work for mvn on CLI as well for m2e in Eclipse.
As i've already described in title i have a problem in accessing generated classes from my source classes when i add #XmlRootElement annotation on resource -generation step.
My jaxb2-maven-plugin config:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/resources/schemas</schemaDirectory>
<packageName>my.classses</packageName>
<bindingDirectory>${project.basedir}/src/main/resources/xjb</bindingDirectory>
<extension>true</extension>
</configuration>
</plugin>
Any help will be useful, thanks in advance!
The generated source files are probably located under generated-sources which is not added as a source directory by default, try using something like:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/DIRNAME</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Just replace DIRNAME with the name of the directory the generated files are in.
I've just got the problem some hours ago and it always seemd to working until now.
I generate code in my pom on the following way:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<configuration>
<sourceDestDir>${basedir}/target/generated/java</sourceDestDir>
<keep>true</keep>
<verbose>true</verbose>
<extension>true</extension>
<wsdlDirectory>${basedir}/src/main/resources/META-INF</wsdlDirectory>
</configuration>
<executions>
<execution>
<id>ecad-ws</id>
<phase>generate-sources</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlFiles>
<wsdlFile>wsdl/ECadDocumentServiceWSDL.wsdl</wsdlFile>
</wsdlFiles>
<staleFile>${project.build.directory}/jaxws/stale/wsdl.ECadDocumentServiceWSDL.done</staleFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/META-INF/xsd</schemaDirectory>
<packageName>be.fgov.health.ecad.xmlstructure</packageName>
<outputDirectory>${basedir}/target/generated/java</outputDirectory>
</configuration>
</plugin>
and I use those generated classes in my project.
If I then do a "right click -> maven -> clean" + "right click -> maven -> install" everything is working.
But when I run mvn clean install -DskipTest=true, then maven can't find the generated sources.. I'm stuck for several hours already and can't really find it. (doing this in Eclipse btw)
EDIT:
just figured out the following: If I remove the second plugin (to generate by xsd) I won't get any error.. If I put all the code that uses thoes generated classes in comment ofc.
Another EDIT:
I've changed the outputDirectory from the jaxb generation and now it's working. Can anyone explain me why it can't be the same as the wsimport location?
By default, the jaxb2-maven-plugin deletes the outputDirectory before putting the generated classes inside.
You can control this behaviour with the attribute clearOutputDir. Your plugin configuration would then look like :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/META-INF/xsd</schemaDirectory>
<packageName>be.fgov.health.ecad.xmlstructure</packageName>
<outputDirectory>${basedir}/target/generated/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
Usually generated sources should be created in the target dir. But how do I handle classes that are only used for test? I dont want that these classes get packaged in my jar. Is there a common way to deal with this situation?
Use maven build helper plugin's add-test-source goal to add your generated test source files to the build -> http://mojo.codehaus.org/build-helper-maven-plugin/add-test-source-mojo.html
It ensures that the directories added by this goal will be picked up automatically by the compiler plugin during test-compile phase of the build.
EDIT
Here is the example of how to generate code for testign with cxf-codegen-plugin
<build>
<plugins>
...
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-test-sources</id>
<phase>generate-test-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/wsdl/myService.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
<executions>
<execution>
<id>add-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated/cxf</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
I tried to generate two xmlbeans in one project. Each one, for example, gets participant object, so I can't put them in one configuration. The way I did was using two excution, here is my pom file:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
<version>2.3.3</version>
<executions>
<execution>
<id>xmlbean1</id>
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
<configuration>
<xmlConfigs>
<xmlConfig implementation="java.io.File">src/main/xsdconfig/xmlbean1</xmlConfig>
</xmlConfigs>
<verbose>true</verbose>
<schemaDirectory>src/main/xsd/xmlbean1</schemaDirectory>
</configuration>
</execution>
<execution>
<id>xmlbean2</id>
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
<configuration>
<xmlConfigs>
<xmlConfig implementation="java.io.File">src/main/xsdconfig/xmlbean2</xmlConfig>
</xmlConfigs>
<verbose>true</verbose>
<schemaDirectory>src/main/xsd/xmlbean2</schemaDirectory>
</configuration>
</execution>
</executions>
<inherited>true</inherited>
</plugin>
But it is not working at all. Could anyone help me with that, thanks
Thanks everybody, i got the answer, the following pom is working fine:
<executions>
<execution>
<id>id1</id>
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
<configuration>
<schemaDirectory>src/main/xsd/first</schemaDirectory>
<xmlConfigs>
<xmlConfig implementation="java.io.File">src/main/xsdconfig/first</xmlConfig>
</xmlConfigs>
<verbose>true</verbose>
<sourceGenerationDirectory>target/first-resource</sourceGenerationDirectory>
<classGenerationDirectory>target/first-class</classGenerationDirectory>
<staleFile>target/first/first.stale</staleFile>
</configuration>
</execution>
<execution>
<id>id2</id>
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
<configuration>
<schemaDirectory>src/main/xsd/second</schemaDirectory>
<xmlConfigs>
<xmlConfig implementation="java.io.File">src/main/xsdconfig/second</xmlConfig>
</xmlConfigs>
<verbose>true</verbose>
<sourceGenerationDirectory>target/second-resource</sourceGenerationDirectory>
<classGenerationDirectory>target/second-class</classGenerationDirectory>
<staleFile>target/second/second.stale</staleFile>
</configuration>
</execution>
</executions>
This doesn't work because the id is only used to find an existing execution (when you want to tweak it).
Your problem is that Maven can't run the same plugin twice in the same phase.
What are your options?
Split that into different sub modules
Use Ant to create xmlbeans and use the antrun element.
But I wonder why you can't use two xmlConfig elements. Just put all your .xsd files into one directory and create as many beans from them as necessary (see "Multiple XSDConfig Directories")
You should try using another, distinct phase for the second invocation. AFAIK the same plugin cannot be executed twice in the same lifecycle phase.