Maven多模块项目搭建

maven pom.xml加载不同properties配置 http://my.oschina.net/vdroid/blog/400708



问题:
1. Check $M2_HOME environment variable and mvn script match. 解决办法: http://fxb4632242.iteye.com/blog/2193945




创建工程
http://my.oschina.net/alexgaoyh/blog/397487
创建一个Maven Project 选择 maven-archetype-quickstart



项目创建完成之后,修改buildPath相关(src/main/java src/main/resources……); JDK ……


项目右键--》New--》Maven Module--》输入 MutiModule-captcha(验证码相关模块)--》选择maven-archetype-quickstart


PS: 如果报错 The parent project must have a packaging type of POM

将pom.xml 中的  <packaging>jar</packaging> 改成  <packaging>pom</packaging>

创建成功之后如下图:




其中 captcha 项目里面包含了验证码的相关操作,并且功能已经实现,项目右键成功通过junit测试。



配置工程:
启动tomcat或者jetty服务器:
mvn tomcat7:run -DskipTests
mvn jetty:run -DskipTests

注意, 在dos下面可以这样执行, 在eclipse或者idea下面, 要去掉mvn.

setting.xml增加tomcat后者jetty的支持
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
	<pluginGroup>org.mortbay.jetty</pluginGroup>
	<pluginGroup>org.apache.tomcat.maven</pluginGroup>
	<pluginGroup>com.jayway.maven.plugins.android.generation2</pluginGroup>
  </pluginGroups>



总工程
<?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>rh</groupId>
  <artifactId>rh_cpm_new</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>rh_cpm_new</name>
  <url>http://maven.apache.org</url>

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

  <properties>
        <version.num>4.1</version.num>
        <!-- 这里配置一些通用属性本信息 -->
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 这里配置所有通用依赖库的版本信息 -->
        </dependencies>
    </dependencyManagement>
    <build>
        <pluginManagement>
            <plugins>
	      <!-- 这里配置所有插件的版本信息 -->
            </plugins>
        </pluginManagement>
    </build>
  <!-- 这里指定它所有子模块 -->
  <modules>
    <module>app_tools</module>
    <module>app_core</module>
    <module>rh_base</module>
    <module>rh_cpm_web</module>
  </modules>
</project>


第1个子模块
<?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>rh</groupId>
    <artifactId>rh_cpm_new</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>rh</groupId>
  <artifactId>app_tools</artifactId>  
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <name>app_tools</name>
  <description>app_tools</description>
  <url>http://maven.apache.org</url>
  <properties>
    <!-- 可以定义一些属性,有必要的情况下 -->
  </properties>
  <dependencies>
    <!-- 把所有父模块定义的通用依赖加入进来,可以不用版本,因为父模块已经定义了版本信息 -->
    <!-- 可以自己增加一些特殊依赖,但是要指定版本信息 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <scope>provided</scope>
     </dependency>
  </dependencies>
</project>


第2个子模块
<?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>rh</groupId>
    <artifactId>rh_cpm_new</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>rh</groupId>
  <artifactId>app_core</artifactId>  
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <name>app_core</name>
  <description>app_core</description>
  <url>http://maven.apache.org</url>
  <properties>
    <!-- 可以定义一些属性,有必要的情况下 -->
  </properties>
  <dependencies>
    <!-- 把第1个模块, 作为第2个模块的依赖 -->
    <dependency>
      <groupId>rh</groupId>
      <artifactId>app_tools</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- 把所有父模块定义的通用依赖加入进来,可以不用版本,因为父模块已经定义了版本信息 -->
    <!-- 可以自己增加一些特殊依赖,但是要指定版本信息 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <scope>provided</scope>
     </dependency>
  </dependencies>
</project>


第3个模块, 跟第2个模块类似
<?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>rh</groupId>
    <artifactId>rh_cpm_new</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>rh</groupId>
  <artifactId>rh_base</artifactId>  
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <name>rh_base</name>
  <description>rh_base</description>
  <url>http://maven.apache.org</url>
  <properties>
    <!-- 可以定义一些属性,有必要的情况下 -->
  </properties>
  <dependencies>
    <!-- 把第1个模块, 作为第2个模块的依赖 -->
    <dependency>
      <groupId>rh</groupId>
      <artifactId>app_core</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- 把所有父模块定义的通用依赖加入进来,可以不用版本,因为父模块已经定义了版本信息 -->
    <!-- 可以自己增加一些特殊依赖,但是要指定版本信息 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <scope>provided</scope>
     </dependency>
  </dependencies>
</project>



最后一个模块, web模块
<?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>rh</groupId>
		<artifactId>rh_cpm_new</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>rh_cpm_web</artifactId>
	<!-- 这里要指定web的打包方式 -->
	<packaging>war</packaging>
	<name>XXXXXXXXXXXX</name>
	<url>http://www.ronghuitec.com</url>


	<dependencies>
	    <!-- 把上一层的模块依赖进来 -->
	    <dependency>
	      <groupId>rh</groupId>
	      <artifactId>rh_base</artifactId>
	      <version>0.0.1-SNAPSHOT</version>
	    </dependency>
	    <dependency>
	      <groupId>javax.servlet</groupId>
	      <artifactId>servlet-api</artifactId>
	      <scope>provided</scope>
	    </dependency>
	    <dependency>
	      <groupId>javax.servlet.jsp</groupId>
	      <artifactId>jsp-api</artifactId>
	      <scope>provided</scope>
	    </dependency>
	</dependencies>
	<build>
	  <finalName>rh_cpm_web</finalName>
	  <defaultGoal>package</defaultGoal>
	  <filters>
	  <!--<filter>${basedir}/src/main/jdbc.properties</filter> -->
	  </filters>
	  <plugins>
	      <!-- 加入依赖处理的插件 -->
	      <plugin>  
	        <groupId>org.apache.maven.plugins</groupId>  
	        <artifactId>maven-dependency-plugin</artifactId>
	        <version>2.8</version>
	        <executions>
		  <execution>
		    <id>copy</id>  
		    <phase>install</phase>  
		    <goals>  
		      <goal>copy-dependencies</goal>  
		    </goals>  
		    <configuration>  
		      <outputDirectory>$/lib</outputDirectory>  
		      <!-- 拷贝所以依赖存放位置 -->  
		      </configuration>  
		    </execution>  
	        </executions>  
	      </plugin>
	      <!-- 加入支持查询svn一些信息的插件 -->
	      <plugin>
	        <groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
	        <artifactId>svn-revision-number-maven-plugin</artifactId>
	        <version>1.13</version>
	        <configuration>
		  <entries>
		    <entry>
		      <prefix>svn_info</prefix>
		      <depth>empty</depth>
		    </entry>
		  </entries>
	        </configuration>
	        <executions>
		  <execution>
		      <phase>validate</phase>
		      <goals>
			<goal>revision</goal>
		      </goals>
		  </execution>
	        </executions>
	        <dependencies>
		  <dependency>
		      <groupId>org.tmatesoft.svnkit</groupId>
		      <artifactId>svnkit</artifactId>
		      <version>1.7.8</version>
		  </dependency>
	        </dependencies>
	      </plugin>
	      <!-- 加入处理国际化信息的插件 -->
	      <plugin>
	        <groupId>org.codehaus.mojo</groupId>
	        <artifactId>native2ascii-maven-plugin</artifactId>
	        <executions>
		    <execution>
		    <id>native2ascii-utf8</id>
		    <goals>
		      <goal>native2ascii</goal>
		    </goals>
		    <phase>process-resources</phase>
		    <configuration>
		      <encoding>UTF8</encoding>
		      <src>src/main/resources</src>
		      <dest>target/classes</dest>
		      <includes>
			<include>applicationResources/*_zh*.properties</include>
			<include>*_zh*.properties</include>
		      </includes>
		    </configuration>
		  </execution>
	        </executions>
	      </plugin>
	      <!-- 加入编译的插件 -->
	      <plugin>
	        <groupId>org.apache.maven.plugins</groupId>
	        <artifactId>maven-compiler-plugin</artifactId>
	        <configuration>
	        	<source>1.7</source>
	        	<target>1.7</target>
	        </configuration>
	      </plugin>
	      <!-- 加入tomcat服务器的插件 -->
	      <plugin>
	        <groupId>org.apache.tomcat.maven</groupId>
	        <artifactId>tomcat7-maven-plugin</artifactId>
	        <version>2.2</version>
	        <configuration>
	        	<url>http://localhost:8080/manager/text</url>
	        	<username>admin</username>
	        	<password>admin</password>
	        	<path>/dev_cpm</path>
	        </configuration>
	      </plugin>
	      <!-- 加入jetty服务器的插件 -->
	      <plugin>
	        <groupId>org.mortbay.jetty</groupId>
	        <artifactId>jetty-maven-plugin</artifactId>
	        <configuration>
	        	<webApp>
	        		<contextPath>/dev_cpm</contextPath>
	        	</webApp>
	        </configuration>
	      </plugin>
	      <!-- 加入清理工程的插件 -->
	      <plugin>
	        <groupId>org.apache.maven.plugins</groupId>
	        <artifactId>maven-clean-plugin</artifactId>
	        <configuration>
	        	<filesets>
	        		<fileset>
	        			<directory>${user.home}/${project.build.finalName}/index</directory>
	        		</fileset>
	        	</filesets>
	        </configuration>
	      </plugin>
	      <!-- 加入war打包插件 -->
	      <plugin>
	        <groupId>org.apache.maven.plugins</groupId>
	        <artifactId>maven-war-plugin</artifactId>
	        <configuration>
		  <escapeString>\</escapeString>
		  <!--<warName>${war.name}-${version.num}</warName> -->
		  <warName>${war.name}</warName>
		  <warSourceExcludes>src/main/resources/packaged/**</warSourceExcludes>
		  <webResources>
		    <resource>
		      <directory>src/main/resources/packaged</directory>
		      <targetPath>WEB-INF/classes</targetPath>
		      <filtering>true</filtering>
		      <includes>
			<include>hibernate.properties</include>
		      </includes>
		    </resource>
		    <resource>
		  <directory>src/main/resources/packaged/tmp</directory>
		  <targetPath>WEB-INF/classes</targetPath>
		  <filtering>true</filtering>
		  <includes>
		      <include>applicationContext-init.xml</include>
		      <include>applicationContext-resources.xml</include>
		  </includes>
		  </resource>
		  </webResources>
		  <warSourceDirectory>src/main/webapp</warSourceDirectory>
		  <webXml>src/main/resources/packaged/tmp/web-${package.profile.flag}.xml</webXml>
		  <archive>
		    <manifestEntries>
		      <SVN-Revision>${svn_info.revision}</SVN-Revision>
		      <SVN-path>${svn_info.path}</SVN-path>
		      <SVN-mixedRevisions>${svn_info.mixedRevisions}</SVN-mixedRevisions>
		    </manifestEntries>
		  </archive>
	        </configuration>
	      </plugin>
	      <!-- 加入ant的功能插件 -->
	      <plugin>
	        <groupId>org.apache.maven.plugins</groupId>
	        <artifactId>maven-antrun-plugin</artifactId>
	        <executions>
		  <execution>
		      <id>process-classes</id>
		      <phase>process-classes</phase>
		      <goals>
			<goal>run</goal>
		      </goals>
		      <configuration>
		      <tasks>
			<delete>
			  <fileset dir="${basedir}/src/main/resources/packaged/tmp" includes="**/*.*" />
			</delete>
	        	        <delete dir="${project.build.directory}/classes/packaged" />
	        	        <!--只用于开发 -->
	        	        <copy
	        	        	file="${basedir}/src/main/resources/packaged/applicationContext-resources-${package.profile.flag}.xml"
	        	        	tofile="${project.build.directory}/classes/applicationContext-resources.xml"
	        	        	overwrite="true"></copy>
	        	        <copy
	        	        	file="${basedir}/src/main/resources/packaged/log4j_${package.profile.flag}.xml"
	        	        	tofile="${project.build.directory}/classes/log4j.xml"
	        	        	overwrite="true"></copy>

	        	        <!-- wyman remove 不需要logback的配置 <copy file="${basedir}/src/main/resources/packaged/logback_${package.profile.flag}.xml" 
	        	        	tofile="${project.build.directory}/classes/logback.xml" overwrite="true"></copy> -->
	        	        <!-- 只适合开发,打包的时候会被war配置覆盖 -->
	        	        <copy
	        	        	file="${basedir}/src/main/resources/packaged/web-${package.profile.flag}.xml"
	        	        	tofile="${basedir}/src/main/webapp/WEB-INF/web.xml" overwrite="true"></copy>

	        	        <copy file="${basedir}/src/common/DGC.jar" tofile="${basedir}/src/main/webapp/WEB-INF/lib/DGC.jar"
	        	        	overwrite="true"></copy>
	        	        <copy file="${basedir}/src/common/safenet-sentinel-hasp-api.jar"
	        	        	tofile="${basedir}/src/main/webapp/WEB-INF/lib/safenet-sentinel-hasp-api.jar"
	        	        	overwrite="true"></copy>
	        	        <copy
	        	        	file="${basedir}/src/common/safenet-sentinel-hasp-nemesis-runtime.jar"
	        	        	tofile="${basedir}/src/main/webapp/WEB-INF/lib/safenet-sentinel-hasp-nemesis-runtime.jar"
	        	        	overwrite="true"></copy>
	        	        <copy
	        	        	file="${basedir}/src/common/safenet-sentinel-hasp-nemesis-supplements.jar"
	        	        	tofile="${basedir}/src/main/webapp/WEB-INF/lib/safenet-sentinel-hasp-nemesis-supplements.jar"
	        	        	overwrite="true"></copy>

	        	        <copy todir="${project.build.directory}/classes/com/rh/templates"
	        	        	overwrite="true">
	        	        	<fileset dir="${basedir}/src/main/resources/com/rh/templates" />
	        	        </copy>
	        			</tasks>
	        		</configuration>
	        	</execution>
	        </executions>
			</plugin>
			<!-- 继续加入有必要的插件 -->
	      </plugins>
	      <!-- 处理resource等文件 -->
	  <resources>
	    <resource>
	      <directory>src/main/resources</directory>
	      <filtering>true</filtering>
	      <excludes>
		<exclude>ApplicationResources*.properties</exclude>
		<exclude>displaytag*.properties</exclude>
	      </excludes>
	    </resource>
	    <resource>
	      <directory>src/main/resources</directory>
	      <filtering>false</filtering>
	    </resource>
	  </resources>
	  <!-- eclipse自动修复的代码 -->
	  <pluginManagement>
	    <plugins>
	      <!--This plugin's configuration is used to store Eclipse m2e settings 
	        	only. It has no influence on the Maven build itself. -->
	      <plugin>
		<groupId>org.eclipse.m2e</groupId>
		<artifactId>lifecycle-mapping</artifactId>
		<version>1.0.0</version>
		<configuration>
		    <lifecycleMappingMetadata>
		    ......
		    </lifecycleMappingMetadata>
		</configuration>
	      </plugin>
	    </plugins>
	  </pluginManagement>
	</build>

	<properties>
		<!-- 覆盖多个propertie -->
		<cache.timeOutNum>15</cache.timeOutNum>
		<sql.isPrintColumnName>false</sql.isPrintColumnName>
		<dbunit.dataTypeFactoryName>org.dbunit.ext.mssql.MsSqlDataTypeFactory</dbunit.dataTypeFactoryName>
		<dbunit.operation.type>MSSQL_CLEAN_INSERT</dbunit.operation.type>
		<!-- 可以改动的配置 -->
		<hibernate.show_sql>false</hibernate.show_sql>
		<!--<hibernate.dialect>org.hibernate.dialect.SQLServerDialect</hibernate.dialect> -->
		<hibernate.dialect>com.rh.sys.core.dao.hibernate.MySQLServerDialect</hibernate.dialect>
		<jdbc.driverClassName>com.microsoft.sqlserver.jdbc.SQLServerDriver</jdbc.driverClassName>
		<jdbc.url>jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${db.name}</jdbc.url>
		<!--<jdbc.driverClassName>net.sf.log4jdbc.DriverSpy</jdbc.driverClassName>
		<jdbc.url>jdbc:log4jdbc:sqlserver://127.0.0.1:1433;DatabaseName=${db.name}</jdbc.url>-->
		<jdbc.username>sa</jdbc.username>
		<jdbc.password>sa</jdbc.password>
		<project.name>my_project</project.name>
		<db.name>database</db.name>
		<war.name>war_name</war.name>
		<pri.data.open.filter>true</pri.data.open.filter>
		<package.profile.flag>dev</package.profile.flag>
	</properties>
	<profiles>
		<!-- 配置多个profile -->
		<profile>
			<id>dev</id>
			<properties>
				<war.name>rh_cpm_dev</war.name>
				<db.name>rh_cpm_dev</db.name>
				<hibernate.show_sql>false</hibernate.show_sql>
				<pri.data.open.filter>true</pri.data.open.filter>
				<!-- 60*60*2 缓存1小时 -->
				<cache.timeOutNum>20</cache.timeOutNum>
				<sql.isPrintColumnName>false</sql.isPrintColumnName>
				<package.profile.flag>dev</package.profile.flag>
			</properties>
		</profile>
	</profiles>
</project>

猜你喜欢

转载自panyongzheng.iteye.com/blog/2200514