1.安装或配在好mvn命令(Maven3)
2.创建项目:
mvn archetype:generate -DarchetypeCatalog=internal -DgroupId=com.cn -DartifactId=Demo1 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mvn archetype:generate 固定格式
-DarchetypeCatalog remote,internal ,local(解决Maven创建项目缓慢);
-DgroupId 组织标识(包名)
-DartifactId 项目名称
-DarchetypeArtifactId maven-archetype-quickstart(Java Project);maven-archetype-webapp(Web Project)......
-DinteractiveMode 是否使用交互模式
3.修改项目下的pom.xml文件中
<packaging>jar</packaging>
改为:
<packaging>pom</packaging>
PS:如果不修改这个参数,后面将无法创建子模块
4.创建子模块:
打开到项目文件夹下,执行mvn命令创建子java模块
cd Demo
mvn archetype:generate -DarchetypeCatalog=internal -DgroupId=com.cn -DartifactId=dao -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
<pre name="code" class="plain">mvn archetype:generate -DarchetypeCatalog=internal -DgroupId=com.cn -DartifactId=service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
执行mvn命令创建子web
模块mvn archetype:generate -DarchetypeCatalog=internal -DgroupId=com.cn -DartifactId=web -DarchetypeArtifactId=<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 13px; line-height: 25px;">maven-archetype-webapp</span> -DinteractiveMode=false
例如:
5.子模块间依赖
例如在web模块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>
<groupId>com.cn</groupId>
<artifactId>Demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.cn</groupId>
<artifactId>web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>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>
<!--web依赖service -->
<dependency>
<groupId>com.cn</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--web依赖dao -->
<dependency>
<groupId>com.cn</groupId>
<artifactId>dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>web</finalName>
</build>
</project>
6.编译运行项目
mvn clean install
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] Demo ............................................... SUCCESS [ 0.985 s]
[INFO] dao ................................................ SUCCESS [ 14.882 s]
[INFO] service ............................................ SUCCESS [ 0.759 s]
[INFO] web Maven Webapp ................................... SUCCESS [ 0.923 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.917 s
[INFO] Finished at: 2016-05-10T22:40:55+08:00
[INFO] Final Memory: 20M/197M
[INFO] ------------------------------------------------------------------------
成功执行后,子模块都多了target文件夹,在web模块中多了一个打包好了的war
web模块有依赖java模块,在lib中会打包成对应的jar。
放到Tomcat(或者其他服务器)运行即可。