在 'src' 中为 maven-jaxb2-plugin 生成文件

VMA*_*VMA 1 java soap maven maven-jaxb2-plugin spring-boot

我是 Spring Boot 的新手,不熟悉 SOAP Web 服务的工作原理。

在 pom 文件中我有以下配置:

<plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.13.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>calculator.wsdl</generatePackage>
                    <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                    <schemas>
                        <schema>
                            <url>http://www.dneonline.com/calculator.asmx?WSDL</url>
                        </schema>
                    </schemas>
                </configuration>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

'generatePackage' 在 target/ generatedsources/calculator/wsdl 中创建源文件。

我希望它们在“{$basedir}/src/java”中生成,以便我可以在代码中使用这些服务。我尝试将上述条目放入“generatePackage”标签中。但文件仍然在“目标”中生成。

任何帮助将不胜感激。谢谢!

lex*_*ore 5

您可以通过配置目标目录generateDirectory。看:

https://github.com/highsource/maven-jaxb2-plugin/wiki/Controlling-the-Output

但正如 Steve C 指出的那样,您不应该将生成的代码放在src/main/java. 将生成的代码与手动编写的代码分开。不要将生成的代码签入 VCS。


mar*_*art 5

正如其他用户所说,您应该创建一个不同的模块,其中将包含生成的源,为了方便起见,我们将其称为生成的。

然后您的“主”代码/模块(将使用生成的源的代码)将在其依赖项中引用生成

当maven运行它的生命周期时,它会根据定义的依赖关系为你确定首先构建哪些模块,并且你不会遇到编译问题。

给你一个小例子,让它更清楚。我们将构建一个多模块项目。将使用xsd而不是wsdl来生成Java类,因为我有一个可用的,但原理是相同的,只是在子模块中使用maven插件来生成源。父 pom 看起来像这样:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example.multimodule</groupId>
  <artifactId>multimodule-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>multimodule.demo</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <modules>
    <module>application</module>
    <module>generated</module>
  </modules>

</project>
Run Code Online (Sandbox Code Playgroud)

父 pom 引用两个子模块:application 和 generated。生成的模块具有以下用于类生成的 xsd:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="shiporder">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="orderperson" type="xs:string"/>
                <xs:element name="shipto">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="address" type="xs:string"/>
                            <xs:element name="city" type="xs:string"/>
                            <xs:element name="country" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="item" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string"/>
                            <xs:element name="note" type="xs:string" minOccurs="0"/>
                            <xs:element name="quantity" type="xs:positiveInteger"/>
                            <xs:element name="price" type="xs:decimal"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="orderid" type="xs:string" use="required"/>
        </xs:complexType>
    </xs:element>

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

它的 pom 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>multimodule.demo</artifactId>
        <groupId>com.example.multimodule</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.multimodule</groupId>
    <artifactId>generated</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <arguments>-Xequals -XhashCode -Xfluent-api</arguments>
                    <schemaDirectory>src/main/resources/xsd</schemaDirectory>
                    <bindingDirectory>src/main/resources/xjb</bindingDirectory>
                    <extension>true</extension>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-fluent-api</artifactId>
                        <version>3.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics</artifactId>
                        <version>0.6.4</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-runtime</artifactId>
            <version>0.9.0</version>
        </dependency>
    </dependencies>

</project>
Run Code Online (Sandbox Code Playgroud)

现在应用程序模块将引用生成的模块作为依赖项:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>multimodule.demo</artifactId>
        <groupId>com.example.multimodule</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.multimodule</groupId>
    <artifactId>application</artifactId>
    <packaging>jar</packaging>
    <name>application</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.example.multimodule</groupId>
            <artifactId>generated</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>
Run Code Online (Sandbox Code Playgroud)

我们将创建一个简单的 main 来使用生成的源并演示它的工作原理:

public static void main( String[] args ) {
        Shiporder shiporder = new Shiporder();
        shiporder.setOrderid("Some orderId");
        System.out.println( shiporder.getOrderid() );
    }
Run Code Online (Sandbox Code Playgroud)

从父目录运行(multimodule-demo): mvn clean install

您将看到您的项目构建成功,因为 Maven 足够智能,可以按照正确的顺序构建它们。