首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在'src‘中为maven-jaxb2-plugin生成文件

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

Stack Overflow用户
提问于 2018-01-09 08:55:10
回答 2查看 9.6K关注 0票数 1

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

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

代码语言:javascript
复制
<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‘在目标/generatedsources/calculator/wsdl中创建源文件。

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

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

EN

回答 2

Stack Overflow用户

发布于 2018-01-24 00:04:34

正如其他用户所说,您应该创建一个不同的模块,它将具有生成的源代码,为了方便起见,让我们调用它。

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

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

我会给你一个小例子,让你更清楚。我们将构建一个多模块项目。我将使用xsd而不是wsdl来生成Java类,因为我有一个可用的类,但原理是相同的,只是在子模块中使用maven plugin来生成源代码。父pom将如下所示:

代码语言:javascript
复制
<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和generated。生成的模块具有以下用于类生成的xsd:

代码语言:javascript
复制
<?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看起来像这样:

代码语言:javascript
复制
<?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>

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

代码语言:javascript
复制
<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来使用生成的源代码,并演示它的工作方式:

代码语言:javascript
复制
public static void main( String[] args ) {
        Shiporder shiporder = new Shiporder();
        shiporder.setOrderid("Some orderId");
        System.out.println( shiporder.getOrderid() );
    }

从父目录运行(multimodule-demo):mvn全新安装

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

票数 5
EN

Stack Overflow用户

发布于 2018-01-09 14:45:31

您可以通过generateDirectory配置目标目录。请参见:

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

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48160097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档