IDEA创建Maven多模块项目
一、创建父工程
- IDEA->新建项目
- 选择Maven->勾选Create from archetype,选择下拉org.apache.maven.archetypes:mave-archetype-webapp,点击下一步
- 填写Groupid(如:cc.liqingsong),填写Arifactid(如:blog),填写Version(如:1.0),点击下一步
- 填写project name:(如:blog),选择project location(如:D盘下blog),点击下一步
二、pom.xml中不需要到删掉,保留如下:
```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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--
maven父工程必须遵循以下两点要求:
1、packaging标签的文本内容必须设置为pom。
2、删除src目录
-->
<packaging>pom</packaging>
</project>
```
三、删除src目录,只保留pom.xml文件。
四、创建子项目模块
- 父项目右键->新建->模块
- 选择Maven->勾选Create from archetype,选择下拉org.apache.maven.archetypes:mave-archetype-webapp,点击下一步
- 选择父项,填写Groupid(如:cc.liqingsong),填写Arifactid(如:api),填写Version(如:1.0),点击下一步
- 填写project name:(如:blog),选择project location(如:D盘下blog),点击下一步
五、父工程管理依赖版本号,父工程的`pom.xml`中的依赖,子工程都可以使用。
- 父工程pom.xml内容:
```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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!--
maven父工程必须遵循以下两点要求:
1、packaging标签的文本内容必须设置为pom。
2、删除src目录
-->
<packaging>pom</packaging>
<modules>
<module>api</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<mysql.version>5.1.9</mysql.version>
<junit.version>4.12</junit.version>
</properties>
<!--父工程要管理所有依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
```
- 子工程pom.xml需要什么引入什么
```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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--指向父工程-->
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
```
六、统一编译插件父工程`pom.xml`中:
```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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
</properties>
<build>
<plugins>
<!--统一编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
```