使用 Maven 包含 JAXB [英] Include JAXB using Maven
问题描述
在我的 1.6.0_16 JDK 上工作,我使用 Apache CXF 2.5.2 从 WSDL 生成了我的存根类,它使用最新的 jaxb-api 2.2.我知道可以让它使用 jaxb-api 2.1,但为了避免兼容性问题,我宁愿让它使用当前版本.由于我的 JDK 具有 jaxb 2.1,因此构建失败并显示以下消息:
@XmlElementRef 错误(name = "protocol", namespace = "urn:ch.beo.emc", type = JAXBElement.class, required = false)
因此,我尝试使用以下依赖项使 maven 包含最新的 jaxb api 和 impl.
<依赖><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.2.5</version></依赖><依赖><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId><version>2.2.5</version></依赖>
虽然这两个 jar 已添加到 Eclipse 中的 Maven 依赖项中,但错误消息在 Eclipse 和 Maven 构建中仍然存在.
如何将这些 jars 包含在我的 Maven 构建中并让它们在 Eclipse 和目标系统中使用?
<小时>PS:请在此处找到完整的 POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>ch.beo</groupId><artifactId>emc.front</artifactId><version>3.1.3-SNAPSHOT</version><存储库><存储库><id>jboss</id><name>JBoss</name><url>http://repository.jboss.org/maven2/</url></repository><存储库><id>freehep</id><name>Freehep</name><url>http://java.freehep.org/maven2</url></repository><存储库><id>JCurl</id><url>http://jcurl.berlios.de/m2/repo</url></repository><存储库><id>JavaNet</id><url>http://download.java.net/maven/2/</url></repository><存储库><id>djmaven2</id><url>http://www.fdvs.com.ar/djmaven2</url><name>DynamicJasper 公共存储库</name></repository></repositories><属性><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></属性><构建><defaultGoal>编译</defaultGoal><sourceDirectory>src/main/java</sourceDirectory><testSourceDirectory>src/test/java</testSourceDirectory><outputDirectory>目标/主</outputDirectory><testOutputDirectory>目标/测试</testOutputDirectory><资源><资源><targetPath>ch/beo/emc/front/resources</targetPath><目录>src/main/resources</directory><排除><exclude>src/main/resources/security/jarsign</exclude></排除></资源></资源><测试资源><测试资源><targetPath>ch/beo/emc/front/resources</targetPath><目录>src/test/resources</directory></testResource><测试资源><targetPath>ch/beo/emc/front/resources</targetPath><目录>src/main/resources</directory><排除><exclude>src/main/resources/security/jarsign</exclude></排除></testResource></testResources><插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><配置><来源>1.6</来源><目标>1.6</目标></配置></插件><插件><groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><version>1.1</version><配置><complianceLevel>1.6</complianceLevel><方面库><方面库><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId></aspectLibrary></aspectLibraries></配置><执行><执行><目标><目标>编译</目标><目标>测试编译</目标></目标></执行></执行></插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><配置><存档><清单><mainClass>ch.beo.emc.front.factory.Front</mainClass><addClasspath>true</addClasspath><classpathPrefix>lib</classpathPrefix></清单></归档><描述符><descriptor>assembly.xml</descriptor></描述符></配置><执行><执行><id>make-assembly</id><phase>包</phase><目标><目标>单</目标></目标></执行></执行></插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><配置><包括><include>ch/beo/emc/front/AllTests.java</include></包括></配置></插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>1.4</version><执行><执行><phase>包</phase><目标><目标>阴影</目标></目标><配置><createDependencyReducedPom>false</createDependencyReducedPom><变形金刚><transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>ch.beo.emc.front.factory.Front</mainClass></变压器><transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"><resource>META-INF/spring.handlers</resource></变压器><transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"><resource>META-INF/spring.schemas</resource></变压器></变形金刚></配置></执行></执行></插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><执行><执行><id>符号</id><phase>包</phase><目标><目标>标志</目标></目标><配置><keystore>src/main/resources/security/jarsign/server.pfx</keystore><type>pkcs12</type><别名>beo-it.ch</别名><storepass>****</storepass><signedjar>${project.build.directory}/signed/${project.build.finalName}.jar</signedjar><verify>true</verify></配置></执行></执行></插件></插件></build><依赖项><依赖><groupId>com.l2fprod</groupId><artifactId>l2fprod-common-all</artifactId><version>6.9.1</version></依赖><依赖><groupId>org.swixml</groupId><artifactId>swixml</artifactId><version>1.5.144</version></依赖><依赖><groupId>net.java.balloontip</groupId><artifactId>balloontip</artifactId><version>1.1</version></依赖><依赖><groupId>org.swinglabs</groupId><artifactId>swingx</artifactId><version>1.6</version></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>2.5.6.SEC01</version></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>2.5.6.SEC01</version></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>2.5.6.SEC01</version></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>2.5.6.SEC01</version></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>2.5.6.SEC01</version></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>2.5.6.SEC01</version><范围>测试</范围><排除事项><排除><artifactId>junit</artifactId><groupId>junit</groupId></排除></排除项></依赖><依赖><groupId>jfree</groupId><artifactId>jfreechart</artifactId><version>1.0.9</version></依赖><依赖><groupId>junit</groupId><artifactId>junit</artifactId><version>4.4</version><范围>测试</范围></依赖><依赖><groupId>com.sun.java.jnlp</groupId><artifactId>jnlp</artifactId><version>6.0</version></依赖><依赖><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.3.1</version></依赖><依赖><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.5</version></依赖><依赖><groupId>org.jscience</groupId><artifactId>jsr-275</artifactId><version>0.8.0</version></依赖><依赖><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.7</version></依赖><依赖><groupId>net.sf.jasperreports</groupId><artifactId>jasperreports</artifactId><version>3.7.4</version><排除事项><排除><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId></排除><排除><groupId>commons-collections</groupId><artifactId>commons-collections</artifactId></排除><排除><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></排除><排除><groupId>jfree</groupId><artifactId>jcommon</artifactId></排除><排除><groupId>jfree</groupId><artifactId>jfreechart</artifactId></排除><排除><groupId>xml-apis</groupId><artifactId>xml-apis</artifactId></排除><排除><groupId>bouncycastle</groupId><artifactId>bcmail-jdk14</artifactId></排除><排除><groupId>bouncycastle</groupId><artifactId>bcprov-jdk14</artifactId></排除><排除><groupId>bouncycastle</groupId><artifactId>bctsp-jdk14</artifactId></排除><排除><artifactId>jdtcore</artifactId><groupId>eclipse</groupId></排除></排除项></依赖><依赖><groupId>org.eclipse.jdt.core.compiler</groupId><artifactId>ecj</artifactId><version>3.5.1</version></依赖><依赖><groupId>ar.com.fdvs</groupId><artifactId>DynamicJasper</artifactId><version>3.1.2</version><排除事项><排除><groupId>jasperreports</groupId><artifactId>jasperreports</artifactId></排除><排除><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></排除></排除项></依赖><依赖><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.2.5</version></依赖><依赖><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId><version>2.2.5</version></依赖></依赖项></项目>
我刚刚在 jaxb 上也遇到了这个问题;天哪,我爱 Maven(不是).这是我解决问题的方法.
添加中央仓库
<前><存储库><id>中央</id><url>http://repo.maven.apache.org/maven2/</url></repository>修改api和impl的版本
<前><依赖><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.2.7-SNAPSHOT</version></依赖><依赖><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId><version>2.2.5-b10</version></依赖>Working on my 1.6.0_16 JDK, I generated my stub classes from a WSDL using Apache CXF 2.5.2, which uses the most recent jaxb-api 2.2. I know it's possible to have it use jaxb-api 2.1, but in order to avoid compatibility issues, I would rather have it use the current version. Since my JDK features jaxb 2.1, the build fails with the following message:
error at @XmlElementRef(name = "protocol", namespace = "urn:ch.beo.emc", type = JAXBElement.class, required = false)
Therefore, I tried to make maven include the most recent jaxb api and impl using the following dependencies.
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.5</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.5</version>
</dependency>
While the two jars have been added to Maven Dependencies in Eclipse, the error message persists both in Eclipse and Maven build.
How can I include these jars in my Maven build and have them used both in Eclipse and on the target system?
PS: Please Find the complete POM here:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.beo</groupId>
<artifactId>emc.front</artifactId>
<version>3.1.3-SNAPSHOT</version>
<repositories>
<repository>
<id>jboss</id>
<name>JBoss</name>
<url>http://repository.jboss.org/maven2/</url>
</repository>
<repository>
<id>freehep</id>
<name>Freehep</name>
<url>http://java.freehep.org/maven2</url>
</repository>
<repository>
<id>JCurl</id>
<url>http://jcurl.berlios.de/m2/repo</url>
</repository>
<repository>
<id>JavaNet</id>
<url>http://download.java.net/maven/2/</url>
</repository>
<repository>
<id>djmaven2</id>
<url>http://www.fdvs.com.ar/djmaven2</url>
<name>DynamicJasper public Repository</name>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>compile</defaultGoal>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<outputDirectory>target/main</outputDirectory>
<testOutputDirectory>target/test</testOutputDirectory>
<resources>
<resource>
<targetPath>ch/beo/emc/front/resources</targetPath>
<directory>src/main/resources</directory>
<excludes>
<exclude>src/main/resources/security/jarsign</exclude>
</excludes>
</resource>
</resources>
<testResources>
<testResource>
<targetPath>ch/beo/emc/front/resources</targetPath>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<targetPath>ch/beo/emc/front/resources</targetPath>
<directory>src/main/resources</directory>
<excludes>
<exclude>src/main/resources/security/jarsign</exclude>
</excludes>
</testResource>
</testResources>
<plugins>
<plugin>
<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>aspectj-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<complianceLevel>1.6</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>ch.beo.emc.front.factory.Front</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
</manifest>
</archive>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>ch/beo/emc/front/AllTests.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>ch.beo.emc.front.factory.Front</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>sign</id>
<phase>package</phase>
<goals>
<goal>sign</goal>
</goals>
<configuration>
<keystore>src/main/resources/security/jarsign/server.pfx</keystore>
<type>pkcs12</type>
<alias>beo-it.ch</alias>
<storepass>****</storepass>
<signedjar>${project.build.directory}/signed/${project.build.finalName}.jar</signedjar>
<verify>true</verify>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.l2fprod</groupId>
<artifactId>l2fprod-common-all</artifactId>
<version>6.9.1</version>
</dependency>
<dependency>
<groupId>org.swixml</groupId>
<artifactId>swixml</artifactId>
<version>1.5.144</version>
</dependency>
<dependency>
<groupId>net.java.balloontip</groupId>
<artifactId>balloontip</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.swinglabs</groupId>
<artifactId>swingx</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.5.6.SEC01</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>2.5.6.SEC01</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>2.5.6.SEC01</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>2.5.6.SEC01</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>2.5.6.SEC01</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5.6.SEC01</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.java.jnlp</groupId>
<artifactId>jnlp</artifactId>
<version>6.0</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.jscience</groupId>
<artifactId>jsr-275</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>3.7.4</version>
<exclusions>
<exclusion>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</exclusion>
<exclusion>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>jfree</groupId>
<artifactId>jcommon</artifactId>
</exclusion>
<exclusion>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
</exclusion>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
<exclusion>
<groupId>bouncycastle</groupId>
<artifactId>bcmail-jdk14</artifactId>
</exclusion>
<exclusion>
<groupId>bouncycastle</groupId>
<artifactId>bcprov-jdk14</artifactId>
</exclusion>
<exclusion>
<groupId>bouncycastle</groupId>
<artifactId>bctsp-jdk14</artifactId>
</exclusion>
<exclusion>
<artifactId>jdtcore</artifactId>
<groupId>eclipse</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>ar.com.fdvs</groupId>
<artifactId>DynamicJasper</artifactId>
<version>3.1.2</version>
<exclusions>
<exclusion>
<groupId>jasperreports</groupId>
<artifactId>jasperreports</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.5</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.5</version>
</dependency>
</dependencies>
</project>
I just ran into this issue with jaxb also; goodness do I love Maven (not). Here's how I resolved the problem.
Add the central repo
<repository> <id>central</id> <url>http://repo.maven.apache.org/maven2/</url> </repository>
Modify the version of the api and impl
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.2.7-SNAPSHOT</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.5-b10</version> </dependency>
这篇关于使用 Maven 包含 JAXB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!