我是否可以捆绑导入控制文件以使用Checkstyle在所有项目中使用Checkstyle?

时间:2019-06-28 13:36:06

标签: maven checkstyle maven-checkstyle-plugin

我试图以这种方式将import-control添加到我们的checkstyle中,使得import-control文件存在于制作checstyle.xml文件的项目中,而不存在于我们稍后构建的项目中。

我们有一个特定的gradle项目,其中定义了所有规则,并且在此项目中是import-control.xml。我的问题是,当我尝试在使用此Checkstyle的另一个项目上运行mvn clean install时,它将尝试在该项目中找到import-control.xml。

我在checkstyle.xml中进行了以下配置:

        <module name="ImportControl">
            <property name="file" value="import-control.xml"/>
        </module>

,然后将import-control.xml放在checkstyle.xml旁边。

谁能告诉我我需要做什么,以便我可以告诉maven该文件存在于我们的checkstyle项目中,而不存在于正在构建的根项目中?

我遇到的错误是: 无法初始化模块TreeWalker-无法初始化模块ImportControl-属性“文件”的非法值'import-control.xml'找不到:import-control.xml

在2.17版中

无法加载import-control.xml:无法找到文件:/ C://import-control.xml:\ import-control.xml

我尝试过的事情: 将checkstyle版本升级到3.1.0(我们以前有2.17) 使用import-control.xml,但不起作用。 试图阅读文档和代码,但没有帮助。

感谢您的帮助

稍后再写给您/Mårten

MVN配置:

      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-checkstyle-plugin</artifactId>
          <version>3.1.0</version>
          <executions>
            <execution>
              <id>do checkstyle</id>
              <phase>process-sources</phase>
              <goals>
                <goal>check</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <includes>projectA/**/*</includes>
            <configLocation>checkstyle.xml</configLocation>
            <consoleOutput>true</consoleOutput>
            <failOnViolation>false</failOnViolation>
            <failsOnError>true</failsOnError>
            <includeTestSourceDirectory>true</includeTestSourceDirectory>
          </configuration>
          <dependencies>
            <dependency>
              <groupId>company.checkstyle</groupId>
              <artifactId>company-checkstyle</artifactId>
              <version>0.2-SNAPSHOT</version>
            </dependency>
          </dependencies>
        </plugin>
      </plugins>
    </pluginManagement>```

1 个答案:

答案 0 :(得分:1)

再次感谢barfuin,看来$ {config_loc}是答案,但我们还需要一件事情来使其完全工作。

因此,为了从checkstyle项目中添加资源,例如在此文件中,我在checkstyle.xml中执行了import_control.xml:

    <module name="ImportControl">
        <property name="file" value="${config_loc}/config/import_control.xml"/>
    </module>

我还需要添加:

<propertyExpansion>config_loc=</propertyExpansion>

在我的pom.xml配置中,这解决了以下问题:未定义config_loc,并让checkstyle将文件作为资源查找,并为我提供了以下pom.xml配置:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-checkstyle-plugin</artifactId>
      <executions>
        <execution>
          <id>do checkstyle</id>
          <phase>process-sources</phase>
          <goals>
            <goal>check</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <includes>projectA/**/*</includes>
        <configLocation>checkstyle.xml</configLocation>
        <consoleOutput>true</consoleOutput>
        <failOnViolation>false</failOnViolation>
        <failsOnError>true</failsOnError>
        <includeTestSourceDirectory>true</includeTestSourceDirectory>
        <propertyExpansion>config_loc=</propertyExpansion>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>company.checkstyle</groupId>
          <artifactId>company-checkstyle</artifactId>
          <version>0.2-SNAPSHOT</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>