在Java項目中,咱們一般會會把一個項目分拆爲模塊,這是爲了下降軟件複雜度。
例如:咱們能夠把一個大的項目氛圍module-a, module-b, module-c三個模塊,這樣咱們就能夠下降軟件的複雜度。
maven能夠有效管理多個模塊java
不要忘了log4j2.xml文件
Greeting.javashell
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.javaapache
package com.greeting; import org.junit.Test; public class GreetingTest { @Test public void print() { Greeting.print("Test"); } }
pom.xmlmaven
<?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>
App.java模塊化
package com.hellomaven; import com.greeting.Greeting; public class App { public static void main(String[] args){ Greeting.print("Maven"); } }
AppTest.javaui
package com.hellomaven; import org.junit.Test; public class AppTest { @Test public void main() { App.main(null); } }
pom.xmlcode
<?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>
模塊greeting,hellomaven的pom文件高度類似,咱們能夠提取出共同部分做爲parent,而模塊greeting,hellomaven分別從parent繼承,就能夠簡化greeting,hellomaven2個模塊自身的配置。
parent的packaging是pom,不是jar,是由於parent自己不包含任何Java代碼/
pom.xmlorm
<?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,結果以下
xml
進入hellomaven,編譯提示找不到greeting的jar包blog
mvn clean compile
若是模塊A依賴模塊B,則模塊須要模塊B的jar包才能正常編譯
中央倉庫:發佈者將模塊B發佈到maven中央倉庫,模塊A從中央倉庫獲取模塊B。
私有倉庫:內網中,發佈者將模塊B發佈到私有倉庫,模塊A從中央倉庫獲取模塊B。
本地倉庫:發佈者使用mvn clean install將模塊B發佈到maven本地倉庫,模塊A從本地倉庫獲取模塊B。不推薦,由於每次修改模塊B源碼,都須要從新安裝。會出現版本不一致的狀況。
模塊化編譯:
<modules> <module>模塊A</module> <module>模塊B</module> </modules>
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>build</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>../parent</module> <module>../greeting</module> <module>../hellomaven</module> </modules> </project>
進入build根目錄,再次編譯成功
打包,運行jar包
mvn clean package #進入hellomaven/target目錄下,運行jar包 cd ../hellomaven/target java -jar hellomaven-1.0-SNAPSHOT.jar