2012-07-16 108 views
3

我在學習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文件。創建工件的順序應該是:從wsdl生成代碼,編譯它,生成jar,構建war,構建。

如何使用Maven構建目標序列?

回答

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耳 - 企業應用 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文件。希望這些信息對某人有用。

+0

卻忽視關於版本的提示等 – khmarbaise 2012-11-21 12:47:57

1

以下是關於您所創建的POM的一些提示/建議/改進:

首先,你應該定義一個版本1.0快照而不是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>