我如何告诉jaxb / Maven生成多个模式包?
例:
</plugin> <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.7.1</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>src/main/resources/dir1</schemaDirectory> <schemaIncludes> <include>schema1.xsd</include> </schemaIncludes> <generatePackage>schema1.package</generatePackage> </configuration> </plugin> <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.7.1</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>src/main/resources/dir2</schemaDirectory> <schemaIncludes> <include>schema2.xsd</include> </schemaIncludes> <generatePackage>schema2.package</generatePackage> </configuration> </plugin> </plugins>
发生了什么事情:Maven执行第一个插件。 然后删除目标文件夹并创build第二个包,然后可见。
我试着为第一个configuration设置target / somedir1,为第二个configuration设置target / somedir2。 但行为不会改变? 有任何想法吗? 我不想直接在src / main / java文件夹中生成包,因为这些包是生成的,不应该与手动创build的类混合使用。
我不得不指定不同的generateDirectory
(没有这个,插件考虑文件是最新的,在第二次执行期间没有产生任何东西)。 而且我build议遵循target/generated-sources/<tool>
的target/generated-sources/<tool>
约定,以便自动将它们导入到您喜欢的IDE中。 我也build议声明几次execution
而不是声明两次插件(并且移动每个execution
元素中的configuration
):
<plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.7.1</version> <executions> <execution> <id>schema1-generate</id> <goals> <goal>generate</goal> </goals> <configuration> <schemaDirectory>src/main/resources/dir1</schemaDirectory> <schemaIncludes> <include>shiporder.xsd</include> </schemaIncludes> <generatePackage>com.stackoverflow.package1</generatePackage> <generateDirectory>${project.build.directory}/generated-sources/xjc1</generateDirectory> </configuration> </execution> <execution> <id>schema2-generate</id> <goals> <goal>generate</goal> </goals> <configuration> <schemaDirectory>src/main/resources/dir2</schemaDirectory> <schemaIncludes> <include>books.xsd</include> </schemaIncludes> <generatePackage>com.stackoverflow.package2</generatePackage> <generateDirectory>${project.build.directory}/generated-sources/xjc2</generateDirectory> </configuration> </execution> </executions> </plugin>
使用这个设置,在mvn clean compile
之后,我得到了下面的结果
$树目标/ 目标/ ├──上课 │├──com ││└──stackoverflow ││├──App.class ││├──package1 │││├──ObjectFactory.class │││├──Shiporder.class │││├──Shiporder $ Item.class │││└──订单$ Shipto.class ││└──package2 ││├──BookForm.class ││├──BooksForm.class ││├──ObjectFactory.class ││└──package-info.class │├──dir1 ││└└─shiporder.xsd │└──dir2 │└──books.xsd └──生成的来源 ├──xjc │└──META-INF │└──sun-jaxb.episode ├──xjc1 │└──com │└──stackoverflow │└──package1 │├──ObjectFactory.java │└└─Shiporder.java └──xjc2 └──com └──stackoverflow └──package2 ├──BookForm.java ├──BooksForm.java ├──ObjectFactory.java └──package-info.java
这似乎是预期的结果。
也可以使用JAXB绑定为每个模式指定不同的包,例如
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0" schemaLocation="book.xsd"> <jaxb:globalBindings> <xjc:serializable uid="1" /> </jaxb:globalBindings> <jaxb:schemaBindings> <jaxb:package name="com.stackoverflow.book" /> </jaxb:schemaBindings> </jaxb:bindings>
然后在pom.xml
使用新的maven-jaxb2-plugin 0.8.0 <schemas>
和<bindings>
元素。 或者在<schemaDirectory>
和<bindingDirectory>
以及<include>
模式和绑定中指定最上面的目录:
<schemaDirectory>src/main/resources/xsd</schemaDirectory> <schemaIncludes> <include>book/*.xsd</include> <include>person/*.xsd</include> </schemaIncludes> <bindingDirectory>src/main/resources</bindingDirectory> <bindingIncludes> <include>book/*.xjb</include> <include>person/*.xjb</include> </bindingIncludes>
我认为这是更方便的解决scheme ,因为当你添加一个新的XSD你不需要改变Maven pom.xml
,只需要添加一个新的XJB绑定文件到同一个目录。
你应该改变它只定义一次插件,并执行两次执行区域…如下所示…和generateDirectory应设置(基于文档)..
<plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.7.1</version> <executions> <execution> <id>firstrun</id> <goals> <goal>generate</goal> </goals> <configuration> <generateDirectory>target/gen1</generateDirectory> <schemaDirectory>src/main/resources/dir1</schemaDirectory> <schemaIncludes> <include>schema1.xsd</include> </schemaIncludes> <generatePackage>schema1.package</generatePackage> </configuration> </execution> <execution> <id>secondrun</id> <goals> <goal>generate</goal> </goals> <configuration> <generateDirectory>target/gen2</generateDirectory> <schemaDirectory>src/main/resources/dir2</schemaDirectory> <schemaIncludes> <include>schema2.xsd</include> </schemaIncludes> <generatePackage>schema2.package</generatePackage> </configuration> </execution> </executions> </plugin>
在我看来,你是反对单一的神器的神器规则…可能是你应该考虑这个。
这也可以通过为模式指定陈旧的文件名而不清除输出目录来实现。 默认的输出目录会自动包含在classpath中,这是很不方便的。 如果我们指定不同的输出目录,则必须在IDE中使用此代码来处理类path。 例如 –
<plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.3.1</version> <configuration> <quiet>true</quiet> <verbose>false</verbose> <clearOutputDir>false</clearOutputDir> <readOnly>true</readOnly> <arguments>-mark-generated</arguments> </configuration> <executions> <execution> <id>reportingSchema</id> <goals> <goal>xjc</goal> </goals> <configuration> <schemaDirectory>src/main/resources/schema/r17/schemaReporting</schemaDirectory> <schemaIncludes> <include>OCISchemaReporting.xsd</include> </schemaIncludes> <packageName>com.broadsoft.oci.r17.reporting</packageName> <staleFile>${build.directory}/generated-sources/.jaxb-staleFlag-reporting</staleFile> </configuration> </execution> <execution> <id>schemaAS</id> <goals> <goal>xjc</goal> </goals> <configuration> <schemaDirectory>src/main/resources/schema/r17/schemaAS</schemaDirectory> <schemaIncludes> <include>OCISchemaAS.xsd</include> </schemaIncludes> <packageName>com.broadsoft.oci.r17.as</packageName> <staleFile>${build.directory}/generated-sources/.jaxb-staleFlag-as</staleFile> </configuration> </execution> </executions> </plugin> </plugins>
来源: 使用JAXB插件生成代码
我已经解决了:
<removeOldOutput>false</removeOldOutput> <clearOutputDir>false</clearOutputDir> <forceRegenerate>true</forceRegenerate>
添加到每个configuration;)
经过多次试验,以下几种方法对我有用
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.1</version> <executions> <execution> <id>xjc1</id> <goals> <goal>xjc</goal> </goals> <configuration> <packageName>com.mycompany.clientSummary</packageName> <sourceType>wsdl</sourceType> <sources> <source>src/main/resources/wsdl/GetClientSummary.wsdl</source> </sources> <outputDirectory>target/generated-sources/xjb</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </execution> <execution> <id>xjc2</id> <goals> <goal>xjc</goal> </goals> <configuration> <packageName>com.mycompany.wsclient.employerProfile</packageName> <sourceType>wsdl</sourceType> <sources> <source>src/main/resources/wsdl/GetEmployerProfile.wsdl</source> </sources> <outputDirectory>target/generated-sources/xjb</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </execution> <execution> <id>xjc3</id> <goals> <goal>xjc</goal> </goals> <configuration> <packageName>com.mycompany.wsclient.producersLicenseData</packageName> <sourceType>wsdl</sourceType> <sources> <source>src/main/resources/wsdl/GetProducersLicenseData.wsdl</source> </sources> <outputDirectory>target/generated-sources/xjb</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </execution> </executions> </plugin>
这在插件的1.6版本中得到了修复 。
<groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.6</version>
快速注意,但我注意到,第一次迭代输出被删除。 我通过将以下内容添加到每个执行中来修复它。
<removeOldOutput>false</removeOldOutput> <clearOutputDir>false</clearOutputDir>
这里是我完整的工作示例,每次迭代都能正确输出。 顺便说一句,我不得不这样做,因为我给了xsd的重复命名空间问题。 这似乎解决了我的问题。
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>submitOrderRequest</id> <goals> <goal>xjc</goal> </goals> <configuration> <extension>true</extension> <schemaDirectory>src/main/resources/xsd/</schemaDirectory> <!-- <schemaFiles>getOrderStatusResponse.xsd,quoteShippingRequest.xsd,quoteShippingResponse.xsd,submitOrderRequest.xsd,submitOrderResponse.xsd</schemaFiles> --> <schemaFiles>submitOrderRequest.xsd</schemaFiles> <bindingDirectory>${project.basedir}/src/main/resources/xjb</bindingDirectory> <bindingFiles>submitOrderRequest.xjb</bindingFiles> <removeOldOutput>false</removeOldOutput> <clearOutputDir>false</clearOutputDir> </configuration> </execution> <execution> <id>submitOrderResponse</id> <goals> <goal>xjc</goal> </goals> <configuration> <extension>true</extension> <schemaDirectory>src/main/resources/xsd/</schemaDirectory> <!-- <schemaFiles>getOrderStatusResponse.xsd,quoteShippingRequest.xsd,quoteShippingResponse.xsd,submitOrderRequest.xsd,submitOrderResponse.xsd</schemaFiles> --> <schemaFiles>submitOrderResponse.xsd</schemaFiles> <bindingDirectory>${project.basedir}/src/main/resources/xjb</bindingDirectory> <bindingFiles>submitOrderResponse.xjb</bindingFiles> <removeOldOutput>false</removeOldOutput> <clearOutputDir>false</clearOutputDir> </configuration> </execution> </executions> </plugin>
在Maven中使用jaxb时遇到了很多问题,但是我通过执行以下操作来解决您的问题
首先创build一个schema.xjc文件
<?xml version="1.0" encoding="UTF-8"?> <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0"> <jaxb:bindings schemaLocation="YOUR_URL?wsdl#types?schema1"> <jaxb:schemaBindings> <jaxb:package name="your.package.name.schema1"/> </jaxb:schemaBindings> </jaxb:bindings> <jaxb:bindings schemaLocation="YOUR_URL??wsdl#types?schema2"> <jaxb:schemaBindings> <jaxb:package name="your.package.name.schema2"/> </jaxb:schemaBindings> </jaxb:bindings> </jaxb:bindings>
软件包名称可以是任何你想要的,只要它不包含Java中的任何保留关键字
接下来,您必须创buildwsimport.bat脚本,以便在首选位置生成打包和代码。
cd C:\YOUR\PATH\TO\PLACE\THE\PACKAGES wsimport -keep -verbose -b "C:\YOUR\PATH\TO\schema.xjb" YOUR_URL?wsdl pause
如果你不想使用cd,你可以把wsimport.bat放在“C:\ YOUR \ PATH \ TO \ PLACE \ THE \ PACKAGES”
如果你运行它没有-keep -verbose它只会生成包而不是.java文件。
-b将确保生成时使用schema.xjc
还有另外一个清晰的(IMO)解决scheme。有一个名为“staleFile”的参数,用作标志不再生成东西。 只需在每次执行中改变它。