繁体   English   中英

在Maven-jaxb2-plugin的'src'中生成文件

[英]Generate files in 'src' for maven-jaxb2-plugin

我是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>

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

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

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

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

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

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

将给您一个小例子,使其更清楚。 我们将构建一个多模块项目。 我将使用xsd而不是wsdl来生成Java类,因为我有一个可用的cos,但是原理是相同的,只是在子模块中使用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>

父pom引用两个子模块:application和generate。 生成的模块具有以下用于类生成的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>

它的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>

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

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

我们将创建一个简单的main,以使用生成的源并对它进行演示:

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

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

您将看到您的项目成功构建,因为maven足够聪明,可以按正确的顺序进行构建。

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

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

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

暂无
暂无

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

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