maven本地父子工程,对依赖其他子模块的子模块进行打包,可以不经过本地仓库吗?

新建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>

阅读 2k
avatarAI BotBETA

在Maven中,模块之间的依赖关系默认是通过Maven的依赖管理机制来解决的,这通常意味着依赖的模块需要被安装到本地仓库(或者通过远程仓库解析)。但是,当你有一个包含多个模块的Maven项目(父子项目),并且你正在开发过程中(即,你还没有将这些模块发布到任何远程仓库),你可以通过配置Maven来直接在项目内部解析这些依赖,而不需要先安装到本地仓库。

为了实现这一点,你可以在父项目的pom.xml中配置<modules>元素来列出所有子模块,并确保你在命令行中从父项目的根目录运行Maven命令。这样,Maven就会知道如何解析这些子模块之间的依赖关系,而不需要通过本地或远程仓库。

在你的情况下,你应该能够直接从父项目的根目录运行mvn clean package命令来打包子模块B,而不需要先单独安装子模块A。Maven会首先构建子模块A(因为它被B所依赖),然后构建子模块B。

但是,如果你确实遇到了问题,并且Maven试图从远程仓库(如阿里云仓库)中解析子模块A,那么可能有几个原因:

  1. 你的Maven设置或项目配置可能有问题:确保你的pom.xml文件正确配置了模块依赖,并且你在父项目的根目录下运行Maven命令。
  2. Maven的多模块构建可能没有正确工作:有时,Maven可能无法正确识别多模块项目的结构。确保你的父pom.xml文件中包含了<modules>元素,并且列出了所有子模块。
  3. 你的IDE配置可能有问题:如果你在使用IDE(如IntelliJ IDEA或Eclipse),确保你的IDE项目设置正确,以便它能够识别多模块项目的结构。

为了验证你的设置是否正确,你可以尝试以下步骤:

  1. 确保你的父pom.xml文件包含了<modules>元素,并且列出了所有子模块。
  2. 在命令行中,从父项目的根目录运行mvn clean package命令。
  3. 检查构建输出,以确保Maven正确构建了子模块A,并且没有尝试从远程仓库中解析它。

如果以上步骤都不能解决问题,你可能需要进一步检查你的Maven设置和项目配置,或者考虑在Stack Overflow等社区中寻求更具体的帮助。

最后,关于你的验证成功条件,如果你按照上述步骤成功打包了子模块B,并且你能够修改子模块A中的代码并在不重新安装A的情况下重新打包B以查看更改,那么你应该已经满足了你的验证成功条件。

1 个回答
新手上路,请多包涵

你可以在父模块的pom.xml配置打包插件,然后在父模块进行clean package,这样可以不经过仓库打包。比如我这里child2是依赖child1的,父模块的pom这样设置即可

    <modules>
        <module>child1</module>
        <module>child2</module>
    </modules>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <build>
        <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>
                <executions>
                    <execution>
                        <configuration>
                            <excludes>
                                <exclude>module-info.java</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题