简体   繁体   English

使用 maven 构建 eclipse 项目:jaxb Z24788D7B62B899D08B0253802B8219 未打包文件

[英]Build eclipse project with maven: jaxb xsd file not packaged

I have some eclipse java project that I want to update the OSS dependencies and rebuild using maven.我有一些 eclipse java 项目,我想更新 OSS 依赖项并使用 maven 重建。 The projects have maven pom.xml files in them.这些项目中有 maven pom.xml文件。

There are some XSD files in the projects, like src/main/java/com/hht/appface/config/columnAccessSetting/xml/columnAccessAuth.xsd etc. I think they are jaxb schema, below is a the start block of an xsd file for reference: There are some XSD files in the projects, like src/main/java/com/hht/appface/config/columnAccessSetting/xml/columnAccessAuth.xsd etc. I think they are jaxb schema, below is a the start block of an xsd file以供参考:

<?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">
.....

As I stated before, I want to build the projects using maven.如前所述,我想使用 maven 构建项目。 But if I build the project using maven using something like mvn clean install , the xsd files are not packaged inside the war file.但是,如果我使用 maven 使用mvn clean install之类的东西构建项目,则 xsd 文件不会打包在 war 文件中。 For that the built package is not runnable.为此,内置的 package 不可运行。

Below is a snippet of code where xsd files are read:以下是读取 xsd 文件的代码片段:

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);

So the program stops with java null pointed exception, as there is no xsd files to load in the package.所以程序停止与 java null 指出异常,因为没有 xsd 文件加载到 ZEFE8B70A8E604E77A7

The xsd files are properly included in the package if it is built by eclipse. xsd 文件正确包含在 package 中,如果它是由 eclipse 构建的。

Tried 1:试过1:

After some google search I stumbled upon jaxb2-maven plugins.经过一些谷歌搜索后,我偶然发现了 jaxb2-maven 插件。 I tried to configure maven pom as follows我尝试如下配置 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>

But this gives me error:但这给了我错误:

[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

Tried 2:试过2:

Changed the another plugin, pom follows:换了另一个插件,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>

Log shows follows, and the xsd files are not pacakged:日志显示如下,xsd文件没有打包:

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

pom.xml pom.xml

Build section of two pom.xml for reference构建部分的两个 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>

Other detail:其他细节:

Projects are on Java 8, spring boot application.项目位于 Java 8、spring 启动应用程序上。 The war files will be mainly deployed in tomcat. war文件将主要部署在tomcat中。

I do not have that much knowledge about maven, and jaxb/xsd is brand new to me.我对 maven 了解不多,而 jaxb/xsd 对我来说是全新的。 I have been searching for 2 days without any meaningful progress, so any kind help will be greatly appreciated.我一直在寻找 2 天没有任何有意义的进展,所以任何帮助将不胜感激。

Thank you.谢谢你。

I think all you need to do is putting your XSD files under src/main/resources .我认为您需要做的就是将 XSD 文件放在src/main/resources下。 This is where maven looks for files to package with your classes.这是 maven 与您的类一起查找 package 文件的地方。

Oh... And by the way when you call getResource() start the filename with a '/' otherwise Java will start searching from the package of your class.哦...顺便说一下,当您调用getResource()时,文件名以“/”开头,否则 Java 将从 ZA2F2ED4F8EBC2CBB4C21A29DC40AB61D 的 package 开始搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM