Maven技术分享(1) 标准maven项目的搭建

Maven技术分享(一) 标准maven项目的搭建
1、 建立目录结构如下:


aggregator:负责聚合各个maven工程;

SDS_BASEDATA:  某个web子工程,将要被合并到SDS_PLATFORM这个web工程中。

SDS_PLATFORM:  负责收集各个web子工程,将要被发布到tomcat中运行。


2、各个工程的pom文件介绍

aggregator工程的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>

  <groupId>com.victorysoft.sds</groupId>

  <artifactId>aggregator</artifactId>

  <version>1.0.0</version>

  <packaging>pom</packaging>

  <name>aggregator project</name>

  

  <distributionManagement>

       <snapshotRepository>

              <id>user-snapshots</id>

              <name>User Project SNAPSHOTS</name>

  <url>http://localhost:8081/nexus/content/repositories/user_snapshot/</url>

       </snapshotRepository>

       <repository>

              <id>user-releases</id>

              <name>User Project Release</name>

  <url>http://localhost:8081/nexus/content/repositories/user_release/</url>

       </repository>

  </distributionManagement>

  

  <modules>

       <module>../basedata</module>

       <module>../platform</module>

</modules>

  

</project>


 

被合并的工程SDS_BASEDATA的pom配置


<?xml version="1.0"?>

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    

  <modelVersion>4.0.0</modelVersion>

  

       <parent>

    <groupId>com.victorysoft.sds</groupId>

    <artifactId>aggregator</artifactId>

    <version>1.0.0</version>

    <relativePath>../aggregator</relativePath>

  </parent>  

  

  <groupId>com.victorysoft.sds</groupId>

  <artifactId>basedata</artifactId>

  <version>1.0.0</version>

  <packaging>war</packaging>

  <name>sds-basedata</name>

  

  <build>

    <finalName>sds-basedata</finalName>

       

    <plugins>

       <plugin>

                           <groupId>org.apache.maven.plugins</groupId>

                           <artifactId>maven-war-plugin</artifactId>

                           <version>2.1-beta-1</version>

                           <configuration>

<!-- 忽略 web.xml 强制要求 -->

                                  <failOnMissingWebXml>false</failOnMissingWebXml>

<!-- 指定 源代码和页面的路径 -->                               

<webappDirectory>

                                         target/${artifactId}

                                  </webappDirectory>

                                  <warSourceDirectory>

                                         WebRoot

                                  </warSourceDirectory>

                                  <webResources>

                                         <resource>

                                                <directory>WebRoot\WEB-INF</directory>

                                                <targetPath>WEB-INF</targetPath>

                                         </resource>

                                  </webResources>

                           </configuration>

                     </plugin>

                     

                     <plugin>

      <groupId>org.codehaus.cargo</groupId>

      <artifactId>cargo-maven2-plugin</artifactId>

      <version>1.3.2</version>

      <configuration>

<!-- 对于该工程不执行cargo自动发布,该工程内容被合并主web工程platform  -->

                           <skip>true</skip>

      </configuration>

     </plugin>

    </plugins>

  </build>

  

</project>


 

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

    <groupId>com.victorysoft.sds</groupId>

    <artifactId>aggregator</artifactId>

    <version>1.0.0</version>

    <relativePath>../aggregator</relativePath>

  </parent>

  

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.victorysoft.sds</groupId>

  <artifactId>platform</artifactId>

  <version>1.0.0</version>

  <packaging>war</packaging>

  <name>platform project</name>

 

  <build>

    <plugins>

                     <plugin>  

         <groupId>org.apache.maven.plugins</groupId>  

         <artifactId>maven-war-plugin</artifactId>  

         <version>2.1-beta-1</version>

         <configuration>  

<!--指定 源代码和页面的路径 -->

                     <webappDirectory>

                                         target/${artifactId}

                                  </webappDirectory>

                                  <warSourceDirectory>

                                         WebRoot

                                  </warSourceDirectory>

                                  <webResources>

                                         <resource>

                                                <directory>WebRoot\WEB-INF</directory>

                                                <targetPath>WEB-INF</targetPath>

                                                <filtering>true</filtering>

                                                <includes>

                                                       <include>web.xml</include>

                                                </includes>

                                         </resource>

                                  </webResources>

       <!-- 指定被合并的工程 -->                 

          <overlays>  

              <overlay>  

                  <groupId>com.victorysoft.sds</groupId>  

                  <artifactId>basedata</artifactId>  

              </overlay>  

          </overlays>  

         </configuration>  

     </plugin>

     

     <plugin>

      <groupId>org.codehaus.cargo</groupId>

      <artifactId>cargo-maven2-plugin</artifactId>

      <version>1.3.2</version>

<!-- 设置远程tomcat的参数 -->

      <configuration>

                           <wait>true</wait>

        <!-- Container configuration -->

        <container>

          <containerId>tomcat6x</containerId>

          <type>remote</type>

              <timeout>600000</timeout>

        </container>

 

        <!-- Configuration to use with the container -->

        <configuration>

          <type>runtime</type>

          <properties>

              <cargo.tomcat.manager.url>http://127.0.0.1:8080/manager/text</cargo.tomcat.manager.url>

            <cargo.hostname>127.0.0.1</cargo.hostname>

            <cargo.servlet.port>8080</cargo.servlet.port>

            <cargo.remote.username>tomcat</cargo.remote.username>

            <cargo.remote.password>tomcat</cargo.remote.password>

          </properties>

        </configuration>

 

        <!-- Deployer configuration -->

        <deployer>

          <type>remote</type>

          <!-- Deployables configuration -->

               <deployables>

                 <deployable>

                   <groupId>com.victorysoft.sds</groupId>

                   <artifactId>platform</artifactId>

                   <type>war</type>

                   <properties>

                     <context>/platform</context>

                   </properties>

                 </deployable>

               </deployables>

        </deployer>

 

      </configuration>

<!-- 绑定maven中的工程生命周期 -->      

<executions>

                           <execution>

                                  <id>redeploy</id>

                                  <phase>deploy</phase>

                                  <goals>

                                         <goal>redeploy</goal>

                                  </goals>

                           </execution>

                     </executions> 

    </plugin>

    </plugins>

   </build>   

   

<!-- 合并工程时,需要在dependencies中指定被合并的工程 -->

   <dependencies>  

             <dependency>  

                 <groupId>com.victorysoft.sds</groupId>  

            <artifactId>basedata</artifactId>   

                 <version>1.0.0</version>  

                 <type>war</type>  

             </dependency>  

                           

         </dependencies> 

         

</project>




3、maven 的setting.xml配置

首先cargo插件不是官方的,所以需要在settings.xml里配置。


<pluginGroups> 

    <pluginGroup>org.codehaus.cargo</pluginGroup> 

</pluginGroups>


4、配置tomcat

为tomcat增加一个管理员角色的用户


<?xml version='1.0' encoding='utf-8'?>

<tomcat-users>

  <role rolename="manager"/>

  <user username="tomcat" password="tomcat" roles="manager"/>

</tomcat-users>


5、运行maven

mvn clean deploy

问题:

1)cargo的cargo-core-api-container、cargo-core-api-module在网络不稳定、无法连接maven服务器时,需要单独下载并安装到maven的nexus资源库中。
2)mvn clean cargo:redploy命令会将工程部署到tomcat中,但是当pom文件执行完毕后,工程会从tomcat中移走。

1、 建立目录结构如下:


aggregator:负责聚合各个maven工程;

SDS_BASEDATA:  某个web子工程,将要被合并到SDS_PLATFORM这个web工程中。

SDS_PLATFORM:  负责收集各个web子工程,将要被发布到tomcat中运行。


2、各个工程的pom文件介绍

aggregator工程的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>

  <groupId>com.victorysoft.sds</groupId>

  <artifactId>aggregator</artifactId>

  <version>1.0.0</version>

  <packaging>pom</packaging>

  <name>aggregator project</name>

  

  <distributionManagement>

       <snapshotRepository>

              <id>user-snapshots</id>

              <name>User Project SNAPSHOTS</name>

  <url>http://localhost:8081/nexus/content/repositories/user_snapshot/</url>

       </snapshotRepository>

       <repository>

              <id>user-releases</id>

              <name>User Project Release</name>

  <url>http://localhost:8081/nexus/content/repositories/user_release/</url>

       </repository>

  </distributionManagement>

  

  <modules>

       <module>../basedata</module>

       <module>../platform</module>

</modules>

  

</project>


 

被合并的工程SDS_BASEDATA的pom配置


<?xml version="1.0"?>

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    

  <modelVersion>4.0.0</modelVersion>

  

       <parent>

    <groupId>com.victorysoft.sds</groupId>

    <artifactId>aggregator</artifactId>

    <version>1.0.0</version>

    <relativePath>../aggregator</relativePath>

  </parent>  

  

  <groupId>com.victorysoft.sds</groupId>

  <artifactId>basedata</artifactId>

  <version>1.0.0</version>

  <packaging>war</packaging>

  <name>sds-basedata</name>

  

  <build>

    <finalName>sds-basedata</finalName>

       

    <plugins>

       <plugin>

                           <groupId>org.apache.maven.plugins</groupId>

                           <artifactId>maven-war-plugin</artifactId>

                           <version>2.1-beta-1</version>

                           <configuration>

<!-- 忽略 web.xml 强制要求 -->

                                  <failOnMissingWebXml>false</failOnMissingWebXml>

<!-- 指定 源代码和页面的路径 -->                               

<webappDirectory>

                                         target/${artifactId}

                                  </webappDirectory>

                                  <warSourceDirectory>

                                         WebRoot

                                  </warSourceDirectory>

                                  <webResources>

                                         <resource>

                                                <directory>WebRoot\WEB-INF</directory>

                                                <targetPath>WEB-INF</targetPath>

                                         </resource>

                                  </webResources>

                           </configuration>

                     </plugin>

                     

                     <plugin>

      <groupId>org.codehaus.cargo</groupId>

      <artifactId>cargo-maven2-plugin</artifactId>

      <version>1.3.2</version>

      <configuration>

<!-- 对于该工程不执行cargo自动发布,该工程内容被合并主web工程platform  -->

                           <skip>true</skip>

      </configuration>

     </plugin>

    </plugins>

  </build>

  

</project>


 

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

    <groupId>com.victorysoft.sds</groupId>

    <artifactId>aggregator</artifactId>

    <version>1.0.0</version>

    <relativePath>../aggregator</relativePath>

  </parent>

  

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.victorysoft.sds</groupId>

  <artifactId>platform</artifactId>

  <version>1.0.0</version>

  <packaging>war</packaging>

  <name>platform project</name>

 

  <build>

    <plugins>

                     <plugin>  

         <groupId>org.apache.maven.plugins</groupId>  

         <artifactId>maven-war-plugin</artifactId>  

         <version>2.1-beta-1</version>

         <configuration>  

<!--指定 源代码和页面的路径 -->

                     <webappDirectory>

                                         target/${artifactId}

                                  </webappDirectory>

                                  <warSourceDirectory>

                                         WebRoot

                                  </warSourceDirectory>

                                  <webResources>

                                         <resource>

                                                <directory>WebRoot\WEB-INF</directory>

                                                <targetPath>WEB-INF</targetPath>

                                                <filtering>true</filtering>

                                                <includes>

                                                       <include>web.xml</include>

                                                </includes>

                                         </resource>

                                  </webResources>

       <!-- 指定被合并的工程 -->                 

          <overlays>  

              <overlay>  

                  <groupId>com.victorysoft.sds</groupId>  

                  <artifactId>basedata</artifactId>  

              </overlay>  

          </overlays>  

         </configuration>  

     </plugin>

     

     <plugin>

      <groupId>org.codehaus.cargo</groupId>

      <artifactId>cargo-maven2-plugin</artifactId>

      <version>1.3.2</version>

<!-- 设置远程tomcat的参数 -->

      <configuration>

                           <wait>true</wait>

        <!-- Container configuration -->

        <container>

          <containerId>tomcat6x</containerId>

          <type>remote</type>

              <timeout>600000</timeout>

        </container>

 

        <!-- Configuration to use with the container -->

        <configuration>

          <type>runtime</type>

          <properties>

              <cargo.tomcat.manager.url>http://127.0.0.1:8080/manager/text</cargo.tomcat.manager.url>

            <cargo.hostname>127.0.0.1</cargo.hostname>

            <cargo.servlet.port>8080</cargo.servlet.port>

            <cargo.remote.username>tomcat</cargo.remote.username>

            <cargo.remote.password>tomcat</cargo.remote.password>

          </properties>

        </configuration>

 

        <!-- Deployer configuration -->

        <deployer>

          <type>remote</type>

          <!-- Deployables configuration -->

               <deployables>

                 <deployable>

                   <groupId>com.victorysoft.sds</groupId>

                   <artifactId>platform</artifactId>

                   <type>war</type>

                   <properties>

                     <context>/platform</context>

                   </properties>

                 </deployable>

               </deployables>

        </deployer>

 

      </configuration>

<!-- 绑定maven中的工程生命周期 -->      

<executions>

                           <execution>

                                  <id>redeploy</id>

                                  <phase>deploy</phase>

                                  <goals>

                                         <goal>redeploy</goal>

                                  </goals>

                           </execution>

                     </executions> 

    </plugin>

    </plugins>

   </build>   

   

<!-- 合并工程时,需要在dependencies中指定被合并的工程 -->

   <dependencies>  

             <dependency>  

                 <groupId>com.victorysoft.sds</groupId>  

            <artifactId>basedata</artifactId>   

                 <version>1.0.0</version>  

                 <type>war</type>  

             </dependency>  

                           

         </dependencies> 

         

</project>





3、maven 的setting.xml配置

首先cargo插件不是官方的,所以需要在settings.xml里配置。


<pluginGroups> 

    <pluginGroup>org.codehaus.cargo</pluginGroup> 

</pluginGroups>


 

4、配置tomcat

为tomcat增加一个管理员角色的用户


<?xml version='1.0' encoding='utf-8'?>

<tomcat-users>

  <role rolename="manager"/>

  <user username="tomcat" password="tomcat" roles="manager"/>

</tomcat-users>





5、运行maven

mvn clean deploy




问题:

1)cargo的cargo-core-api-container、cargo-core-api-module在网络不稳定、无法连接maven服务器时,需要单独下载并安装到maven的nexus资源库中。
2)mvn clean cargo:redploy命令会将工程部署到tomcat中,但是当pom文件执行完毕后,工程会从tomcat中移走。
本文章由Android培训 上海JAVA培训   推荐阅读