Author: struberg
Date: Thu Feb 27 17:57:09 2014
New Revision: 1572667

URL: http://svn.apache.org/r1572667
Log:
MCHECKSTYLE-211 add feature to enable 'inline configuration'

Enable a way to configure the checkstyle rules directly in the pom.
This is an easy way to get rid of needing an own parent-pom structure
for not introducing a build cycle because of the otherwise needed
separate checkstyle-rules project.
Txs to ASF committer rmannibucau for the initial baseline patch.
I reworked it to not need any CDATA section.  

Added:
    maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/
    
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/invoker.properties
   (with props)
    maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/
    
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/pom.xml   
(with props)
    maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/
    
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/main/
    
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/main/java/
    
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/main/java/example/
    
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/main/java/example/module/
    
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/main/java/example/module/Example.java
   (with props)
    maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/pom.xml   
(with props)
    maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/verify.groovy
      - copied, changed from r1572483, 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/checkstyle-report/verify.groovy
Modified:
    
maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java

Added: 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/invoker.properties
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/invoker.properties?rev=1572667&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/invoker.properties
 (added)
+++ 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/invoker.properties
 Thu Feb 27 17:57:09 2014
@@ -0,0 +1,20 @@
+# 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 checkstyle:checkstyle checkstyle:check
+# we forced 1 failure
+invoker.buildResult=failure

Propchange: 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/invoker.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/pom.xml?rev=1572667&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/pom.xml 
(added)
+++ 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/pom.xml 
Thu Feb 27 17:57:09 2014
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<project
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";
+    xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <artifactId>example</artifactId>
+    <groupId>com.mycompany</groupId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+  <artifactId>module</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <name>Example Module</name>
+  <packaging>jar</packaging>
+  <url>http://maven.apache.org</url>
+</project>

Propchange: 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/main/java/example/module/Example.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/main/java/example/module/Example.java?rev=1572667&view=auto
==============================================================================
--- 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/main/java/example/module/Example.java
 (added)
+++ 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/main/java/example/module/Example.java
 Thu Feb 27 17:57:09 2014
@@ -0,0 +1,30 @@
+package example.module;
+
+/*
+ * 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.
+ */
+
+public class Example {
+
+    public void test() {
+               // empty block
+               if (System.getProperty("true") != null)
+               {
+               }
+    }
+}

Propchange: 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/module/src/main/java/example/module/Example.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/pom.xml?rev=1572667&view=auto
==============================================================================
--- maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/pom.xml 
(added)
+++ maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/pom.xml Thu 
Feb 27 17:57:09 2014
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>com.mycompany</groupId>
+  <artifactId>example</artifactId>
+  <packaging>pom</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>Example</name>
+  <url>http://maven.apache.org</url>
+  <modules>
+    <module>module</module>
+  </modules>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-checkstyle-plugin</artifactId>
+        <version>2.12-SNAPSHOT</version>
+        <executions>
+          <execution>
+            <id>check</id>
+            <phase>process-resources</phase>
+            <goals>
+              <goal>check</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <logViolationsToConsole>true</logViolationsToConsole>
+          <checkstyleRules>
+            <module name="Checker">
+              <module name="TreeWalker">
+                <module name="EmptyBlock"/>
+              </module>
+              <module name="FileTabCharacter">
+                <property name="eachLine" value="true" />
+              </module>
+            </module>
+          </checkstyleRules>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Propchange: 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/verify.groovy 
(from r1572483, 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/checkstyle-report/verify.groovy)
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/verify.groovy?p2=maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/verify.groovy&p1=maven/plugins/trunk/maven-checkstyle-plugin/src/it/checkstyle-report/verify.groovy&r1=1572483&r2=1572667&rev=1572667&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/checkstyle-report/verify.groovy
 (original)
+++ 
maven/plugins/trunk/maven-checkstyle-plugin/src/it/inlinerules/verify.groovy 
Thu Feb 27 17:57:09 2014
@@ -1,4 +1,3 @@
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -17,25 +16,9 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-assert new File(basedir, 'target/site/checkstyle.html').exists();
-
-assert new File(basedir, 'target/checkstyle-cachefile').exists();
 assert new File(basedir, 'target/checkstyle-checker.xml').exists();
 assert new File(basedir, 'target/checkstyle-header.txt').exists();
 assert new File(basedir, 'target/checkstyle-result.xml').exists();
 
-File rssFile = new File( basedir, 'target/site/checkstyle.rss' );
-assert rssFile.exists();
-
-def rss = new XmlParser().parse( rssFile );
-
-def channel = rss.channel[0]
-
-assert channel.title.text() == 'check-pass - Checkstyle report'
-
-def item = channel.item[0]
-assert item != null
-assert item.title.text().startsWith('File: 1,')
-
 
-return true;
\ No newline at end of file
+return true;

Modified: 
maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java?rev=1572667&r1=1572666&r2=1572667&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-checkstyle-plugin/src/main/java/org/apache/maven/plugin/checkstyle/CheckstyleViolationCheckMojo.java
 Thu Feb 27 17:57:09 2014
@@ -33,6 +33,8 @@ import org.apache.maven.plugins.annotati
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
 import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
+import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.ReaderFactory;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.pull.MXParser;
@@ -64,6 +66,10 @@ public class CheckstyleViolationCheckMoj
 
     private static final String JAVA_FILES = "**\\/*.java";
 
+    private static final String CHECKSTYLE_FILE_HEADER = "<?xml 
version=\"1.0\"?>\n" +
+            "<!DOCTYPE module PUBLIC \"-//Puppy Crawl//DTD Check Configuration 
1.2//EN\"\n" +
+            "        
\"http://www.puppycrawl.com/dtds/configuration_1_2.dtd\";>\n";
+
     /**
      * Specifies the path and filename to save the Checkstyle output. The 
format
      * of the output file is determined by the <code>outputFileFormat</code>
@@ -379,6 +385,15 @@ public class CheckstyleViolationCheckMoj
     @Parameter( property = "checkstyle.includeTestResources", defaultValue = 
"true", required = true )
     private boolean includeTestResources = true;
 
+    @Parameter
+    private PlexusConfiguration checkstyleRules;
+
+    /**
+     * dump file for inlined checkstyle rules 
+     */
+    @Parameter( property = "checkstyle.output.rules.file", defaultValue = 
"${project.build.directory}/checkstyle-rules.xml" )
+    private File rulesFiles;
+
     private ByteArrayOutputStream stringOutputStream;
 
     /** {@inheritDoc} */
@@ -392,6 +407,30 @@ public class CheckstyleViolationCheckMoj
             if ( !skipExec )
             {
 
+                if (checkstyleRules != null)
+                {
+                    if (!"config/sun_checks.xml".equals(configLocation))
+                    {
+                        throw new MojoExecutionException("If you use inline 
configuration for rules don't specify a configLocation");
+                    }
+                    if (checkstyleRules.getChildCount() > 1)
+                    {
+                        throw new MojoExecutionException("Currently only one 
root module is supported");
+                    }
+                    PlexusConfiguration checkerModule = 
checkstyleRules.getChild(0);
+
+                    try
+                    {
+                        FileUtils.forceMkdir(rulesFiles.getParentFile());
+                        FileUtils.fileWrite(rulesFiles, CHECKSTYLE_FILE_HEADER 
+ checkerModule.toString());
+                    }
+                    catch (final IOException e)
+                    {
+                        throw new MojoExecutionException(e.getMessage(), e);
+                    }
+                    configLocation = rulesFiles.getAbsolutePath();
+                }
+
                 ClassLoader currentClassLoader = 
Thread.currentThread().getContextClassLoader();
 
                 try
@@ -620,4 +659,4 @@ public class CheckstyleViolationCheckMoj
         return listener;
     }
 
-}
\ No newline at end of file
+}


Reply via email to