【建項目】eclipse maven建立多模塊工程
在工作的時候,大多時候都是用Maven來管理項目,可是一般我們都知道怎麽用maven管理工程,卻不知道通過Maven自己來建立多模塊工程。於是自己抽時間,在網上找些資料,做了起來。
建立簡單的Maven模塊結構是這樣的:
---- mv-parent
|-- pom.xml (pom)
|
|-- mv-xml
| |-- pom.xml (jar)
|
|-- mv-lib
| |-- pom.xml (jar)
|
|-- mv-core
| |-- pom.xml (jar)
|
|-- mv-web
|-- pom.xml (war)
項目工程的依賴關系:mv-core依賴mv-lib,mv-xml;mv-web依賴 mv-lib,mv-xm,mv-core
建立整個項目的步驟如下:
1.建立mv-parent工程
new->other->maven->maven project。
註意選擇 create a simple project
packaging 選擇類型 pom .
建立完成後,得到項目:
得到 pom.xml
<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>org.com.dk</groupId> <artifactId>mv-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> </project>
2. 建立mv-lib工程.主要用來存放項目jar
new->other->maven->maven Module.
選擇module工程
這個mv-lib工程完成,得到
pom.xml.我把 commons-lang.jar放進去,作為測試:
<?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> <artifactId>mv-parent</artifactId> <groupId>org.com.dk</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>org.com.dk</groupId> <artifactId>mv-lib</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mv-lib</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.4</version> </dependency> </dependencies> </project>
3. 建立mv-xml工程.主要用來存放項目配置文件
new->other->maven->maven Module.
選擇module工程
pom.xml:
<?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> <artifactId>mv-parent</artifactId> <groupId>org.com.dk</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>org.com.dk</groupId> <artifactId>mv-xml</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mv-xml</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
4. 建立mv-core工程.主要用來存項目業務代碼
new->other->maven->maven Module.
選擇module工程
項目完成。不過要註意mv-core依賴mv-lib,mv-xml。這個需要在pom.xml配置( 標紅 )
如下:
<?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> <artifactId>mv-parent</artifactId> <groupId>org.com.dk</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>org.com.dk</groupId> <artifactId>mv-core</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mv-core</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.com.dk</groupId> <artifactId>mv-lib</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.com.dk</groupId> <artifactId>mv-xml</artifactId> <version>${project.version}</version> </dependency> </dependencies> </project>
5. 建立mv-web工程.主要用來項目web
建立web工程,可見我的
http://dk05408.iteye.com/blog/2100525 這個比較詳細
new->other->maven->maven Module.
選擇module工程
這一步的選擇比較重要:catalog:Internal,下面選擇 maven-archetype-webapp.如圖:
項目建立後: 右擊項目,點擊Properties->Project Facets
得到web工程,得到如圖:
mv-web依賴mv-lib,mv-xml,mv-core。
pom.xml:
<?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> <artifactId>mv-parent</artifactId> <groupId>org.com.dk</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>org.com.dk</groupId> <artifactId>mv-web</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>mv-web Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.com.dk</groupId> <artifactId>mv-lib</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.com.dk</groupId> <artifactId>mv-xml</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.com.dk</groupId> <artifactId>mv-core</artifactId> <version>${project.version}</version> </dependency> </dependencies> <build> <finalName>mv-web</finalName> </build> </project>
整個工程完成了
最後可以看下mv-parent裏的pom.xml:
<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>org.com.dk</groupId> <artifactId>mv-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>mv-lib</module> <module>mv-xml</module> <module>mv-core</module> <module>mv-web</module> </modules> </project>
你就可以看到mv-parent下面有4個Module。
對 mv-parent進行run-as clean install。
看到的部分日誌:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ mv-web ---
[INFO] Packaging webapp
[INFO] Assembling webapp [mv-web] in [F:\java\workspace\mv-parent\mv-web\target\mv-web]
[INFO] Processing war project
[INFO] Copying webapp resources [F:\java\workspace\mv-parent\mv-web\src\main\webapp]
[INFO] Webapp assembled in [200 msecs]
[INFO] Building war: F:\java\workspace\mv-parent\mv-web\target\mv-web.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored
(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as ‘true‘)
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ mv-web ---
[INFO] Installing F:\java\workspace\mv-parent\mv-web\target\mv-web.war to C:\Users\dk\.m2\repository\org\com\dk\mv-web\0.0.1-SNAPSHOT\mv-web-0.0.1-SNAPSHOT.war
[INFO] Installing F:\java\workspace\mv-parent\mv-web\pom.xml to C:\Users\dk\.m2\repository\org\com\dk\mv-web\0.0.1-SNAPSHOT\mv-web-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] mv-parent ......................................... SUCCESS [0.273s]
[INFO] mv-lib ............................................ SUCCESS [1.860s]
[INFO] mv-xml ............................................ SUCCESS [1.017s]
[INFO] mv-core ........................................... SUCCESS [0.986s]
[INFO] mv-web Maven Webapp ............................... SUCCESS [1.665s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.944s
[INFO] Finished at: Sat Aug 09 11:56:38 CST 2014
[INFO] Final Memory: 12M/32M
[INFO] ------------------------------------------------------------------------
可以得到最後的工程
mv-parent工程
mv-web工程:
項目完成。
這樣後面你就可以向裏面添加jar,xml等,就可以豐富你的工程。
【建項目】eclipse maven建立多模塊工程