如何在 Maven 中对所有模块使用单个 checkstyle 抑制文件

发布于 2024-08-04 13:48:03 字数 1014 浏览 4 评论 0原文

我有一个由多个 Maven 模块组成的项目,这些模块都是父模块的子模块。 我将父模块设置为使用 checkstyle,并且子模块都正确继承了此行为。我希望所有子模块都使用其插件中定义的父级抑制文件。 我定义了一个在 checkstyle 插件中使用的属性 checkstyle.suppression

<properties>
  <checkstyle.suppressions>${basedir}\src\checkstyle\suppressions.xml</checkstyle.suppressions>
</properties>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <configLocation>config/sun_checks.xml</configLocation>
      <suppressionsLocation>${checkstyle.suppressions}</suppressionsLocation>
      <suppressionsFileExpression>${checkstyle.suppressions}</suppressionsFileExpression>
    </configuration>
  </plugin>
</plugins>

它对父模块工作正常,但所有子模块都尝试在其 basedir 中查找该文件,这确实使感觉。
我确信我缺少一个简单的解决方案,但是有没有办法定义这个位置,以便所有子模块都将使用父位置而无需对其进行硬编码?

I have a project that consists of several Maven modules which are all children of a parent module.
I have the parent set up to use checkstyle and the child modules all inherit this behaviour correctly. I would like all the child modules to use the parents suppression file defined in its plugin.
I define a property checkstyle.suppression which is used in the checkstyle plugin

<properties>
  <checkstyle.suppressions>${basedir}\src\checkstyle\suppressions.xml</checkstyle.suppressions>
</properties>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <configLocation>config/sun_checks.xml</configLocation>
      <suppressionsLocation>${checkstyle.suppressions}</suppressionsLocation>
      <suppressionsFileExpression>${checkstyle.suppressions}</suppressionsFileExpression>
    </configuration>
  </plugin>
</plugins>

Which works fine for the parent but all the child modules try to find the file in their basedir which does make sense.
I am sure there must be a simple solution I am missing but is there a way to define this location so that all the child modules will use the parent location without hard coding it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

百思不得你姐 2024-08-11 13:48:03

上面的答案很危险。我认为每个项目都应该是独立的,因此引用外部文件迟早会破坏构建。 Checkstyle 可以获取文件的 url,但这意味着您无法离线构建。更好的方法是将文件(也可以添加 pmd.xml)打包到 jar 中,然后将该 jar 添加到 checkstyle(或 pmd)插件的类路径中。我有一个例子 这里以及有关覆盖插件类路径的更多信息这里

The answers above are dangerous. I maintain that each project should be self contained, so referring to files external to it is going to break a build sooner or later. Checkstyle can take a url for the file but that means you can't build offline. A better approach is to package your file (can also add pmd.xml) into a jar and then add that jar to the classpath of the checkstyle (or pmd) plugin. I have an example of it here and more about overridding a plugin classpath here

花间憩 2024-08-11 13:48:03

您是否尝试过在父 pom 中定义这样的属性或在子 pom 中重新定义它?

 <properties>
   <checkstyle.suppressions>${parent.project.basedir}\src\checkstyle\suppressions.xml</checkstyle.suppressions>
 </properties>

Have you tried defining the property like this in the parent pom or redefining it in the childrens?

 <properties>
   <checkstyle.suppressions>${parent.project.basedir}\src\checkstyle\suppressions.xml</checkstyle.suppressions>
 </properties>
蓝眼泪 2024-08-11 13:48:03

如果父级不打算运行 checkstyle,您可能可以将其重写为

<properties>
  <checkstyle.suppressions>..\..\src\checkstyle\suppressions.xml</checkstyle.suppressions>
</properties>

Or 类似的内容。或者您可以在 settings.xml 中添加一些内容,将所有内容指向系统范围的配置目录。

虽然可能不建议这样做,但您可以使用引导程序或设置项目或任务将您的suppressions.xml文件的副本放置到settings.xml中的属性指定的位置,然后始终通过该位置引用它地点。

If the parent isn't going to run checkstyle, you might just be able to rewrite it to

<properties>
  <checkstyle.suppressions>..\..\src\checkstyle\suppressions.xml</checkstyle.suppressions>
</properties>

Or something like this. Or you could put something in settings.xml to point everything to an system wide config directory.

While it might not be recommended, you can have use a boot-strap or set-up project or task put a copy of the suppressions.xml file to a location specified by a property in settings.xml and then always refer to it by that locations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文