英文:
jaxb2-maven-plugin Jakarta and java 11
问题
package org.ttt.raw.reporting.core.fussnote;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlType;
/**
* Java-Klasse für FussnoteType complex type.
*
* Das folgende Schemafragment gibt den erwarteten Content an, der in dieser Klasse enthalten ist.
*
* <complexType name="FussnoteType">
* <complexContent>
*
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
*
* <sequence>
*
* <element name="FussnoteNummer" type="{http://www.w3.org/2001/XMLSchema}string"/>
*
* <element name="FussnoteText" type="{http://www.w3.org/2001/XMLSchema}string"/>
*
* </sequence>
*
* </restriction>
* </complexContent>
* </complexType>
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FussnoteType", propOrder = {
"fussnoteNummer",
"fussnoteText"
})
public class FussnoteType {
@XmlElement(name = "FussnoteNummer", required = true)
protected String fussnoteNummer;
@XmlElement(name = "FussnoteText", required = true)
protected String fussnoteText;
/**
* Ruft den Wert der fussnoteNummer-Eigenschaft ab.
*
* @return
* possible object is
* {@link String }
*
*/
public String getFussnoteNummer() {
return fussnoteNummer;
}
/**
* Legt den Wert der fussnoteNummer-Eigenschaft fest.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setFussnoteNummer(String value) {
this.fussnoteNummer = value;
}
/**
* Ruft den Wert der fussnoteText-Eigenschaft ab.
*
* @return
* possible object is
* {@link String }
*
*/
public String getFussnoteText() {
return fussnoteText;
}
/**
* Legt den Wert der fussnoteText-Eigenschaft fest.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setFussnoteText(String value) {
this.fussnoteText = value;
}
}
<dependencies>
<!-- Other dependencies -->
<!-- API, java.xml.bind module -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0-RC3</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Other plugins -->
<!-- JAXB2 Maven Plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>src/main/resources/reports/core/fussnote/xsd/fussnote.xsd</source>
</sources>
<packageName>org.ttt.raw.reporting.core.fussnote</packageName>
<outputDirectory>target/generated-sources</outputDirectory>
<clearOutputDir>false</clearOutputDir>
<xsdPathWithinArtifact>.</xsdPathWithinArtifact>
</configuration>
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0-RC3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Note: The provided code snippets have been translated according to your instructions. If you need any further assistance, feel free to ask.
英文:
I am trying to migrate to java 11 and jakarta. I need to create java classes from XSD Files as I did in Java EE. I have changed all jaxb imports into jakarta. But I can not make jaxb2 to create classes with jakarta namspace instead of jaxb.
If I add no dependencies to the plugin part then the class will be created with javax namspace, which I do not need and the imports can not be found anyway with Java error: package javax.xml.bind.annotation does not exist.
The generated class is:
package org.ttt.raw.reporting.core.fussnote;
import javax.xml.bind.annotation.XmlAccessType; // I need to have jakarta.xml.bind ... here!
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
Java-Klasse für FussnoteType complex type.
Das folgende Schemafragment gibt den erwarteten Content an, der in dieser Klasse enthalten ist.
<complexType name="FussnoteType">
<complexContent>
&lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
&lt;sequence&gt;
&lt;element name="FussnoteNummer" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
&lt;element name="FussnoteText" type="{http://www.w3.org/2001/XMLSchema}string"/&gt;
&lt;/sequence&gt;
&lt;/restriction&gt;
</complexContent>
</complexType>
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FussnoteType", propOrder = {
"fussnoteNummer",
"fussnoteText"
})
public class FussnoteType {
@XmlElement(name = "FussnoteNummer", required = true)
protected String fussnoteNummer;
@XmlElement(name = "FussnoteText", required = true)
protected String fussnoteText;
/**
* Ruft den Wert der fussnoteNummer-Eigenschaft ab.
*
* @return
* possible object is
* {@link String }
*
*/
public String getFussnoteNummer() {
return fussnoteNummer;
}
/**
* Legt den Wert der fussnoteNummer-Eigenschaft fest.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setFussnoteNummer(String value) {
this.fussnoteNummer = value;
}
/**
* Ruft den Wert der fussnoteText-Eigenschaft ab.
*
* @return
* possible object is
* {@link String }
*
*/
public String getFussnoteText() {
return fussnoteText;
}
/**
* Legt den Wert der fussnoteText-Eigenschaft fest.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setFussnoteText(String value) {
this.fussnoteText = value;
}
}
If I add jakarta.xml.bind
then the FussnoteType class will not be generated at all an I get these errors:
[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.5.0:xjc (xjc) on project raw-reporting-core: A Plug-in could not be loaded: "java.util.ServiceConfigurationError: com.sun.tools.xjc.Plugin: com.sun.tools.xjc.addon.accessors.PluginImpl Unable to get public no-arg constructor
[ERROR] at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:581)
[ERROR] at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:672)
[ERROR] at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1232)
[ERROR] at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1264)
[ERROR] at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1299)
[ERROR] at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1384)
[ERROR] at com.sun.tools.xjc.Options.findServices(Options.java:979)
[ERROR] at com.sun.tools.xjc.Options.getAllPlugins(Options.java:355)
[ERROR] at com.sun.tools.xjc.Options.parseArgument(Options.java:694)
[ERROR] at com.sun.tools.xjc.Driver$OptionsEx.parseArgument(Driver.java:502)
[ERROR] at com.sun.tools.xjc.Options.parseArguments(Options.java:827)
[ERROR] at com.sun.tools.xjc.Driver.run(Driver.java:231)
[ERROR] at org.codehaus.mojo.jaxb2.javageneration.AbstractJavaGeneratorMojo.performExecution(AbstractJavaGeneratorMojo.java:475)
[ERROR] at org.codehaus.mojo.jaxb2.AbstractJaxbMojo.execute(AbstractJaxbMojo.java:337)
[ERROR] at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
[ERROR] at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
[ERROR] at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
[ERROR] at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
[ERROR] at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
[ERROR] at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
[ERROR] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR] at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
[ERROR] at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
[ERROR] Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlAccessType
[ERROR] at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
[ERROR] at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3137)
[ERROR] at java.base/java.lang.Class.getConstructor0(Class.java:3342)
[ERROR] at java.base/java.lang.Class.getConstructor(Class.java:2151)
[ERROR] at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:659)
[ERROR] at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:656)
[ERROR] at java.base/java.security.AccessController.doPrivileged(Native Method)
[ERROR] at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:667)
[ERROR] ... 35 more
[ERROR] Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlAccessType
[ERROR] at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
[ERROR] at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
[ERROR] at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
[ERROR] at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
[ERROR] ... 43 more
The pom is:
<parent>
<groupId>org.ttt.baw.reporting</groupId>
<artifactId>parent-baw-reporting</artifactId>
<version>${revision}</version>
<relativePath>../parent-baw-reporting</relativePath>
</parent>
<artifactId>baw-reporting-core</artifactId>
<name>RAW-Reporting-Core</name>
<description>
- Technisches Modul
- Die technische Implementierung des Reportings
- Keine Fachlichkeit!
</description>
<dependencies>
<dependency>
<groupId>org.ttt.baw.reporting</groupId>
<artifactId>baw-config-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ttt.baw.reporting</groupId>
<artifactId>baw-io</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ttt.baw.reporting</groupId>
<artifactId>baw-results-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.ttt.swaw.pl</groupId>
<artifactId>swaw-pl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- API, java.xml.bind module -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0-RC3</version>
</dependency>
<!-- Tests -->
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- @see http://www.mojohaus.org/jaxb2-maven-plugin/Documentation/v2.2/example_xjc_basic.html -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>src/main/resources/reports/core/fussnote/xsd/fussnote.xsd</source>
</sources>
<packageName>org.ttt.baw.reporting.core.fussnote</packageName>
<outputDirectory>target/generated-sources</outputDirectory>
<clearOutputDir>false</clearOutputDir>
<xsdPathWithinArtifact>.</xsdPathWithinArtifact>
</configuration>
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0-RC3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
答案1
得分: 16
经过几天的研究,我针对与同一个插件相关的问题的基线解决方案如下:
pom.xml 文件
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<!-- maven compiler version -->
<maven.compiler.version>3.8.1</maven.compiler.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<version.maven.jakartaee>9.0.0</version.maven.jakartaee>
<version.maven.jakarta.xml.bind-api>3.0.0</version.maven.jakarta.xml.bind-api>
<version.maven.jakarta.ws.rs-api>3.0.0</version.maven.jakarta.ws.rs-api>
<version.maven.jakarta.activation.api>2.0.0</version.maven.jakarta.activation.api>
<version.maven.jaxb.impl>3.0.0</version.maven.jaxb.impl>
<version.maven.jaxb.xjc>3.0.0</version.maven.jaxb.xjc>
<version.maven.servlet.api>5.0.0</version.maven.servlet.api>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<xjbSources>
<xjbSource>src/main/resources/global.xjb</xjbSource>
</xjbSources>
<sources>
<source>src/main/resources/xsd</source>
</sources>
</configuration>
<dependencies>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>${version.maven.jaxb.xjc}</version>
</dependency>
<!-- other dependencies -->
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<!-- dependencies -->
</dependencies>
在 resources 文件夹中,我有一个 bindings.xjb 文件,还有一个名为 XSD 的文件夹,其中包含 XSD 模式。
global.xjb 文件
<jaxb:bindings xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2001/XMLSchema" version="3.0">
<jaxb:globalBindings>
<jaxb:serializable uid="1"/>
<!-- date time adapter -->
<jaxb:javaType name="java.util.Calendar" xmlType="xsi:dateTime"
parseMethod="jakarta.xml.bind.DatatypeConverter.parseDateTime"
printMethod="jakarta.xml.bind.DatatypeConverter.printDateTime" />
</jaxb:globalBindings>
<jaxb:bindings schemaLocation="xsd/schema.xsd">
<jaxb:schemaBindings>
<jaxb:package name="com.test.request" />
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
schema.xsd 文件
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://services.memorynotfound.com/" elementFormDefault="qualified">
<xs:complexType name="CourseRequest">
<xs:sequence>
<xs:element name="id" type="xs:int" nillable="false"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="CourseResponse">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="description" type="xs:string"/>
<xs:element name="created" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
英文:
After some days of research my baseline solution for a problem related to the same plugin is the following:
pom.xml file
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<!-- maven compiler version -->
<maven.compiler.version>3.8.1</maven.compiler.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<version.maven.jakartaee>9.0.0</version.maven.jakartaee>
<version.maven.jakarta.xml.bind-api>3.0.0</version.maven.jakarta.xml.bind-api>
<version.maven.jakarta.ws.rs-api>3.0.0</version.maven.jakarta.ws.rs-api>
<version.maven.jakarta.activation.api>2.0.0</version.maven.jakarta.activation.api>
<version.maven.jaxb.impl>3.0.0</version.maven.jaxb.impl>
<version.maven.jaxb.xjc>3.0.0</version.maven.jaxb.xjc>
<version.maven.servlet.api>5.0.0</version.maven.servlet.api>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.5.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<xjbSources>
<xjbSource>src/main/resources/global.xjb</xjbSource>
</xjbSources>
<sources>
<source>src/main/resources/xsd</source>
</sources>
</configuration>
<dependencies>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>${version.maven.jaxb.xjc}</version>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>${version.maven.jakarta.activation.api}</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>${version.maven.jakarta.xml.bind-api}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${version.maven.jaxb.impl}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>${version.maven.jakarta.xml.bind-api}</version>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>${version.maven.jakarta.ws.rs-api}</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>${version.maven.servlet.api}</version>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>${version.maven.jakartaee}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${version.maven.jaxb.impl}</version>
</dependency>
</dependencies>
In the resources folder, I have a bindings.xjb file and another folder named XSD with the XSD schema.
global.xjb file
<jaxb:bindings xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2001/XMLSchema" version="3.0">
<jaxb:globalBindings>
<jaxb:serializable uid="1"/>
<!-- date time adapter -->
<jaxb:javaType name="java.util.Calendar" xmlType="xsi:dateTime"
parseMethod="jakarta.xml.bind.DatatypeConverter.parseDateTime"
printMethod="jakarta.xml.bind.DatatypeConverter.printDateTime" />
</jaxb:globalBindings>
<jaxb:bindings schemaLocation="xsd/schema.xsd">
<jaxb:schemaBindings>
<jaxb:package name="com.test.request" />
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
schema.xsd file
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://services.memorynotfound.com/" elementFormDefault="qualified">
<xs:complexType name="CourseRequest">
<xs:sequence>
<xs:element name="id" type="xs:int" nillable="false"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="CourseResponse">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="description" type="xs:string"/>
<xs:element name="created" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论