解决 CheckStyle 插件中的依赖

Resolve dependency in CheckStyle plugin

根据文档,我正在运行时升级 CheckStyle 插件。

但是,我的本地和 CI/CD 构建失败了,因为它似乎在插件升级中发生了依赖项解析,不遵守 pom.xml.[=14= 中定义的 repositories ]

有问题的依赖项包含公司 checkstyle 文件。

注意:我什至尝试在插件外部添加依赖项,如下所示。没有骰子。

关于如何解决这个问题的任何提示?日志遵循配置。

<repositories>
    <repository>
        <id>checkstyle-repo</id>
        <url>https://repo/where/dependency/resides</url>
    </repository>
    <repository>
        <id>company-repo</id>
        <url>http://nexus-01.co.lan/content/groups/CompanyRepository/</url>
    </repository>
</repositories>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>3.1.2</version>
        <dependencies>
            <dependency>
                <groupId>com.puppycrawl.tools</groupId>
                <artifactId>checkstyle</artifactId>
                <version>8.41</version>
            </dependency>
            <dependency>
                <groupId>com.company</groupId>
                <artifactId>co-checkstyles</artifactId>
                <version>1.0.4</version>
            </dependency>
        </dependencies>
        <configuration>
            <configLocation>company_transitional_checkstyle.xml</configLocation>
            <encoding>UTF-8</encoding>
            <failOnViolation>true</failOnViolation>
        </configuration>
        <executions>
            <execution>
                <id>validate</id>
                <phase>validate</phase>
                <goals>
                    <goal>check</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
...
</plugins>
...
...
<dependencies>
...
    <dependency>
        <groupId>com.company</groupId>
        <artifactId>co-checkstyles</artifactId>
        <version>1.0.4</version>
    </dependency>
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.985 s
[INFO] Finished at: 2021-03-11T19:14:33-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.2:check (validate) 
on project tax-service: Execution validate of goal org.apache.maven.plugins:maven-checkstyle-plugin:3.1.2:check failed: 
Plugin org.apache.maven.plugins:maven-checkstyle-plugin:3.1.2 or one of its dependencies could not be resolved: 
Failure to find com.company:co-checkstyles:jar:1.0.4 in http://nexus-01.co.lan/content/groups/CompanyRepository/ was cached in the local repository, 
resolution will not be reattempted until the update interval of corepository has elapsed or updates are forced -> [Help 1]
[ERROR] 

已解决。

mvn -X 获胜。

可是神器啊!蝙蝠侠...是的,这不是很好理解,或者可能只是没有很好的记录,而且我使用 Maven 将近 13 年,从 1.0 的后期和第一个 2.0 版本开始。我不记得在生命周期中遇到过这种配置细微差别

在不深入研究杂草的情况下,maven 依赖项解析有两 (2) 种模式。

  1. 通过 repository
  2. 的项目依赖
  3. 插件 依赖项 通过 pluginRepository

在像 CheckStyle 这样的插件的情况下,类路径可以“在运行时升级”以使用最新的 checkstyle 版本,从 pluginRepository.

解析依赖关系

在工作中,我们通常不会在我们的 POM 中定义 pluginRepositories 并允许 Maven“默认”到项目 repositories,例如 corepositorynexus-01.

mvn 运行时打开调试 -X 让我得到了这一点信息...

11835 [DEBUG] === PROJECT BUILD PLAN ================================================
11835 [DEBUG] Project:       com.company:tax-service:1.1.3.36cc94b4
11835 [DEBUG] Dependencies (collect): [runtime]
11835 [DEBUG] Dependencies (resolve): [compile, compile+runtime, runtime, test]
11835 [DEBUG] Repositories (dependencies): [gitlab-co-checkstyle (https://gitlab.com/api/v4/projects/nnnnnnnn/packages/maven, default, releases+snapshots), corepository (http://nexus-01.co.lan/content/groups/CompanyRepository/, default, releases)]
11836 [DEBUG] Repositories (plugins)     : [corepository (http://nexus-01.co.lan/content/groups/CompanyRepository/, default, releases)]

BOOM...CheckStyle 插件解决了 pluginRepository!这点信息告诉我所有我需要的。

我修补了 ci_settings.xml,一切正常。

11835 [DEBUG] === PROJECT BUILD PLAN ================================================
11835 [DEBUG] Project:       com.company:tax-service:1.1.3.36cc94b4
11835 [DEBUG] Dependencies (collect): [runtime]
11835 [DEBUG] Dependencies (resolve): [compile, compile+runtime, runtime, test]
11835 [DEBUG] Repositories (dependencies): [gitlab-co-checkstyle (https://gitlab.com/api/v4/projects/nnnnnnnn/packages/maven, default, releases+snapshots), corepository (http://nexus-01.co.lan/content/groups/CompanyRepository/, default, releases)]
11836 [DEBUG] Repositories (plugins)     : [gitlab-co-checkstyle (https://gitlab.com/api/v4/projects/nnnnnnnn/packages/maven, default, releases+snapshots), corepository (http://nexus-01.co.lan/content/groups/CompanyRepository/, default, releases)]