廖雪峰Java12maven基础-2maven进阶-2模块管理
1. 把大项目拆分为模块是降低软件复杂度的有效方法
在Java项目中,我们通常会会把一个项目分拆为模块,这是为了降低软件复杂度。
例如:我们可以把一个大的项目氛围module-a, module-b, module-c三个模块,这样我们就可以降低软件的复杂度。
maven可以有效管理多个模块
2.建立一个非mavend的项目,。在这个项目下新建4个maven模块。
2.1 模块greeting
不要忘了log4j2.xml文件
Greeting.java
package com.greeting;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Greeting {
public static void print(String name){
Log log = LogFactory.getLog(Greeting.class);
log.info("Hello,"+name+"!");
}
}
GreetingTest.java
package com.greeting;
import org.junit.Test;
public class GreetingTest {
@Test
public void print() {
Greeting.print("Test");
}
}
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.csj2018</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<artifactId>greeting</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
2.2 模块hellomaven
App.java
package com.hellomaven;
import com.greeting.Greeting;
public class App {
public static void main(String[] args){
Greeting.print("Maven");
}
}
AppTest.java
package com.hellomaven;
import org.junit.Test;
public class AppTest {
@Test
public void main() {
App.main(null);
}
}
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.csj2018</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<!--<groupId>com.csj2018</groupId>继承parent,可以删除-->
<artifactId>hellomaven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.csj2018</groupId>
<artifactId>greeting</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.hellomaven.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.3 模块parent
模块greeting,hellomaven的pom文件高度相似,我们可以提取出共同部分作为parent,而模块greeting,hellomaven分别从parent继承,就可以简化greeting,hellomaven2个模块自身的配置。
parent的packaging是pom,不是jar,是因为parent本身不包含任何Java代码/
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.csj2018</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.verison>1.8</java.verison>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.10.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
运行hellomaven的App.java,结果如下
3.编译
进入hellomaven,编译提示找不到greeting的jar包
mvn clean compile
![](https://img2018.cnblogs.com/blog/1418970/201907/1418970-20190705130525277-1050414464.png)
<groupId>com.csj2018</groupId>
<artifactId>build</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../parent</module>
<module>../greeting</module>
<module>../hellomaven</module>
</modules>
![](https://img2018.cnblogs.com/blog/1418970/201907/1418970-20190705131333887-1958378430.png)
![](https://img2018.cnblogs.com/blog/1418970/201907/1418970-20190705132539131-599347514.png)
4. 总结:
- maven支持模块化管理,可以把一个大项目拆成几个模块
- 可以通过继承在parent的pom.xml同一定义重复配置
- 可以通过
编译多个模块