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

时间:2017-09-14 05:42:01

标签: java maven maven-plugin checkstyle

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

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

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

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

Root 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>

build-tools 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-tools模块?

2 个答案:

答案 0 :(得分:1)

您当前的设置无效!当我克隆你的项目并尝试构建时,它肯定会失败,因为我在构建父pom时在我的本地存储库中没有build-tool工件。

这是你应该拥有的结构:

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

build-tools / pom.xml as

<?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

答案 1 :(得分:0)

收到same document。您应尝试将插件设置与<dependency>标记中的<build><reporting>标记中)配置为:

<reporting>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <version>2.17</version>
      <configuration>
         <configLocation>default-checkstyle.xml</configLocation>
         <encoding>UTF-8</encoding>
         <consoleOutput>true</consoleOutput>
         <failOnViolation>true</failOnViolation>
      </configuration>
    </plugin>
  </plugins>
</reporting>