如何告诉 jaxb/Maven 生成多个模式包? [英] How can I tell jaxb / Maven to generate multiple schema packages?

查看:24
本文介绍了如何告诉 jaxb/Maven 生成多个模式包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

</plugin><插件><groupId>org.jvnet.jaxb2.maven2</groupId><artifactId>maven-jaxb2-plugin</artifactId><version>0.7.1</version><执行><执行><目标><目标>生成</目标></目标></执行></执行><配置><schemaDirectory>src/main/resources/dir1</schemaDirectory><schemaIncludes><include>schema1.xsd</include></schemaIncludes><generatePackage>schema1.package</generatePackage></配置></插件><插件><groupId>org.jvnet.jaxb2.maven2</groupId><artifactId>maven-jaxb2-plugin</artifactId><version>0.7.1</version><执行><执行><目标><目标>生成</目标></目标></执行></执行><配置><schemaDirectory>src/main/resources/dir2</schemaDirectory><schemaIncludes><include>schema2.xsd</include></schemaIncludes><generatePackage>schema2.package</generatePackage></配置></插件></插件>

发生了什么:Maven 执行第一个插件.然后删除目标文件夹并创建第二个包,然后可见.

我尝试为第一个配置设置 target/somedir1,为第二个配置设置 target/somedir2.但行为不会改变?有任何想法吗?我不想直接在 src/main/java 文件夹中生成包,因为这些包是生成的,不应与手动创建的类混合.

解决方案

我不得不指定不同的 generateDirectory(如果没有这个,插件会认为文件是最新的并且在第二次执行期间没有生成任何东西).我建议对生成的源遵循 target/generated-sources/ 约定,以便它们将自动导入到您喜欢的 IDE 中.我还建议声明多个 execution 而不是声明两次插件(并将 configuration 移动到每个 execution 元素中):

<groupId>org.jvnet.jaxb2.maven2</groupId><artifactId>maven-jaxb2-plugin</artifactId><version>0.7.1</version><执行><执行><id>schema1-generate</id><目标><目标>生成</目标></目标><配置><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></配置></执行><执行><id>schema2-generate</id><目标><目标>生成</目标></目标><配置><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></配置></执行></执行></插件>

通过这个设置,我在 mvn clean compile

后得到以下结果<前>$树目标/目标/├── 类│ ├── com│ │ └── stackoverflow│ │ ├── App.class│ │ ├── package1│ │ │ ├── ObjectFactory.class│ │ │ ├── Shiporder.class│ │ │ ├── Shiporder$Item.class│ │ │ └── Shiporder$Shipto.class│ │ └── package2│ │ ├── BookForm.class│ │ ├── BooksForm.class│ │ ├── ObjectFactory.class│ │ └── package-info.class│ ├── dir1│ │ └── shiporder.xsd│ └── dir2│ └── book.xsd└── 生成源├── xjc│ └── META-INF│ └── sun-jaxb.episode├── xjc1│ └── com│ └── stackoverflow│ └── package1│ ├── ObjectFactory.java│ └── Shiporder.java└── xjc2└── com└── stackoverflow└── 套餐2├── BookForm.java├── BooksForm.java├── ObjectFactory.java└── package-info.java

这似乎是预期的结果.

Example:

</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>

What happened: Maven executes the the first plugin. Then deletes the target folder and creates the second package, which then is visible.

I tried to set target/somedir1 for the first configuration and target/somedir2 for the second configuration. But the behavior does not not change? Any ideas? I do not want to generate the packages directly in the src/main/java folder, because these packages are genereated and should not be mixed with manual created classes.

解决方案

I had to specify different generateDirectory (without this, the plugin was considering that files were up to date and wasn't generating anything during the second execution). And I recommend to follow the target/generated-sources/<tool> convention for generated sources so that they will be imported in your favorite IDE automatically. I also recommend to declare several execution instead of declaring the plugin twice (and to move the configuration inside each execution element):

<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>

With this setup, I get the following result after a mvn clean compile

$ tree target/
target/
├── classes
│   ├── com
│   │   └── stackoverflow
│   │       ├── App.class
│   │       ├── package1
│   │       │   ├── ObjectFactory.class
│   │       │   ├── Shiporder.class
│   │       │   ├── Shiporder$Item.class
│   │       │   └── Shiporder$Shipto.class
│   │       └── package2
│   │           ├── BookForm.class
│   │           ├── BooksForm.class
│   │           ├── ObjectFactory.class
│   │           └── package-info.class
│   ├── dir1
│   │   └── shiporder.xsd
│   └── dir2
│       └── books.xsd
└── generated-sources
    ├── 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

Which seems to be the expected result.

这篇关于如何告诉 jaxb/Maven 生成多个模式包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆