可以配置 Checkstyle 插件以在大型多模块项目中使用,但需要一些设置。
这个例子将使用一个名为whizbang的神秘项目。这是该项目的结构:
whizbang |-- pom.xml |-- core | `-- pom.xml |-- gui | `-- pom.xml |-- jmx | `-- pom.xml `-- src
我们将从添加另一个子项目开始,该子项目将容纳我们的通用配置。我们称之为build-tools。我们在其中放置了我们想要包含的资源。在此示例中,我们将为 Checkstyle 插件添加配置文件。如果您愿意,可以将其他插件(如 PMD 插件)的配置文件包含在同一子项目中。
whizbang |-- pom.xml |-- build-tools | |-- src | | `-- main | | `-- resources | | `-- whizbang | | |-- checkstyle.xml | | `-- LICENSE.TXT | `-- pom.xml |-- core |-- gui |-- jmx `-- src
提示:将资源放入一个子目录中,您可以确保它是唯一的并且不会与其他任何人冲突。
现在我们可以在顶级pom.xml中包含 Checkstyle 配置。
注意:您必须在pom.xml的<build>元素中指定对build-tools的插件依赖项。它在<reporting>元素中不起作用,因为<reporting>不支持插件依赖项。其余的配置在<reporting>元素中以正常方式完成。
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.whizbang</groupId>
<artifactId>whizbang-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>WhizBang Parent</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.3</version>
<dependencies>
<dependency>
<groupId>com.example.whizbang</groupId>
<artifactId>build-tools</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.3</version>
<configuration>
<configLocation>whizbang/checkstyle.xml</configLocation>
<headerLocation>whizbang/LICENSE.txt</headerLocation>
</configuration>
</plugin>
</plugins>
</reporting>
<modules>
<module>build-tools</module>
<module>core</module>
<module>jmx</module>
<module>gui</module>
</modules>
</project>
完成此操作后,请确保不要在子模块中包含 Maven Checkstyle 插件,因为它们的定义和配置将覆盖顶级父 pom 的定义。
根据上面的 Checkstyle Plugin 配置,configLocation和headerLocation的值将从类路径中解析。当build-tools JAR 被声明为插件的依赖项时,它被包含在类路径中。
最后,开始构建网站。
mvn site
现在,每个子项目都将使用相同的 Checkstyle 设置和配置。