【问题标题】:Build eclipse project with maven: jaxb xsd file not packaged使用maven构建eclipse项目:jaxb xsd文件未打包
【发布时间】:2022-01-29 07:56:19
【问题描述】:

我有一些 eclipse java 项目,我想更新 OSS 依赖项并使用 maven 重建。这些项目中有 maven pom.xml 文件。

项目中有一些XSD文件,如src/main/java/com/hht/appface/config/columnAccessSetting/xml/columnAccessAuth.xsd等。我认为它们是jaxb模式,下面是一个xsd文件的起始块供参考:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0">
    <xsd:annotation>
        <xsd:appinfo>
            <jaxb:globalBindings generateIsSetMethod="true" />
        </xsd:appinfo>
    </xsd:annotation>
    <xsd:element name="columnAccessSetting">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="customers" minOccurs="1" maxOccurs="1">
.....

正如我之前所说,我想使用 maven 构建项目。但是,如果我使用 maven 使用 mvn clean install 之类的东西构建项目,则 xsd 文件不会打包在 war 文件中。为此,构建的包不可运行。

下面是读取xsd文件的sn-p代码:

Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
URL url = getClass().getClassLoader().getResource("com/hht/appface/config/columnAccessSetting/xml/columnAccessAuth.xsd");
SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = sf.newSchema(url);
unmarshaller.setSchema(schema);

因此程序因 java 空指针异常而停止,因为包中没有要加载的 xsd 文件。

如果是 eclipse 构建的,xsd 文件会正确包含在包中。

尝试 1:

经过一些谷歌搜索后,我偶然发现了 jaxb2-maven 插件。我尝试如下配置maven pom

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.5.0</version>
    <executions>
        <execution>
            <id>schemagen</id>
            <goals>
                <goal>schemagen</goal>
            </goals>
        </execution>
    </executions>
</plugin>

但这给了我错误:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.128 s
[INFO] Finished at: 2022-01-29T16:04:31+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.5.0:schemagen (schemagen) on project AppFace: Execution schemagen of goal org.codehaus.mojo:jaxb2-maven-plugin:2.5.0:schemagen failed: syntax error @[1,1] in file:/G:/manual/AppFace_3.3.2/src/main/java/com/hht/appface/config/columnAccessSetting/xml/columnAccessAuth.xsd -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException

尝试 2:

换了另一个插件,pom如下:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.14.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <schemaDirectory>src/main/java</schemaDirectory>
        <schemaIncludes>
            <include>*.xsd</include>
        </schemaIncludes>
    </configuration>
</plugin>

日志显示如下,xsd文件未打包:

[INFO] --- maven-jaxb2-plugin:0.14.0:generate (default) @ AppFace ---
[WARNING] No schemas to compile. Skipping XJC execution.

pom.xml

构建两个pom.xml部分供参考

<build>
    <finalName>AppFace</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

其他细节:

项目在 Java 8,spring boot 应用程序上。 war文件将主要部署在tomcat中。

我对 maven 了解不多,而 jaxb/xsd 对我来说是全新的。我一直在寻找 2 天没有任何有意义的进展,所以任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 首先将XSD模式放入src/main/xsd第二次删除maven-jaxb2-plugin的配置只保留目标部分......此外,你使用spring boot parent?另一件事是,如果您想定义像surefire/resources/compiler 插件这样的插件。然后使用它们的最新版本 maven-compiler-plugin:3.9.0、maven-surefire-plugin:2.22.2(或 3.0.0-M5)、maven-resources-plugin:3.2.0。从 maven-compiler-plugin 中删除 skipTest 部分,因为它不存在。您使用哪个 Maven 版本?
  • 谢谢。那么,我会将 XSD 架构放在像 src/main/xsd/com/hht/appface/config/columnAccessSetting/xml/columnAccessAuth.xsd 这样的文件夹中吗? Spring 框架包如 spring-webmvc、spring-tx、spring-tx 版本为 4.3.30.RELEASE,spring security 版本为 4.2.3.RELEASE,Maven 版本:3.8.4,java 版本:Temurin build 1.8.0_322-b06。
  • 试过把XSD放到src/main/xsd/columnAccessAuth.xsdsrc/main/xsd/com/hht/appface/config/columnAccessSetting/xml/columnAccessAuth.xsd下,都没有把文件放到包里。

标签: java eclipse maven xsd jaxb


【解决方案1】:

我认为您需要做的就是将您的 XSD 文件放在src/main/resources 下。这是 maven 查找要与您的类打包的文件的地方。

哦...顺便说一下,当您调用 getResource() 时,文件名以“/”开头,否则 Java 将从您的类的包开始搜索。

【讨论】:

    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多