diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle1/pom.xml b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle1/pom.xml new file mode 100644 index 0000000000..128abc5066 --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle1/pom.xml @@ -0,0 +1,34 @@ + + + + 4.0.0 + + + se.west.foobar + foobar-parent + 1.0.0-SNAPSHOT + + + se.west.foobar.codestyle1 + foobar-codestyle1 + jar + + diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle1/src/main/resources/checkstyle/checkstyle-suppressions.xml b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle1/src/main/resources/checkstyle/checkstyle-suppressions.xml new file mode 100644 index 0000000000..101d0c0878 --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle1/src/main/resources/checkstyle/checkstyle-suppressions.xml @@ -0,0 +1,27 @@ + + + + + + diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle1/src/main/resources/checkstyle/checkstyle.xml b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle1/src/main/resources/checkstyle/checkstyle.xml new file mode 100644 index 0000000000..c840424edf --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle1/src/main/resources/checkstyle/checkstyle.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle2/pom.xml b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle2/pom.xml new file mode 100644 index 0000000000..e2894ec47d --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle2/pom.xml @@ -0,0 +1,34 @@ + + + + 4.0.0 + + + se.west.foobar + foobar-parent + 1.0.0-SNAPSHOT + + + se.west.foobar.codestyle2 + foobar-codestyle2 + jar + + diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle2/src/main/resources/checkstyle/checkstyle-suppressions.xml b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle2/src/main/resources/checkstyle/checkstyle-suppressions.xml new file mode 100644 index 0000000000..c2327cd975 --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle2/src/main/resources/checkstyle/checkstyle-suppressions.xml @@ -0,0 +1,27 @@ + + + + + + diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle2/src/main/resources/checkstyle/checkstyle.xml b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle2/src/main/resources/checkstyle/checkstyle.xml new file mode 100644 index 0000000000..4380d7bdae --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/codestyle2/src/main/resources/checkstyle/checkstyle.xml @@ -0,0 +1,157 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/invoker.properties b/maven-checkstyle-plugin/src/it/multimodule-configlocation/invoker.properties new file mode 100644 index 0000000000..87cb87e6ba --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/invoker.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +invoker.goals=clean package site diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/module1/pom.xml b/maven-checkstyle-plugin/src/it/multimodule-configlocation/module1/pom.xml new file mode 100644 index 0000000000..1c2f8f5c71 --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/module1/pom.xml @@ -0,0 +1,63 @@ + + + + 4.0.0 + + + se.west.foobar + foobar-parent + 1.0.0-SNAPSHOT + + + se.west.foobar.module1 + foobar-module1 + jar + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + maven-checkstyle-plugin + + + + se.west.foobar.codestyle1 + foobar-codestyle1 + 1.0.0-SNAPSHOT + + + se.west.foobar.codestyle2 + foobar-codestyle2 + 1.0.0-SNAPSHOT + + + + + + diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/module1/src/main/java/se/west/foobar/module1/UsesIncorrectImports.java b/maven-checkstyle-plugin/src/it/multimodule-configlocation/module1/src/main/java/se/west/foobar/module1/UsesIncorrectImports.java new file mode 100644 index 0000000000..787c9f1f7d --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/module1/src/main/java/se/west/foobar/module1/UsesIncorrectImports.java @@ -0,0 +1,43 @@ +package se.west.foobar.module1; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Date; + +/** + * Class which uses an incorrect import, namely java.util.Date. + */ +public class UsesIncorrectImports +{ + private Date startOfDates; + + public UsesIncorrectImports() + { + this.startOfDates = new Date(0L); + } + + /** + * @return Retrieves a {@link Date} referring to the 1 of January 1970 (i.e. the "start of Dates" timestamp). + */ + public Date getStartOfDates() + { + return startOfDates; + } +} diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/module2/pom.xml b/maven-checkstyle-plugin/src/it/multimodule-configlocation/module2/pom.xml new file mode 100644 index 0000000000..32da5122ef --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/module2/pom.xml @@ -0,0 +1,64 @@ + + + + 4.0.0 + + + se.west.foobar + foobar-parent + 1.0.0-SNAPSHOT + + + se.west.foobar.module2 + foobar-module2 + jar + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + maven-checkstyle-plugin + + + + se.west.foobar.codestyle2 + foobar-codestyle2 + 1.0.0-SNAPSHOT + + + se.west.foobar.codestyle1 + foobar-codestyle1 + 1.0.0-SNAPSHOT + + + + + + + diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/module2/src/main/java/se/west/foobar/module2/UsesTooLongLine.java b/maven-checkstyle-plugin/src/it/multimodule-configlocation/module2/src/main/java/se/west/foobar/module2/UsesTooLongLine.java new file mode 100644 index 0000000000..c4e7ea92e2 --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/module2/src/main/java/se/west/foobar/module2/UsesTooLongLine.java @@ -0,0 +1,32 @@ +package se.west.foobar.module2; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Class which has a line being exactly 115 chars long. + * (This will create an exception with the checkstyle.xml found in the module "codestyle1", but not in "codestyle2"). + */ +public class UsesTooLongLine +{ + public String getGreeting() + { + return "This line is - in all - between 110 and 120 characters long; should generate exception in style1"; + } +} diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/pom.xml b/maven-checkstyle-plugin/src/it/multimodule-configlocation/pom.xml new file mode 100644 index 0000000000..6e733757ed --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/pom.xml @@ -0,0 +1,122 @@ + + + + 4.0.0 + se.west.foobar + foobar-parent + 1.0.0-SNAPSHOT + pom + + + Validate that configLocation is handled correctly, and according to description. + + + + + + 1.8 + UTF-8 + + + 6.16.1 + + + false + /checkstyle/checkstyle.xml + /checkstyle/checkstyle-suppressions.xml + + + + codestyle1 + codestyle2 + module1 + module2 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${jdk.version} + ${jdk.version} + + + + + maven-checkstyle-plugin + @pom.version@ + + true + ${project.build.sourceEncoding} + ${checkstyle.configLocation} + ${checkstyle.suppressionsLocation} + ${checkstyle.failOnViolation} + error + + + + com.puppycrawl.tools + checkstyle + ${checkstyle.version} + + + + + checkstyle_check + + check + + package + + + checkstyle_report + + checkstyle + + site + + + + + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + + + diff --git a/maven-checkstyle-plugin/src/it/multimodule-configlocation/verify.groovy b/maven-checkstyle-plugin/src/it/multimodule-configlocation/verify.groovy new file mode 100644 index 0000000000..b3981720e9 --- /dev/null +++ b/maven-checkstyle-plugin/src/it/multimodule-configlocation/verify.groovy @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +def assertCheckstyleErrorExists(final File theFile, + final String[] fileContent, + final int lineNumber, + final int colNumber, + final String severity) { + + String expected = " + + + + + + + + +*/ + + +// #3) Assert content of module2/target/checkstyle-result.xml +// +// Module 2 should use *only* the configuration defined within the condestyle2 project. +// +assertCheckstyleErrorExists(resultModule2, result2Lines, 24, -1, "warning"); +assertCheckstyleErrorExists(resultModule2, result2Lines, 29, 5, "warning"); +assertCheckstyleErrorExists(resultModule2, result2Lines, 30, -1, "warning"); + +/* +Expected checkstyle-result.xml for module 2: + + + + + + + + + + +*/ diff --git a/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/AbstractCheckstyleReport.java b/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/AbstractCheckstyleReport.java index c37c92a0ea..9ee2963db0 100644 --- a/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/AbstractCheckstyleReport.java +++ b/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/AbstractCheckstyleReport.java @@ -467,8 +467,9 @@ public void executeReport( Locale locale ) try { - CheckstyleExecutorRequest request = createRequest().setLicenseArtifacts( collectArtifacts( "license" ) ) - .setConfigurationArtifacts( collectArtifacts( "configuration" ) ); + CheckstyleExecutorRequest request = createRequest() + .setLicenseArtifacts( collectArtifacts( "license" ) ) + .setConfigurationArtifacts( collectArtifacts( "configuration" ) ); CheckstyleResults results = checkstyleExecutor.executeCheckstyle( request ); @@ -540,7 +541,17 @@ private List getCheckstylePluginDependenciesAsArtifacts( Map. This is what - the structure of that project looks like: + [[1]] Define a separate Maven project containing the checkstyle configuration files saved + below the <<>> directory -+-----+ -whizbang -|-- pom.xml -|-- core -| `-- pom.xml -|-- gui -| `-- pom.xml -|-- jmx -| `-- pom.xml -`-- src -+-----+ + [[2]] Define a checkstyle plugin configuration template within the parent project. Use the configLocation as + given in the configuration project above + + [[3]] In the artifact projects, define a dependency from the checkstyle plugin to the above mentioned project + + [] + + These steps are exemplified within an integration test (called ) in the codebase + of the maven-checkstyle-plugin. Let us walk through the steps in order, and use the code within the integration + test as a point of origin. Feel free to examine the source code itself to clarify. The overall structure of the + small maven project is seen below (ignore the file, which is used only for integration testing): + +[../images/multimodule.png] Multimodule project structure + + The project consists of a parent project, two modules which produce JARs containing artifacts (<<>> + and <<>>), and two projects containing checkstyle configuration (<<>> and <<>>). + The reason for using 2 separate codestyle projects is to illustrate how the maven-checkstyle-plugin handles + multiple configuration projects - but we will start out with the simplest possible configuration and expand later on. + +* 1: Define a separate Checkstyle configuration Maven project + + We will start the process by creating two checkstyle configuration files, namely <<>> and + <<>>. Please refer to the checkstyle project for details of the configuration + settings available within these files. Both these files are stored under the <<>> directory, + as illustrated below: -* Create a subproject to house the resources +[../images/codestyle1.png] Codestyle1 Project with Checkstyle resources - We'll start by adding another sub project that will house our common - configuration. Let's call it . In it we put the resources that - we want to include. In this example, we will add configuration files for the - Checkstyle Plugin. Configuration files for other plugins, like the PMD - plugin, can be included in the same subproject if you like. + Note that the (relative) resource path to the checkstyle.xml file is <<>> and + correspondingly the resource path of the checkstyle-suppressions.xml file + is <<>>. This occurs since all files below <>> are + copied into the resulting JAR artifact. + + <> Store checkstyle resources in a subdirectory whose path is + unique within the reactor. This avoids potential conflicts (but see also {Configuration resource handling}). + +* 2: Define a checkstyle plugin configuration template within the parent project + + It is a good idea to reuse the maven-checkstyle-plugin configuration in multiple projects within the build reactor. + A recommended approach is therefore to configure the checkstyle plugin within a <<>> element in the + pom.xml file in the topmost ("parent") project, and reuse that configuration in the <<>> and <<>> + projects. Note that the <> property is set to the resource path to the checkstyle.xml file + which we defined in the section above, and corresponding for the <> property. +-----+ -whizbang -|-- pom.xml -|-- build-tools -| |-- src -| | `-- main -| | `-- resources -| | `-- whizbang -| | |-- checkstyle.xml -| | `-- LICENSE.TXT -| `-- pom.xml -|-- core -|-- gui -|-- jmx -`-- src + + 4.0.0 + se.west.foobar + foobar-parent + 1.0.0-SNAPSHOT + pom + + + codestyle1 + module1 + module2 + + + + + + + maven-checkstyle-plugin + ${project.version} + + true + /checkstyle/checkstyle.xml + /checkstyle/checkstyle-suppressions.xml + + + + checkstyle_check + + check + + package + + + checkstyle_report + + checkstyle + + site + + + + + + + +-----+ - <> put the resources into a subdirectory that you can ensure will be - unique and not conflict with anyone else. +* 3: Complete the configuration in each artifact project + + Artifact projects require an active checkstyle plugin configuration. + To use the template configuration provided within the parent project's pluginManagement element, include a + plugin definition similar within the pom's build element. + + The <<>> within the <<>> project provides a checkstyle-plugin definition, and adds the + checkstyle configuration by declaring a dependency within the checkstyle plugin. - The <<>> file for should look like this: + <> You have to specify a plugin dependency on <<>> in the + <<<\>>> element of your <<>>. It will not work inside the + <<<\>>> element, because <<<\>>> does not support + plugin dependencies. The rest of the configuration is done in the normal way + in the <<<\>>> element. +-----+ - - 4.0.0 - com.example.whizbang - build-tools - 1.0 - Build Tools + + 4.0.0 + + + se.west.foobar + foobar-parent + 1.0.0-SNAPSHOT + + + se.west.foobar.module1 + foobar-module1 + jar + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + se.west.foobar.codestyle1 + foobar-codestyle1 + 1.0.0-SNAPSHOT + + + + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + +-----+ - -* Configure the other projects to use it - Now we can include the Checkstyle configuration in the top level - <<>>. + The configuration above constitutes a simple but working pom configuration. + However, we should illustrate another aspect about multimodule setup for the maven-checkstyle-plugin, + namely how the checkstyle-maven-plugin treats overridden configurations. + +Multimodule Configuration, part 2: {{Configuration resource handling}}. + + The checkstyle-maven-plugin has a rather simplistic view on configuration resources. + The first <<>> resource which can be loaded by the plugin from its classpath is used, + and all other <<>> files are ignored. This implies that the order of dependencies + (to configuration projects) for plugin is important. + + Returning to the full setup of the integration test, you may note that it sports 2 codestyle projects (which is + unusual but not impossible in the real world). The reason is to illustrate how the maven-checkstyle-plugin + treats multiple checkstyle configuration resources in multi-module projects. + +[../images/codestyles.png] Multimodule project structure - 2 Codestyle projects - <> You have to specify a plugin dependency on <<>> in the - <<<\>>> element of your <<>>. It will not work inside the - <<<\>>> element, because <<<\>>> does not support - plugin dependencies. The rest of the configuration is done in the normal way - in the <<<\>>> element. + Assume that our <<>> project requires the checkstyle configuration found in <<>>, + and that our <<>> project requires the checkstyle configuration found in <<>>. + A stable and usable configuration implies either (but not both) of the two codestyle projects are imported + as a dependency to the maven-checkstyle-plugin. + + However, as the checkstyle-maven-plugin only uses the first codestyle project in its classpath, we can + illustrate the effects on the plugin by the following configuration: +-----+ - - 4.0.0 - com.example.whizbang - whizbang-parent - 1.0 - pom - WhizBang Parent - - - - org.apache.maven.plugins - maven-checkstyle-plugin - ${project.version} - - - com.example.whizbang - build-tools - 1.0 - - - - - - + - - org.apache.maven.plugins - maven-checkstyle-plugin - ${project.version} - - whizbang/checkstyle.xml - whizbang/LICENSE.txt - - + + maven-checkstyle-plugin + + + + se.west.foobar.codestyle1 + foobar-codestyle1 + 1.0.0-SNAPSHOT + + + se.west.foobar.codestyle2 + foobar-codestyle2 + 1.0.0-SNAPSHOT + + + - - - build-tools - core - jmx - gui - - + +-----+ - Once you are done with that, ensure that you do not include - the Maven Checkstyle Plugin in your sub modules, as their definition and - configuration, will override the top level parent pom's definition. - - Based on the Checkstyle Plugin configuration above, the values of - <<>> and <<>> will be resolved from the - classpath. The JAR was included in the classpath when it was - declared as a dependency to the plugin. - - Lastly, kick off a build of the site. + The resource load order can be changed by simply changing the dependency order: +-----+ -mvn site + + + + maven-checkstyle-plugin + + + + se.west.foobar.codestyle2 + foobar-codestyle2 + 1.0.0-SNAPSHOT + + + se.west.foobar.codestyle1 + foobar-codestyle1 + 1.0.0-SNAPSHOT + + + + + +-----+ - Every sub project will now use the same Checkstyle setup and configuration. + <> This is similar to the standard JDK ClassLoader which loads/uses the first + class <<>> it can find in its classpath. Should 2 JARs in the classpath contain a + class with the fully qualified class name <<>> only the first occurrence will be loaded and used. diff --git a/maven-checkstyle-plugin/src/site/resources/images/codestyle1.png b/maven-checkstyle-plugin/src/site/resources/images/codestyle1.png new file mode 100644 index 0000000000..17ed2b5250 Binary files /dev/null and b/maven-checkstyle-plugin/src/site/resources/images/codestyle1.png differ diff --git a/maven-checkstyle-plugin/src/site/resources/images/codestyles.png b/maven-checkstyle-plugin/src/site/resources/images/codestyles.png new file mode 100644 index 0000000000..1daa5c3619 Binary files /dev/null and b/maven-checkstyle-plugin/src/site/resources/images/codestyles.png differ diff --git a/maven-checkstyle-plugin/src/site/resources/images/multimodule.png b/maven-checkstyle-plugin/src/site/resources/images/multimodule.png new file mode 100644 index 0000000000..d24eacea65 Binary files /dev/null and b/maven-checkstyle-plugin/src/site/resources/images/multimodule.png differ