使用Maven构建企业应用程序

时间:2012-07-16 12:33:05

标签: java maven

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

我有一个申请。之前的建筑是使用ANT制作的。现在我正在将它迁移到Maven。我想制作.ear文件。应用程序是WSDL优先的Web服务,因为我使用的是cxf-codegen-plugin Maven插件。代码生成成功,除了如何生成生成源的.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构建该目标序列?

2 个答案:

答案 0 :(得分: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​​zzWSEAR pom.xml文件构建所有内容并打包到.ear文件。希望这些信息对某人有用。

答案 1 :(得分: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>