Ste*_*ins 11 java jaxb java-ee maven spring-oxm
我有一个基于Maven的项目,我尝试添加一些由"jaxb2-maven-plugin"Maven插件自动生成的JAXB类.但是,我的第一个剪辑让我进入循环依赖循环:
似乎有两种明显的可能性来解决这个问题:
/target到/src/main/java,以便对它们的引用不会导致编译错误.我在这里错过了什么吗?选项#1看起来很荒谬......这不是人们使用JAXB的方式.选项#2似乎更合理,但仍然相当低效和繁琐.我真的不得不承担一个完全独立的项目的开销只是为了使用JAXB?
开发人员是否有更优雅的方法在Maven插件生成它们的同一个项目中引用JAXB生成的类?
更新: 根据要求,这是我的POM的相关部分:
<build>
<plugins>
<plugin>
<!-- configure the compiler to compile to Java 1.6 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- The name of your generated source package -->
<packageName>com.mypackage</packageName>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
当我运行时mvn clean package,我会看到我的JAXB源是在/target子目录下生成的.但是,这些生成的源不会自动添加到编译阶段的类路径中.
POST-RESOLUTION UPDATE: 事实证明我的编译问题更多地与我在Eclipse中运行的事实有关,而且它的Maven集成在"jaxb2-maven-plugin"中存在一些问题.有关该问题及其解决方案的更多详细信息,请参阅此StackOverflow问题.
你是如何配置你的jaxb maven插件的?通常它在生成源生命周期中运行,该生命周期在编译生命周期之前.因此,当您自己的代码被编译时,您的JAXB生成的类应该已存在,Maven将它们放在target/generated-source中并将该文件夹放在类路径上.
编辑:这是我在工作中使用的代码(并且按预期工作):
<plugin>
<groupId>com.sun.tools.xjc.maven2</groupId>
<artifactId>maven-jaxb-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/resources/<companyname>/xsd</schemaDirectory>
<includeSchemas>
<includeSchema>retrieval.xsd</includeSchema>
<includeSchema>storage.xsd</includeSchema>
</includeSchemas>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
显然我们使用另一个jaxb插件...(另见这个主题:Maven JAXB插件的区别).
小智 7
我建议你将jaxb生成的类(api)和你的BL类(实现)分成2个maven项目,每个项目使用单独的pom.xml,以及带有编译顺序的主根pom.xml.这样,您就可以构建api.jar,然后maven会将其安装在本地仓库中,之后您可以将其用作实现的依赖项.所以它看起来像:
-API\
--pom.xml - for api, jaxb generation
-IMPL\
--pom.xml - for impl, api dependency is here
pom.xml - main pom.xml with references to the projects above
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30550 次 |
| 最近记录: |