在工作的时候,大多时候都是用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等,就可以丰富你的工程。