首先 创建一个mvn项目, 直接在命令行执行, 原型生成:
mvn archetype:generate
选一个maven quick start的template, 然后删除src和target文件夹
在pom.xml里面version 下面加上<packing>pom</packing>
在此目录中再次执行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
建好的项目文件在:
https://files.cnblogs.com/files/hualiu0/mvnparent.7z?t=1698752418\&download=true