3

我正在学习 Maven,我需要一些帮助来理解它是如何工作的。

我有一个应用程序。以前的建筑是使用 ANT 制作的。现在我将它迁移到 Maven。我想制作一个 .ear 文件。应用程序是 WSDL 优先的 Web 服务,因为我使用的是cxf-codegen-pluginMaven 插件。代码生成成功,除了如何制作生成源的 .jar 文件外,该部分一切正常。

是否可以仅使用一个 Maven 文件构建应用程序pom.xml?或者我需要单独的 Maven 文件来构建生成源的 .jar 文件,构建 .war 文件和其他pom.xml文件以最终构建 .ear 文件?

我的 Maven 文件如下所示:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.groupgti.ws.bizzbuzz</groupId>
    <artifactId>BizzBuzzWSEAR</artifactId>
    <version>1.0</version>

    <packaging>ear</packaging>

    <name>BizzBuzzWSEAR</name>

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

    <dependencies>
        <!-- All needed dependencies -->
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <includes>
                        <include>src/main/**</include>
                    </includes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <version>6</version>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>2.5.2</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>${basedir}/src/main/webapp/WEB-INF/wsdl/BizzBuzz.wsdl</wsdl>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

当我运行此脚本时,会创建 .ear 文件,但其中没有 .war 文件。创建工件的顺序应该是:generate-code-from-wsdl、compile-it、make-jar、build-war、build-ear。

如何使用 Maven 构建目标序列?

4

2 回答 2

2

经过一些尝试和测试,我设法自己做到了。

我创建了一个名为 BizzBu​​zzWSEAR 的项目,然后pom.xml为它创建了文件。它看起来像这样:

<?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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.groupgti.ws.bizzbuzz</groupId>
    <artifactId>BizzBuzzWSEAR</artifactId>
    <version>1.0</version>
    <modules>
        <module>BizzBuzz-web</module>
        <module>BizzBuzz-ear</module>
        <module>BizzBuzz-cxf</module>
    </modules>
    <packaging>pom</packaging>
    <name>BizzBuzzWSEAR</name>
</project>

然后我创建了其他三个模块:

BizzBu​​zz-web - 用于 Web 应用程序(Web 服务) BizzBu​​zz-ear - 用于企业应用程序 BizzBu​​zz-cxf - 用于 wsdl2java .jar 文件

BizzBu​​zz-web 模块pom.xml如下所示:

<?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>BizzBuzzWSEAR</artifactId>
        <groupId>com.groupgti.ws.bizzbuzz</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>BizzBuzz-web</artifactId>
    <groupId>com.groupgti.ws.bizzbuzz</groupId>
    <version>1.0</version>
    <packaging>war</packaging>

    <name>BizzBuzz-web</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.groupgti.ws.bizzbuzz</groupId>
            <artifactId>BizzBuzz-cxf</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>

BizzBu​​zz-ear 模块 pom.xml 文件如下所示:

<?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>BizzBuzzWSEAR</artifactId>
        <groupId>com.groupgti.ws.bizzbuzz</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>BizzBuzz-ear</artifactId>
    <groupId>com.groupgti.ws.bizzbuzz</groupId>
    <version>1.0</version>
    <packaging>ear</packaging>

    <name>BizzBuzz-ear</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
    </properties>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <version>6</version>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.groupgti.ws.bizzbuzz</groupId>
            <artifactId>BizzBuzz-web</artifactId>
            <version>1.0</version>
            <type>war</type>
        </dependency>
    </dependencies>

</project>

对于 BizzBu​​zz-cxf 模块pom.xml文件如下所示:

<?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>BizzBuzzWSEAR</artifactId>
        <groupId>com.groupgti.ws.bizzbuzz</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>BizzBuzz-cxf</artifactId>
    <groupId>com.groupgti.ws.bizzbuzz</groupId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>BizzBuzz-cxf</name>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>2.5.2</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>${basedir}/src/main/resources/BizzBuzz.wsdl</wsdl>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

然后,当我构建我的项目 BizzBu​​zzWSEARpom.xml文件时,将所有内容和包构建到 .ear 文件中。希望这些信息对某人有用。

于 2012-07-16T15:02:00.900 回答
1

以下是有关您创建的 pom 的一些提示/建议/改进:

首先,您应该定义一个版本 1.0-SNAPSHOT 而不是 1.0,因为您的应用程序正在开发中,尚未发布。

在这种情况下,您有一个多模块构建,它在使用中具有一些优势。您在每个模块中定义版本号和组 ID,如下所示:

<parent>
    <artifactId>BizzBuzzWSEAR</artifactId>
    <groupId>com.groupgti.ws.bizzbuzz</groupId>
    <version>1.0</version>
</parent>
<artifactId>BizzBuzz-web</artifactId>
<groupId>com.groupgti.ws.bizzbuzz</groupId>
<version>1.0</version>
<packaging>war</packaging>

在您的多模块构建中,您可以将其减少到以下(最佳实践,例如):

<parent>
    <artifactId>BizzBuzzWSEAR</artifactId>
    <groupId>com.groupgti.ws.bizzbuzz</groupId>
    <version>1.0</version>
</parent>

<artifactId>BizzBuzz-web</artifactId>
<packaging>war</packaging>

groupId 和版本将由父模块继承到使 pom 更小的模块。

此外,您应该放置全局配置,例如属性:

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

进入父 pom。除此之外,您可以为父级中的插件定义默认版本等,如下所示(父级):

  <build>
    <pluginManagement>
      <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <version>2.3.2</version>
           <configuration>
              <source>1.6</source>
              <target>1.6</target>
           </configuration>
       </plugin>

       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-ear-plugin</artifactId>
           <version>2.6</version>
           <configuration>
              <version>6</version>
              <defaultLibBundleDir>lib</defaultLibBundleDir>
           </configuration>
       </plugin>
     ...

这将导致以下耳模块 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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <artifactId>BizzBuzzWSEAR</artifactId>
        <groupId>com.groupgti.ws.bizzbuzz</groupId>
        <version>1.0</version>
    </parent>

    <artifactId>BizzBuzz-ear</artifactId>
    <packaging>ear</packaging>

    <name>BizzBuzz-ear</name>

    <dependencies>
        <dependency>
            <groupId>${project.goupId}</groupId>
            <artifactId>BizzBuzz-web</artifactId>
            <version>${project.version}</version>
            <type>war</type>
        </dependency>
    </dependencies>

</project>
于 2012-07-17T08:54:04.583 回答