我有一个具有CheckStyle设置的项目,该项目使用SuppressionFilter
将checkstyle_suppressions.xml
文件设置为忽略文件。
<module name="SuppressionFilter">
<property name="file" value="${config_loc}/checkstyle_suppressions.xml"/>
<property name="optional" value="false"/>
</module>
从项目checkstyle.xml
文件中引用了包含此SuppressionFilter
的{{1}}文件:
pom.xml
使用Java Eclipse执行CheckStyle检查时,所有功能均按预期运行。但是,在使用maven进行构建时,由于未设置<!-- Checkstyle plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>checkstyle</id>
<phase>test</phase>
<configuration>
<sourceDirectories>${project.compileSourceRoots}</sourceDirectories>
<testSourceDirectories>${project.testCompileSourceRoots}</testSourceDirectories>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<configLocation>${basedir}/checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.9</version>
</dependency>
</dependencies>
</plugin>
属性,因此构建失败。原始错误:
${config_loc}
在上述配置中将[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.0.0:check (checkstyle) on project MyProject: Failed during checkstyle execution: Failed during checkstyle configuration: unable to parse configuration stream: Property ${config_loc} has not been set -> [Help 1]
更改为${config_loc}/checkstyle_suppressions.xml
时,maven将按预期方式构建应用程序,但是Java Eclipse的CheckStyle插件不再能够解析该文件,在日志中打印以下异常:
!ENTRY net.sf.eclipsecs.core 4 0 2018-06-07 05:40:02.460 !MESSAGE Checkstyle-Plugin:无法初始化模块SuppressionFilter-无法找到:checkstyle_suppressions.xml !堆栈0 com.puppycrawl.tools.checkstyle.api.CheckstyleException:无法初始化模块SuppressionFilter-无法找到:checkstyle_suppressions.xml 在com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:460) 在com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:198) 在net.sf.eclipsecs.core.builder.CheckerFactory.createCheckerInternal(CheckerFactory.java:299) 在net.sf.eclipsecs.core.builder.CheckerFactory.createChecker(CheckerFactory.java:133) 在net.sf.eclipsecs.core.builder.Auditor.runAudit(Auditor.java:141) 在net.sf.eclipsecs.core.builder.CheckstyleBuilder.handleBuildSelection(CheckstyleBuilder.java:306) 在net.sf.eclipsecs.core.builder.CheckstyleBuilder.build(CheckstyleBuilder.java:172) 在org.eclipse.core.internal.events.BuildManager $ 2.run(BuildManager.java:735) 在org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) 在org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:206) 在org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:246) 在org.eclipse.core.internal.events.BuildManager $ 1.run(BuildManager.java:301) 在org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) 在org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:304) 在org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:360) 在org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:383) 在org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:142) 在org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:232) 在org.eclipse.core.internal.jobs.Worker.run(Worker.java:56) 引起原因:com.puppycrawl.tools.checkstyle.api.CheckstyleException:找不到:checkstyle_suppressions.xml 在com.puppycrawl.tools.checkstyle.utils.CommonUtils.getUriByFilename(CommonUtils.java:510) 在com.puppycrawl.tools.checkstyle.filters.SuppressionsLoader.loadSuppressions(SuppressionsLoader.java:202) 在com.puppycrawl.tools.checkstyle.filters.SuppressionFilter.finishLocalSetup(SuppressionFilter.java:100) 在com.puppycrawl.tools.checkstyle.api.AutomaticBean.configure(AutomaticBean.java:194) 在com.puppycrawl.tools.checkstyle.Checker.setupChild(Checker.java:455) ...另外18个
我试图在Maven pom中的多个位置设置此checkstyle_suppressions.xml
变量,但是没有成功。
如何设置我的CheckStyle配置,使其既适用于Maven又适用于Java Eclipse?我正在寻找一种可分配的解决方案,这意味着克隆或分叉此项目的其他开发人员将不会遇到此问题。
答案 0 :(得分:2)
我已经设法通过将<propertyExpansion>config_loc=${basedir}</propertyExpansion>
添加到项目pom.xml
文件中来解决此问题。对于有类似问题的任何人,现在都可以使用CheckStyle maven插件设置:
<!-- Checkstyle plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>checkstyle</id>
<phase>test</phase>
<configuration>
<sourceDirectories>${project.compileSourceRoots}</sourceDirectories>
<testSourceDirectories>${project.testCompileSourceRoots}</testSourceDirectories>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<configLocation>${basedir}/checkstyle.xml</configLocation>
<propertyExpansion>config_loc=${basedir}</propertyExpansion>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<linkXRef>false</linkXRef>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>8.9</version>
</dependency>
</dependencies>
</plugin>