How to specify the JAXB version in maven-jaxb2-plu

2019-03-29 22:33发布

问题:

I need to use the latest version jaxb: 2.2.4-1, but maven or maven-jaxb2-plugin seems to pick up the one from the JDK.

I tried specifying the version like this:

<configuration>
    <specVersion>2.2</specVersion>
    ...
</configuration>

but the logs read:

[INFO] [jaxb2:generate {execution: common}]
[INFO] Started execution.
[INFO] JAXB API is loaded from the [jar:file:/opt/jdk1.6.0_24/jre/lib/rt.jar!].
[INFO] Detected JAXB API version [2.1].

I tried to add dependencies to the correct versions of javax.xml.bind:jaxb-api and com.sun.xml.bind:jaxb-impl, but that didn't help.

<plugins>
    <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.8.0</version>

        <dependencies>
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.2.4</version>
            </dependency>

            <dependency>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-impl</artifactId>
                <version>2.2.4-1</version>
            </dependency>
        </dependencies>

        <executions>
            <execution>
                <id>common</id>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <specVersion>2.2</specVersion>
                    ...
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

I also tried using maven-jaxb22-plugin but it also didn't work.

回答1:

The following code is adapted from the default webapp that netbeans generates. It uses the dependency plugin to copy the jars to a temporary folder and specifies this folder as the endorsed directory to the compiler so it overrides the implementation in the jdk.

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
</properties>

...

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax.xml.bind</groupId>
                                <artifactId>jaxb-api</artifactId>
                                <version>2.2.4</version>
                                <type>jar</type>
                            </artifactItem>
                            <artifactItem>
                                <groupId>com.sun.xml.bind</groupId>
                                <artifactId>jaxb-impl</artifactId>
                                <version>2.2.4-1</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>


回答2:

I attempted to use Jörn's solution, but it looks like maven-jaxb2-plugin went ahead and used the rt.jar version anyway, as I got the telling message from the plugin: [INFO] JAXB API is loaded from the [jar:file:/C:/jdk1.6.0_25/jre/lib/rt.jar!].

My unsuccessful version of the solution is slightly different in how it uses the dependency plugin, but that's the one part of the build that succeeds...

<properties>
  <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
  <v.jaxb2-api>2.2.4</v.jaxb2-api>
  <v.jaxb2-impl>2.2.4-1</v.jaxb2-impl>
  <v.jaxb2-xjc>2.2.4-1</v.jaxb2-xjc>
  <v.jaxb2-basics-jaxb>2.1.13.MR2</v.jaxb2-basics-jaxb>
</properties>
...
<build>
  <plugins>
    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-dependencies</id>
          <phase>validate</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <outputDirectory>${endorsed.dir}</outputDirectory>
            <excludeTransitive>true</excludeTransitive>
            <includeArtifactIds>jaxb-api,jaxb-impl</includeArtifactIds>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>                        
        <source>1.6</source>
        <target>1.6</target>
        <fork>true</fork>
        <meminitial>256m</meminitial>
        <maxmem>768m</maxmem>
        <compilerArguments>
          <endorseddirs>${endorsed.dir}</endorseddirs>
        </compilerArguments>
      </configuration>
    </plugin>
  </plugins> 
</build>


回答3:

hey just I want to save time of the people.
for the people who work on jaxb-impl, the version jaxb-impl 2.2.4-1 that meant to fix a bug of the version 2.2.4, the pom of istack-commons-runtime under the META-INF Folder contain a reference to its parent pom 2.4-SNAPSHOT when it should be jsut 2.4, due this version isn't a snapshot.

<parent>
    <groupId>com.sun.istack</groupId>
    <artifactId>istack-commons</artifactId>
    <version>2.4-SNAPSHOT</version>
</parent>

so if you don't want to work with snapshot you will smash with this error, unless you want to add everything on your local repository you may have to update manually this version into the pom on the jar. cheers, Manuel.