新建maven父子工程,然后新建子模块A和子模块B,在A模块随便写几个带有system.out.println函数代码,配置子模块B依赖子模块A,子模块B内调用子模块A的输出测试内容的函数。对子模块B进行clean package,如果不对父工程执行install,对子模块B进行clean package就会出现这个以下错误。
Could not resolve dependencies for project com.test:module-b:jar:1.0.1: The following artifacts could not be resolved: com.test.module-a:jar:1.0.1: Could not find artifact com.test:module-a:jar:1.0.1 in alimaven (http://maven.aliyun.com/nexus/content/repositories/central/)
由于com.test.module-a没有安装到本地仓库,然后不知道为什么maven去阿里云的maven仓库去找了,但com.test.module-a是同属父模块下的子模块。
为什么对子模块进行clean package,打包过程中对父子工程的模块之间引用依赖的访问加载一定要经过本地仓库或者其他远程仓库?
所以子模块B依赖子模块A。对子模块B进行clean package,子模块B依赖子模块A在关联上没有install到本地仓库中。这个情况下如何对子模块B打包成功?
验证成功条件:
把maven父子工程复制一份项目副本,把副本项目中的子模块A中带有system.out.println的函数代码中修改输出内容,然后对副本的子模块B执行clean package在部署运行,看看副本和原来的在部署输出的测试内容上是不是不一样的。
父模块:
<?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>com.test</groupId>
<artifactId>test_box1</artifactId>
<version>1.0.1</version>
<packaging>pom</packaging>
<modules>
<module>module-a</module>
<module>module-b</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>module-a</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
</dependencies>
</project>
子模块A:
<?xml version="1.0"?>
<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.test</groupId>
<artifactId>test_box1</artifactId>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath> <!-- 相对于父工程的路径 -->
</parent>
<artifactId>module-a</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
</project>
子模块B:
<?xml version="1.0"?>
<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.test</groupId>
<artifactId>test_box1</artifactId>
<version>1.0.1</version>
<relativePath>../pom.xml</relativePath> <!-- 相对于父工程的路径 -->
</parent>
<artifactId>module-b</artifactId>
<packaging>jar</packaging>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>module-a</artifactId>
</dependency>
</dependencies>
<build>
<finalName>module_b</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<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>
<finalName>demo</finalName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>start.AppStart1</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
你可以在父模块的pom.xml配置打包插件,然后在父模块进行clean package,这样可以不经过仓库打包。比如我这里child2是依赖child1的,父模块的pom这样设置即可