1. 点击 New -- Project
2. 设置工程父Pom, 如下
<?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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.test</groupId> <artifactId>test-parent</artifactId> <version>1.0.0.Beta</version> <dependencies> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>15.0</version> </dependency> </dependencies> </project>
3. 删除父工程Src并创建子Module, 点击 New -- Module
4. 将子module的java和resource目录设置为source目录
右击java和resource目录, 选择 Mark Directory As -- Source Root
5. 用子module的pom继承父module的pom, 这样就能使用父module的dependencies了
pom如下
<?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/xsd/maven-4.0.0.xsd"> <parent> <artifactId>test-parent</artifactId> <groupId>org.test</groupId> <version>1.0.0.Beta</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>sub-module</artifactId> </project>
6. 在子module的dependency中加入所有pom里引入的依赖(如果开启了Auto-Import denpendency, 则不需要这一步)
右击子module, 选择 Open Module Setting
点右边的+号,选择Library
添加需要的Jar