gpt4 book ai didi

java - Maven - Checkstyle 对模块的依赖不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 03:32:29 24 4
gpt4 key购买 nike

项目链接:https://github.com/randomquestion/MultiModuleMavenCheckstyleSample

我有一个包含多个模块的非常简单的 Maven 项目。我正在关注 this Maven guide在其上启用 Checkstyle。所以我创建了一个名为 build-tools 的通用模块(根据指南)并在根 pom 文件中添加了 Checkstyle 配置。但是我遇到了一个非常奇怪的问题。

当我从头开始创建一个新的 Maven 项目时,mvn clean install似乎工作得很好,包括 Checkstyle。但是,一旦我重命名 checkstyle 文件并更新 pom.xml使用新名称,构建开始失败并出现错误 Could not find resource <new Checkstyle file name> .在那之后,我再也无法让它重新构建。问题似乎是对 build-tools 的依赖根 pom 中的模块不工作。

├── build-tools
│   ├── build-tools\ (1).iml
│   ├── pom.xml
│   ├── src
│      └── main
│       └── resources
│      └── default-checkstyle.xml
├── pom.xml

根 pom.xml:

<modules>
<module>build-tools</module>
</modules>

<properties>
<checkstyle.config.location>default-checkstyle.xml</checkstyle.config.location>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<dependencies>
<dependency>
<groupId>com.mycompany.app</groupId>
<artifactId>build-tools</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<configuration>
<configLocation>default-checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failOnViolation>true</failOnViolation>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

构建工具 pom.xml

<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.mycompany.app</groupId>
<artifactId>build-tools</artifactId>
<version>1.0-SNAPSHOT</version>
<name>build-tools</name>

错误:

INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] my-app
[INFO] build-tools
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ my-app ---
[INFO] Deleting /Users/../workspace/my-app/target
[INFO]
[INFO] --- maven-checkstyle-plugin:2.17:check (validate) @ my-app ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] my-app ............................................. FAILURE [ 1.170 s]
[INFO] build-tools ........................................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.291 s
[INFO] Finished at: 2017-09-13T22:25:26-07:00
[INFO] Final Memory: 12M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check (validate) on project my-app: Failed during checkstyle execution: Unable to find configuration file at location: default-checkstyle.xml: Could not find resource 'default-checkstyle.xml'. -> [Help 1]

有什么可能出错的建议吗?为什么是mvn install不 build build-tools模块优先?

最佳答案

您当前的设置将不起作用!当我克隆您的项目并尝试构建时,它肯定会失败,因为在构建父 pom 时我的本地存储库中没有 build-tool Artifact 。

这是你应该拥有的结构:

 -- build-tools
| |
| - pom.xml
|
- my app
| |
| - src
| - pom.xml
|
- pom.xml

这是父pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<name>my-app-parent</name>
<url>http://maven.apache.org</url>

<!--
~ Modules
-->
<modules>
<module>build-tools</module>
<module>my-app</module>
</modules>

<build>
<!--
~ Plugin Settings
-->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<dependencies>
<dependency>
<groupId>com.mycompany.app</groupId>
<artifactId>build-tools</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<configLocation>my-app-checkstyle.xml</configLocation>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<configuration>
<configLocation>default-checkstyle.xml</configLocation>
</configuration>
</plugin>
</plugins>
</reporting>
</project>

构建工具/pom.xml 为

<?xml version="1.0" encoding="UTF-8"?>
<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.mycompany.app</groupId>
<artifactId>my-app-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>build-tools</artifactId>
</project>

你的其他项目/pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>

<name>my-app</name>
<url>http://maven.apache.org</url>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

将 checkstyle 文件名重命名为 build-tools/src/main/resources/my-app-checkstyle.xml

关于java - Maven - Checkstyle 对模块的依赖不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46211370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com