首先 建立一個mvn專案, 直接在命令列執行, 原型生成:
mvn archetype:generate
選一個maven quick start的template, 然後刪除src和target資料夾
在pom.xml裡面version 下面加上<packing>pom</packing> (maven 的三種打包方式: pom為打包依賴方式, jar檔案,或者 war檔案)
在此目錄中再次執行mvn archetype:generate, 構件artifactId選為child1, 完成後自動在mvnparent目錄的Pom中新增了<modules> 節點
再次在mvnparent目錄執行mvn archetype:generate 生成child2專案, 會在mvnparent的pom.xml中新增child2的module, 把裡面沒用的build 節點都刪掉
<?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> <artifactId>mvnparent</artifactId> <groupId>org.caloch</groupId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>child2</artifactId> <packaging>jar</packaging> <name>child2</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> </project>
mvnchild1參照了child2專案的pom.xml, 裡面刪除groupid和version兩個,它們會繼承parent, packaging改為jar
<?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> <artifactId>mvnparent</artifactId> <groupId>org.caloch</groupId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>mvnchild1</artifactId> <packaging>jar</packaging> <name>mvnchild1</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.caloch</groupId> <artifactId>child2</artifactId> <version>${project.version}</version> </dependency> </dependencies> </project>
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <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.caloch</groupId> <artifactId>mvnparent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>mvnparent</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <modules> <module>mvnchild1</module> <module>child2</module> </modules> </project>
將專案匯入idea中可以偵錯, 執行為 java -cp "加上另外專案的class檔案路徑"
D:\source\repos\mvn_multi_module\mvnparent\mvnchild1> java -cp "D:\source\repos\mvn_multi_module\mvnparent\mvnchild1\target\classes;D:\source\repos\mvn_multi_module\mvnparent\child2\target\classes;" child1.App
mvn compile
mvn package
需要指定main class
指定時 java -jar xx.jar可以執行
不指定main class時, 使用java -cp xx.jar {maiclass} 來執行
構建, 在child1的pom裡面新增build
<build> <plugins> <!--打包普通專案--> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <!-- 可以指定打包的Main類,也可以不指定--> <!--指定了某個主類的話,使用: java -jar xxx.jar 引數 來執行--> <!--不指定主類的話使用:java -cp xxx.jar 類的路徑 引數 來執行,注意這裡需要手動指定執行類--> <!-- <archive>--> <!-- <manifest>--> <!-- <!–這裡要替換成jar包main方法所在類 –>--> <!-- <mainClass>GetName</mainClass>--> <!-- </manifest>--> <!-- <manifestEntries>--> <!-- <!–上面指定類的路徑–>--> <!-- <Class-Path>./src/main/java</Class-Path>--> <!-- </manifestEntries>--> <!-- </archive>--> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- 指定在打包節點執行jar包合併操作 --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
執行,注意要執行和dependency一起打包的jar才可以不需要額外新增另外一個包的class path, 否則還是要加class path -cp選項:
cd D:\source\repos\mvn_multi_module\mvnparent\mvnchild1\target java -cp .\mvnchild1-1.0-SNAPSHOT-jar-with-dependencies.jar child1.App Hello util in child2
關於參照兩個jar,裡面的包名和類名都相同的問題: 按搜尋,只需要改變參照的順序就會造成後一個覆蓋前一個的結果
建好的專案檔案在:
https://files.cnblogs.com/files/hualiu0/mvnparent.7z?t=1698752418&download=true