简体   繁体   中英

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. The projects have maven pom.xml files in them.

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:

<?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. But if I build the project using maven using something like mvn clean install , the xsd files are not packaged inside the war file. For that the built package is not runnable.

Below is a snippet of code where xsd files are read:

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.

The xsd files are properly included in the package if it is built by eclipse.

Tried 1:

After some google search I stumbled upon jaxb2-maven plugins. I tried to configure maven pom as follows

<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:

Changed the another plugin, pom follows:

<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:

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

pom.xml

Build section of two pom.xml for reference

<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. The war files will be mainly deployed in tomcat.

I do not have that much knowledge about maven, and jaxb/xsd is brand new to me. I have been searching for 2 days without any meaningful progress, so any kind help will be greatly appreciated.

Thank you.

I think all you need to do is putting your XSD files under src/main/resources . This is where maven looks for files to package with your classes.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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