maven构建web应用

1.建立项目骨架:mvn archetype:create -DgroupId=com.zcms -DartifactId=zcms -DarchetypeArtifactId=maven-archetype-webapp
2.补全项目的目录结构,Maven标准项目布局结构:
mkdir -p src/{main/config,main/scripts,main/java/com/zmcs,test/java/com/zcms,test/resources,site}
touch LICENSE.txt NOTICE.txt READEME.txt
zcms
|
|---READEME.txt
|---NOTICE.txt
|---LICENSE.txt
|---pom.xml
|---src
|    |---main
|    |    |---scripts
|    |    |---config
|    |    |---resources
|    |    |---java
|    |    |    |---com
|    |    |    |    |---zmcs
|    |    |---webapp
|    |    |     |---index.jsp
|    |    |     |---WEB-INF
|    |    |     |      |---web.xml
|    |---site
|    |---test
|    |    |---resources
|    |    |---java
|    |    |    |---com
|    |    |    |    |---zcms
3.添加嵌入式服务器jetty6,便于开发和调试web项目,编辑pom.xml:
...
 <build>
    <finalName>zcms</finalName>
    <plugins>  
      <plugin>  
        <groupId>org.mortbay.jetty</groupId>  
        <artifactId>maven-jetty-plugin</artifactId>  
      </plugin>  
   </plugins>  
  </build>
...
4.测试运行:mvn jetty:run,浏览器中访问http://localhost:8080/zcms/,ctrl+c停止jetty
5.在pom.xml中添加spring,hibernate/ibatis,struts2/webwork2以及report,xdoclet,hibernate代码生成工具,DbUnit等
Maven的超级POM POM描述 POM详细解释:(下载)
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <!--父项目的位置
  <parent>
    <groupId/>
    <artifactId/>
    <version/>
    <relativePath/>
  </parent>-->
  <!-- The Basics -->
  <groupId>com.zcms</groupId>
  <artifactId>zcms</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>zcms</name>
  <description>cms</description>
  <url>http://www.zcms.com</url>
  <!--此项目开始年份,用四位整数指定。当产生版权信息时使用-->
  <inceptionYear>2012</inceptionYear>
  <!--组织-项目所属机构-->
  <organization>
    <name>发动</name>
    <url>http://www.fd.com</url>
  </organization>
  <!--许可证-->
  <licenses>
    <license>
      <name>The Apache Software License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      <distribution>repo</distribution>
      <comments>A business-friendly OSS license</comments>
    </license>
  </licenses>
  <!--项目开发小组-->
  <developers>
    <developer>
      <id>eric</id>
      <name>Eric</name>
      <email>wen12128@yahoo.com.cn</email>
      <roles>
        <role>architect</role>
        <role>developer</role>
      </roles>
    </developer>
  </developers>
  <!--项目贡献者,不属于开发小组-->
  <contributors>
    <contributor>
      <name>zq</name>
      <email>zq@gmail.com</email>
      <url>http://zq.com</url>
      <organization>personal</organization>
      <organizationUrl>http://zq.com</organizationUrl>
      <roles>
        <role>tester</role>
      </roles>
      <timezone>-5</timezone>
      <properties>
        <gtalk>zq@gmail.com</gtalk>
      </properties>
    </contributor>
  </contributors>
  <!--项目的邮件列表。自动产生的站点将引用此信息-->
  <mailingLists>
    <mailingList>
      <name>User List</name><!--邮件列表的名称-->
      <subscribe>wen12128@yahoo.com.cn</subscribe><!--订阅此邮件列表的email地址或连接-->
      <unsubscribe>wen12128@yahoo.com.cn</unsubscribe><!--退订此邮件列表的email地址或连接-->
      <post>wen12128@yahoo.com.cn</post><!--可以投递到此邮件列表的email地址或连接-->
      <archive>http://127.0.0.1/user/</archive><!--可以浏览到邮件列表存档信息的URL-->
      <otherArchives><!--可选的替代URLs-->
        <otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive>
      </otherArchives>
    </mailingList>
  </mailingLists>
  <!--<prerequisites>描述此项目构建环境的先决条件</prerequisites>-->
  <!--<modules>本项目的子模块</modules>-->
  <!--指定此项目使用的源代码控制系统,如git等-->
  <scm>
    <connection>scm:git:git://github.com/wen12128/ZCMS.git</connection>
    <developerConnection>scm:git:https://wen12128@github.com/wen12128/ZCMS.git</developerConnection>
    <tag>HEAD</tag>
    <url>https://github.com/wen12128/ZCMS</url>
  </scm>
  <!--bug跟踪系统,如bugzilla,testtrack,clearQuest等-->
  <issueManagement>
    <system>Bugzilla</system>
    <url>http://127.0.0.1/bugzilla</url>
  </issueManagement>
  <!--持续集成系统-->
  <ciManagement>
    <system>continuum</system>
    <url>http://127.0.0.1:8080/continuum</url><!--使用持续集成系统的URL,如果有WEB界面的话-->
    <notifiers><!--配置用户信息和通知模式,当构建成功通知开发人员/用户-->
      <notifier>
        <type>mail</type><!--投递通知的机制-->
        <sendOnError>true</sendOnError><!--是否发送错误通知-->
        <sendOnFailure>true</sendOnFailure><!--是否发送失败通知-->
        <sendOnSuccess>false</sendOnSuccess><!--是否发送成功通知-->
        <sendOnWarning>false</sendOnWarning><!--是否发送警告通知-->
        <configuration>
          <address>wen12128@yahoo.com.cn</address><!--发送通知的地址-->
        </configuration>
      </notifier>
    </notifiers>
  </ciManagement>
  <!--用于配置分发管理,配置相应的产品发布信息,主要用于发布,在执行mvn deploy后表示要发布的位置-->
  <distributionManagement>
    <repository>
      <id>zcms-repository</id>
      <name>zcms Repository</name>
      <url>file://${basedir}/target/deploy</url>
    </repository>
    <downloadUrl>http://github.com/wen12128/ZCMS</downloadUrl>
    <status>deployed</status>
  </distributionManagement>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <!--所有项目的引入依赖信息继承于此-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactid>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>log4j</groupId>
        <artifactid>log4j</artifactId>
        <version>1.2.16</version>
      </dependency>
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.4</version>
        <scope>provided</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependency>
    <groupId>junit</groupId>
    <artifactid>junit</artifactId>
  </dependency>
  <dependency>
    <groupId>log4j</groupId>
    <artifactid>log4j</artifactId>
  </dependency>
  <dependency>
    <groupId>javax.servlet4j</groupId>
    <artifactid>servlet-api</artifactId>
  </dependency>
  <!--所有依赖项列表-->
  <!--<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
      <exclusions>
        <exclusion>
          <groupId>com.sun.jdmk</groupId>
          <artifactId>jmxtools</artifactId>
        </exclusion>
        <exclusion>
          <groupId>com.sun.jmx</groupId>
          <artifactId>jmxri</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.4</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>
  <repositories>私有仓库-查找发现依赖项和扩展项的远程仓库[配置同setting.xml中的开发库]
    <repository>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <layout>default</layout>
      <url>http://repo1.maven.org/maven2</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
  <pluginRepositories>查找发现构建和报表所需插件的远程仓库</pluginRepositories>
  -->

  <!-- Build Settings -->
  <build>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>

    <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>
            <encoding>${project.build.sourceEncoding}</encoding>  
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>  
      <plugin>  
        <groupId>org.mortbay.jetty</groupId>  
        <artifactId>maven-jetty-plugin</artifactId>  
      </plugin>  
      <!--指定JDK版本-->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <target>1.6</target>
          <source>1.6</source>
          <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
      </plugin>
      <plugin>  
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-resources-plugin</artifactId>  
        <version>2.3</version>  
        <configuration>  
          <encoding>${project.build.sourceEncoding}</encoding>  
        </configuration>  
      </plugin>
    </plugins>
  </build>
  <!--项目站点中产生报表的插件规格-在执行mvn site命令时运行-->
  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
    <plugins>            
      <plugin>   
        <groupId>org.codehaus.mojo</groupId>   
        <artifactId>jxr-maven-plugin</artifactId>   
      </plugin>   
    </plugins>
  </reporting>
 
  <!--项目本地构建文件列表,可改变默认构建过程-->
  <profiles>
    <profile>
      <id>test</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>1.5</jdk>
        <os>
          <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
          <version>5.1.2600</version>
        </os>
        <property>
          <name>mavenVersion</name>
          <value>2.0.6</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>
    </profile>
  </profiles>
</project>
实例:
1.生成父级工程:
  mvn archetype:create -DgroupId=com.zcms -DartifactId=zcms
  删除zcms下的src文件夹,打开pom.xml,将packaging属性修改为pom(表示为父工程)

2.生成子工程:进入zcms目录
  mvn archetype:create -DgroupId=com.zcms -DartifactId=zcms-business -Dpackage=com.zcms.business
  
  mvn archetype:create -DgroupId=com.zcms -DartifactId=zcms-zk -Dpackage=com.zcms.zk

  mvn archetype:create -DgroupId=com.zcms -DartifactId=zcms-web -Dpackage=com.zcms.web -DarchetypeArtifactId=maven-archetype-webapp

3.生成IDEA的项目文件:
  mvn idea:clean idea:idea -DdownloadSources=true -DdownloadJavadocs=true -DjdkLevel=1.6

4.当pom.xml文件发生变化时,我们只需使用下面命令重新生成module文件即可,新生成的module文件会和原来module文件进行很好的合并:
  mvn idea:module

5.补全项目的目录结构:
  mkdir -p src/{main/config,main/assembly,main/scripts,main/java/com/zmcs,test/java/com/zcms,test/resources,site}

参考:http://tianya23.blog.51cto.com/1081650/289393
http://www.cnblogs.com/joycel/archive/2010/05/05/1727681.html
http://www.ibm.com/developerworks/cn/java/j-5things13/index.html
http://www.blogjava.net/jiangshachina/category/16092.html
原文地址:https://www.cnblogs.com/wen12128/p/2804891.html