自定义 springboot starter 时遇到的坑

849 阅读2分钟

我的项目是一个三层结构,上一层是下一层的parent,结构是这样的:
| my-project
|---- my-spring-boot
|-------- my-spring-boot-autoconfigure
|-------- my-spring-boot-starter

在编译引入 my-spring-boot-starter 时遇到以下几个问题,记录分享一下。

my-spring-boot-autoconfigure 项目 mvn install 时报错 Could not find artifact xxx in snapshots

详细错误信息:Could not find artifact com.basic:my-spring-boot:pom:0.0.1-SNAPSHOT in snapshots (artifactory.xx.com/libs-snapsh…)

这个错误的意思是在远程仓库中找不到 my-spring-boot-autoconfigure 的 parent 项目。所以我们需要先mvn install my-spring-boot 项目,打包上传到本地或远程仓库即可。

同理,想编译 my-spring-boot 就需要先 mvn install 他的 parent 项目,也就是 my-project 项目,否则也会报同样的错误。

my-project 项目 mvn install 时报错 Non-resolvable parent POM for xxx was not found xxx

详细的错误是:

[ERROR] The project com.basic:my-spring-boot-autoconfigure:0.0.1-SNAPSHOT (xxx/my-spring-boot/my-spring-boot-autoconfigure/pom.xml) has 1 error

[ERROR] Non-resolvable parent POM for com.basic:my-spring-boot-autoconfigure:0.0.1-SNAPSHOT: com.basic:my-spring-boot:pom:0.0.1-SNAPSHOT was not found in artifactory.xx.com/libs-snapsh… during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of snapshots has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM @ line 7, column 13 -> [Help 2]

这个错误的意思和上面提到的错误是同一款,说的是找不到 my-spring-boot-autoconfigure 的 parent pom。

这里的错误信息还提到了一个标签parent.relativePath,也就是<relativePath>标签,他用于指定从哪里查找 parent,relativePath查找顺序是:relativePath元素中的地址–本地仓库–远程仓库。

如实使用空标签,也就是<relativePath/>设定一个空值,那它将始终从仓库中获取,不从本地路径获取parent。

解决这个问题的步骤是:
1、自上而下,先将顶层 my-project 的 <modules> 移除,然后 mvn install,确保本地mvn仓库中生成了相关jar包,然后再还原<modules>的配置。

2、同上,再将 my-spring-boot 的 <modules> 移除、mvn install、还原配置。

3、最后,依次 mvn install my-spring-boot-autoconfigure、my-spring-boot-starter 项目

总结一下就是,新项目在创建时需要先 mvn install 再添加 module,保证至少本地有仓库中存在相关jar 包

xxx was not found in xxx during a previous attempt. 异常

详细的错误信息是:

com.basic:my-spring-boot:pom:${revision} was not found in artifactory.xx.com/libs-releas… during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of central has elapsed or updates are forced.

原因还是在远程仓库找不到相关jar包。

解决方式:

  • 如果远程仓库有这个jar包,我们本地没有拉取到,可以配置settings.xml开启强制更新,每次都更新依赖库,代码示例如下:
<repositories>
        <repository>
                <id>central</id>
                <url>http://central</url>
                <releases>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                </releases>
                <snapshots>
                        <enabled>true</enabled>
                        <updatePolicy>always</updatePolicy>
                </snapshots>
        </repository>
</repositories>
  • 如果新项目,相关jar包没有上传到远程仓库,那我们要注意在配置pom坐标的时候不要使用类似${reversion}这样的引用版本号,改为明确的版本号。并且在子module中声明出version版本。代码类似:
    <!--my-project-->
    <groupId>com.basic</groupId>
    <artifactId>my-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>my-spring-boot</module>
    </modules>

    <!--my-spring-boot-->
    <parent>
        <groupId>com.basic</groupId>
        <artifactId>my-project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>my-spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>pom</packaging>

    <!--my-spring-boot-autoconfigure/my-spring-boot-starter-->
    <parent>
        <artifactId>my-spring-boot</artifactId>
        <groupId>com.basic</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>my-spring-boot-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>

在项目中引入了 my-spring-boot-starter 但依赖传递没有生效,没有加载 my-spring-boot-autoconfigure jar包

解决方式:

  • 检查下本地仓库是否存在 my-spring-boot-autoconfigure jar 包,如果不存在的话尝试单独引入 my-spring-boot-autoconfigure jar 包,排查问题。
  • 在 my-spring-boot-starter/src/main/resource/META-INF/ 下创建 spring.provides 文件,写入你的autoconfigure依赖,文件示例如下:

spring.provides

provides: my-spring-boot-autoconfigure

远程仓库(nexus)无法上传 snapshot jar包

这个只能使用命令上传了:

mvn deploy:deploy-file -DgroupId=com.basic -DartifactId=my-spring-boot-starter -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -Dfile=my-spring-boot-starter-0.0.1-SNAPSHOT.jar -Durl=http://localhost:8081/repository/3rd_part_snapshot/ -DrepositoryId=local-snapshots