1.创建一个多模块的Java项目
shift+鼠标右键
创建项目命令:
旧版:
mvn archetype:create -DgroupId=com.qhong -DartifactId=MavenParent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
新版:
mvn archetype:generate -DgroupId=com.qhong -DartifactId=MavenParent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
发现就改变一个单词,新版创建的速度真是太慢了
用旧版方式创建报错:
网上进行查找,发现在maven3.0.5以上版本舍弃了create,使用generate生成项目,我的是3.3.9版本,所以。。。
新版创建成功
创建的MavenParent项目中的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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.qhong</groupId> <artifactId>MavenParent</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>MavenParent</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
现在修改打包方式为pom
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.qhong</groupId> <artifactId>MavenParent</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>MavenParent</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
现在在MavenParent文件夹内部再次创建项目Service,不得不说,真慢!
mvn archetype:generate -DgroupId=com.qhong -DartifactId=Service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
在创建一个Web,
mvn archetype:generate -DgroupId=com.qhong -DartifactId=Web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
查看MavenParent的pom.xml
<?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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.qhong</groupId> <artifactId>MavenParent</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <name>MavenParent</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <modules> <module>Service</module> <module>Web</module> </modules> </project>
发现多了modules
现在一个多模块项目就创建好了,MavenParent中的src文件没有用,可以删除了。
用idea打开,查看整个结构:
Service项目的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.qhong</groupId> <artifactId>MavenParent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.qhong</groupId> <artifactId>Service</artifactId> <version>1.0-SNAPSHOT</version> <name>Service</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
修改Web的pom.xml,使其依赖Service项目
<?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.qhong</groupId> <artifactId>MavenParent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.qhong</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> <dependency> <groupId>com.qhong</groupId> <artifactId>Service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <finalName>Web</finalName> </build> </project>
对Service进行安装
下面对web项目进行打包,部署
把打包好的Web.war包放在tomcat9的webapp下
发现Web.war放下后会自动解压
访问url