最近在學(xué)習(xí)Maven,把一個(gè)開(kāi)源的項(xiàng)目改成maven管理,期間使用到了多項(xiàng)目,從網(wǎng)上查閱了一些資料,主要參考的是http://kyfxbl.iteye.com/blog/1680045,在此把自己的一些心得體會(huì)寫出來(lái),供大家學(xué)習(xí)交流。
關(guān)于maven的安裝,在此就不進(jìn)行闡述,請(qǐng)參考網(wǎng)上其他教程。
本實(shí)例由4個(gè)項(xiàng)目組成,其中,aggregator是父工程,同時(shí)承擔(dān)聚合模塊和父模塊的作用,沒(méi)有實(shí)際代碼和資源文件;open-plagform-common是公共的java工程;open-platfor-web是公共的web文件,主要包括css、js等;open-bug-m是最終要發(fā)布的應(yīng)用,形成war包。
一、建立一個(gè)Maven工程:aggregator
/aggregator
/src/main/java
/src/test/java
pom.xml
此工程主要是父模塊,聚合其他工程,沒(méi)有實(shí)際代碼和資源文件,最主要的是pom.xml文件,其主要內(nèi)容如下:
<modelVersion>4.0.0</modelVersion> <groupId>cn.jess.platform</groupId> <artifactId>aggregator</artifactId> <version>0.0.1-SNAPSHOT</version><!-- 因?yàn)槭歉腹こ?,因此此處的packaging必須為pom --> <packaging>pom</packaging> <name>aggregator</name> <modules> <module>../open-platform-common</module> <module>../open-platform-web</module> <module>../open-bug-m</module> </modules> <!-- 配置部署的遠(yuǎn)程倉(cāng)庫(kù) --> <distributionManagement> <snapshotRepository> <id>nexus-snapshots</id> <name>nexus distribution snapshot repository</name> <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <encoding>UTF-8</encoding> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </pluginManagement> </build> <dependencyManagement> <dependencies> <dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.6.0</version> <scope>system</scope> <systemPath>${env.
JAVA_HOME}/lib/tools.jar</systemPath> </dependency> </dependencies> </dependencyManagement>
二、建立一個(gè)Maven工程:open-platform-common
此工程主要是項(xiàng)目中使用到的公共java類庫(kù),pom文件主要內(nèi)容如下:
<!-- 由于存在parent工程,因此groupId和version可以省略,直接使用parent工程--> <modelVersion>4.0.0</modelVersion> <artifactId>open-platform-common</artifactId> <!-- 因?yàn)榇斯こ桃l(fā)布到webapp的lib目錄下,因此為jar(不知道這樣解釋對(duì)否?) --> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- 指定Maven倉(cāng)庫(kù) --> <repositories> <!-- my的maven倉(cāng)庫(kù) --> <repository> <id>myRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 指定maven plugin倉(cāng)庫(kù) --> <pluginRepositories> <!-- oschina的maven plugin倉(cāng)庫(kù) --> <pluginRepository> <id>myPluginRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <dependencies> <!-- 此處的類庫(kù)根據(jù)自己的需要進(jìn)行添加 --> </dependencies> <!-- 用來(lái)指定父工程--> <parent> <groupId>cn.jess.platform</groupId> <artifactId>aggregator</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../aggregator</relativePath> </parent>
三、建立一個(gè)Maven工程:open-platform-web
此工程主要是項(xiàng)目中使用到的公共web文件,pom文件主要內(nèi)容如下:
<!-- 由于存在parent工程,因此groupId和version可以省略,直接使用parent工程--> <modelVersion>4.0.0</modelVersion> <artifactId>open-platform-web</artifactId><!-- 因?yàn)榇斯こ桃l(fā)布到webapp應(yīng)用的根目錄下,因此為war(不知道這樣解釋對(duì)否?) --> <packaging>war<ng> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <!-- 指定Maven倉(cāng)庫(kù) --> <repositories> <!-- my的maven倉(cāng)庫(kù) --> <repository> <id>myRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 指定maven plugin倉(cāng)庫(kù) --> <pluginRepositories> <!-- oschina的maven plugin倉(cāng)庫(kù) --> <pluginRepository> <id>myPluginRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <parent> <groupId>cn.jess.platform</groupId> <artifactId>aggregator</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../aggregator</relativePath> </parent></project>
注意:此工程的WEB-INF目錄下必須包含web.xml文件,否則在執(zhí)行mvn時(shí)會(huì)報(bào)錯(cuò)
四、建立一個(gè)Maven工程:open-bug-m:
此工程是最終要發(fā)布的應(yīng)用,其依賴于open-platform-common和open-platform-web,因此在pom文件中要加入這兩個(gè)工程的依賴,pom文件內(nèi)容如下所示:
<groupId>open-bug-m</groupId> <artifactId>open-bug-m</artifactId> <packaging>war</packaging> <name/> <description/> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>cn.jess.platform</groupId> <artifactId>aggregator</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../aggregator</relativePath> </parent> <!-- 指定Maven倉(cāng)庫(kù) --> <repositories> <!-- my的maven倉(cāng)庫(kù) --> <repository> <id>myRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <!-- 指定maven plugin倉(cāng)庫(kù) --> <pluginRepositories> <!-- oschina的maven plugin倉(cāng)庫(kù) --> <pluginRepository> <id>myPluginRepository</id> <name>local private nexus</name> <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <dependencies> <dependency> <groupId>cn.jess.platform</groupId> <artifactId>open-platform-common</artifactId> <version>0.0.1-SNAPSHOT</version> <type>jar</type> </dependency> <dependency> <groupId>cn.jess.platform</groupId> <artifactId>open-platform-web</artifactId> <version>0.0.1-SNAPSHOT</version> <type>war</type> </dependency> <!-- 此處的類庫(kù)根據(jù)自己的需要進(jìn)行添加 --> </dependencies> <build> <finalName>open-bug</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <packagingExcludes>WEB-INF/web.xml</packagingExcludes> <overlays> <overlay> <groupId>cn.jess.platform</groupId> <artifactId>open-platform-web</artifactId> </overlay> </overlays> </configuration> </plugin> <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.2.3</version> <configuration> <container> <containerId>tomcat7x</containerId> <home>F:\apache-tomcat-7.0.42(x64)</home> </container> <configuration> <type>existing</type> <home>F:\apache-tomcat-7.0.42(x64)</home> <properties> <cargo.jvmargs> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787 </cargo.jvmargs> </properties> </configuration> </configuration> <executions> <execution> <id>cargo-run</id> <phase>pre-integration-test</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
關(guān)于maven-war-plugin和cargo-maven2-plugin的使用方法請(qǐng)參考網(wǎng)上其他使用教程。
所有上述四個(gè)工程準(zhǔn)備就緒后,執(zhí)行mvn install就可對(duì)工程項(xiàng)目進(jìn)行部署。
現(xiàn)在存在的一個(gè)問(wèn)題主要是:
如果把open-platform-common和open-platform-web工程合并為一個(gè),則在open-bug-m不知道如何去引用它?