springboot多模块打包报错解决办法

  在springboot开发中如果项目比较复杂,我们会想到把项目拆解成多个模块,也就是形成一个多层级模块的maven项目。例如现在我们的项目组成是一个父模块litchi,两个子模块litchi-common和litchi-admin。其中litchi-common模块是一个公共模块,里面定义了一些公共的方法操作,litchi-admin模块是一个应用模块,litchi-admin中定义了对litchi-common的依赖。整个项目非常清晰,构建也很简单,一切都很顺利,在eclipse中也可以正常运行。

 

litchi的pom

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath />
    </parent>
    <groupId>com.color</groupId>
    <artifactId>litchi</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>
    <name>litchi</name>
    <description>project for Spring Boot</description>

    <modules>
        <module>litchi-common</module>
        <module>litchi-admin</module>
    </modules>

litchi-common的pom

    <parent>
        <groupId>com.color</groupId>
        <artifactId>litchi</artifactId>
        <version>0.0.1</version>
    </parent>

    <groupId>com.color.litchi</groupId>
    <artifactId>litchi-common</artifactId>
    <version>1.0.0</version>
    <name>litchi-common</name>
    <packaging>jar</packaging>

 

litchi-admin的pom

    <parent>
        <groupId>com.color</groupId>
        <artifactId>litchi</artifactId>
        <version>0.0.1</version>
    </parent>
    <packaging>jar</packaging>
    <groupId>com.color.litchi</groupId>
    <artifactId>litchi-admin</artifactId>
    <name>litchi-admin</name>
    <version>1.0.0</version>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.color.litchi</groupId>
            <artifactId>litchi-common</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

 

现在需要对litchi-admin进行打包

1、由于litchi-admin对litchi-common的依赖,所以先把litchi-common安装到本地仓库,执行maven命令install,安装成功,本地仓库目录已经出现对应的jar包了

这里需要注意一点,litchi-common的pom中不要定义任何插件,删除build标签,程序会自动调用默认的插件maven-compiler-plugin进行打包

 

 2、对litchi-admin执行package命令,这个时候错误来了,报错信息如下:

Could not resolve dependencies for project com.color.litchi:litchi-admin:jar:1.0.0: Failed to collect dependencies at com.color.litchi:litchi-common:jar:1.0.0: Failed to read artifact descriptor for com.color.litchi:litchi-common:jar:1.0.0: Failure to find com.color:litchi:pom:0.0.1 in http://maven.aliyun.com/nexus/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of Sinotrans-Repositories has elapsed or updates are forced

报错的原因看起来很清晰,litchi-common加载不成功。明明上一步已经安装到本地仓库了呀,为什么会出现这个问题呢?查了一下资料,各种大神纷纷登场,有说删除litchi-common本地仓库中_remote.repositories文件的,有说使用各种各种的插件的,各种办法都尝试了仍然无法解决。

自己静下来仔细想了一下流程:按理说maven在打包的时候默认会优先从本地仓库查找相应的jar包然后才会去远程仓库查找下载或者更新。从报错来看,打包的时候直接就从远程仓库开始下载,完全无视本地的存在。在确信本地jar没有问题的情况下,看来需要强行让程序从本地加载这个jar包,好了直接上解决方案,修改litchi-admin的pom

1、依赖中增加scope、systemPath标签指定jar的本地路径

2、build标签下增加resources把本地jar打包到生成的jar中

    <dependencies>
        <dependency>
            <groupId>com.color.litchi</groupId>
            <artifactId>litchi-common</artifactId>
            <version>1.0.0</version>
            <scope>system</scope>
            <systemPath>D:\softWare\repository\com\color\litchi\litchi-common\1.0.0\litchi-common-1.0.0.jar</systemPath>
        </dependency>
    </dependencies>
    <build>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>D:\softWare\repository\com\color\litchi\litchi-common\1.0.0\</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>

    </build>

后记:maven为什么不能直接从本地仓库加载这个jar包的原因仍然不清楚,希望有大神留言说明!

posted @ 2020-05-28 17:10  colorfulCat  阅读(1629)  评论(0编辑  收藏  举报