You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by do...@apache.org on 2011/10/29 02:05:54 UTC

svn commit: r1190695 - in /buildr/trunk: CHANGELOG addon/buildr/checkstyle.rake doc/more_stuff.textile

Author: donaldp
Date: Sat Oct 29 00:05:54 2011
New Revision: 1190695

URL: http://svn.apache.org/viewvc?rev=1190695&view=rev
Log:
Add in a checkstyle extension and some basic documentation

Added:
    buildr/trunk/addon/buildr/checkstyle.rake
Modified:
    buildr/trunk/CHANGELOG
    buildr/trunk/doc/more_stuff.textile

Modified: buildr/trunk/CHANGELOG
URL: http://svn.apache.org/viewvc/buildr/trunk/CHANGELOG?rev=1190695&r1=1190694&r2=1190695&view=diff
==============================================================================
--- buildr/trunk/CHANGELOG (original)
+++ buildr/trunk/CHANGELOG Sat Oct 29 00:05:54 2011
@@ -1,4 +1,5 @@
 1.4.7 (Pending)
+* Added:  Add a Checkstyle extension.
 * Change: Parameterize the the directory where the top level cobertura tasks will generate
           reports. Specify using Buildr::Cobertura.report_dir = '....'
 * Fixed:  BUILDR-611 Buildr should not unnecessarily recompile Java files

Added: buildr/trunk/addon/buildr/checkstyle.rake
URL: http://svn.apache.org/viewvc/buildr/trunk/addon/buildr/checkstyle.rake?rev=1190695&view=auto
==============================================================================
--- buildr/trunk/addon/buildr/checkstyle.rake (added)
+++ buildr/trunk/addon/buildr/checkstyle.rake Sat Oct 29 00:05:54 2011
@@ -0,0 +1,201 @@
+# 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.
+
+module Buildr
+  # Provides the <code>checkstyle:html</code> and <code>checkstyle:xml</code> tasks.
+  # Require explicitly using <code>require "buildr/checkstyle"</code>.
+  module Checkstyle
+
+    class << self
+
+      # The specs for requirements
+      def dependencies
+        [
+          'com.puppycrawl.tools:checkstyle:jar:5.4',
+          'commons-cli:commons-cli:jar:1.2',
+          'antlr:antlr:jar:2.7.7',
+          'com.google.collections:google-collections:jar:1.0',
+          'commons-beanutils:commons-beanutils-core:jar:1.8.3',
+          'commons-logging:commons-logging:jar:1.1.1'
+        ]
+      end
+
+      def checkstyle(configuration_file, format, output_file, source_paths, options = {})
+        dependencies = (options[:dependencies] || []) + self.dependencies
+        cp = Buildr.artifacts(dependencies).each(&:invoke).map(&:to_s)
+
+        args = []
+        if options[:properties_file]
+          args << "-p"
+          args << options[:properties_file]
+        end
+        args << "-c"
+        args << configuration_file
+        args << "-f"
+        args << format
+        args << "-o"
+        args << output_file
+        source_paths.each do |source_path|
+          args << "-r"
+          args << source_path
+        end
+
+        begin
+          Java::Commands.java 'com.puppycrawl.tools.checkstyle.Main', *(args + [{:classpath => cp, :properties => options[:properties], :java_args => options[:java_args]}])
+        rescue => e
+          raise e if options[:fail_on_error]
+        end
+      end
+    end
+
+    class Config
+      def enabled?
+        File.exist?(self.configuration_file)
+      end
+
+      def html_enabled?
+        File.exist?(self.style_file)
+      end
+
+      attr_writer :config_directory
+
+      def config_directory
+        @config_directory || project._(:source, :main, :etc, :checkstyle)
+      end
+
+      attr_writer :report_dir
+
+      def report_dir
+        @report_dir || project._(:reports, :checkstyle)
+      end
+
+      attr_writer :configuration_file
+
+      def configuration_file
+        @configuration_file || "#{self.config_directory}/checks.xml"
+      end
+
+      attr_writer :fail_on_error
+
+      def fail_on_error?
+        @fail_on_error.nil? ? false : @fail_on_error
+      end
+
+      attr_writer :format
+
+      def format
+        @format || 'xml'
+      end
+
+      attr_writer :xml_output_file
+
+      def xml_output_file
+        @xml_output_file || "#{self.report_dir}/checkstyle.xml"
+      end
+
+      attr_writer :html_output_file
+
+      def html_output_file
+        @html_output_file || "#{self.report_dir}/checkstyle.html"
+      end
+
+      attr_writer :style_file
+
+      def style_file
+        @style_file || "#{self.config_directory}/checkstyle-report.xsl"
+      end
+
+      attr_writer :suppressions_file
+
+      def suppressions_file
+        @suppressions_file || "#{self.config_directory}/suppressions.xml"
+      end
+
+      attr_writer :import_control_file
+
+      def import_control_file
+        @import_control_file || "#{self.config_directory}/import-control.xml"
+      end
+
+      def properties
+        properties = {:basedir => self.project.base_dir}
+        properties['checkstyle.suppressions.file'] = self.suppressions_file if File.exist?(self.suppressions_file)
+        properties['checkstyle.import-control.file'] = self.import_control_file if File.exist?(self.import_control_file)
+        properties
+      end
+
+      def source_paths
+        @source_paths ||= [self.project.compile.sources, self.project.test.compile.sources]
+      end
+
+      def extra_dependencies
+        @extra_dependencies ||= [self.project.compile.dependencies, self.project.test.compile.dependencies]
+      end
+
+      protected
+
+      def initialize(project)
+        @project = project
+      end
+
+      attr_reader :project
+
+    end
+
+    module ProjectExtension
+      include Extension
+
+      def checkstyle
+        @checkstyle ||= Buildr::Checkstyle::Config.new(project)
+      end
+
+      after_define do |project|
+        if project.checkstyle.enabled?
+          desc "Generate checkstyle xml report."
+          project.task("checkstyle:xml") do
+            puts "Checkstyle: Analyzing source code..."
+            mkdir_p File.dirname(project.checkstyle.xml_output_file)
+            Buildr::Checkstyle.checkstyle(project.checkstyle.configuration_file,
+                                          project.checkstyle.format,
+                                          project.checkstyle.xml_output_file,
+                                          project.checkstyle.source_paths.flatten.compact,
+                                          :properties => project.checkstyle.properties,
+                                          :fail_on_error => project.checkstyle.fail_on_error?,
+                                          :dependencies => project.checkstyle.extra_dependencies)
+          end
+
+          if project.checkstyle.html_enabled?
+            xml_task = project.task("checkstyle:xml")
+            desc "Generate checkstyle html report."
+            project.task("checkstyle:html" => xml_task) do
+              puts "Checkstyle: Generating report"
+              mkdir_p File.dirname(project.checkstyle.html_output_file)
+              Buildr.ant "checkstyle" do |ant|
+                ant.xslt :in => project.checkstyle.xml_output_file,
+                         :out => project.checkstyle.html_output_file,
+                         :style => project.checkstyle.style_file
+              end
+            end
+
+          end
+        end
+      end
+    end
+  end
+end
+
+class Buildr::Project
+  include Buildr::Checkstyle::ProjectExtension
+end

Modified: buildr/trunk/doc/more_stuff.textile
URL: http://svn.apache.org/viewvc/buildr/trunk/doc/more_stuff.textile?rev=1190695&r1=1190694&r2=1190695&view=diff
==============================================================================
--- buildr/trunk/doc/more_stuff.textile (original)
+++ buildr/trunk/doc/more_stuff.textile Sat Oct 29 00:05:54 2011
@@ -808,6 +808,31 @@ $ buildr --require buildr/jdepend jdepen
 $ buildr -rbuildr/java/cobertura cobertura:html
 {% endhighlight %}
 
+h2(#checkstyle). Checkstyle
+
+Checkstyle is integrated into Buildr through an extension. The extension adds the "checkstyle:xml" task that generates an xml report listing checkstyle violations in xml format and may add a "checkstyle:html" task if an appropriate xsl is preset. A typical project that uses the extension may look something like;
+
+{% highlight ruby %}
+require 'buildr/checkstyle'
+
+define "foo" do
+  project.version = "1.0.0"
+
+  define "bar" do ... end
+
+  checkstyle.config_directory = _('etc/checkstyle')
+  checkstyle.source_paths << project('bar')._(:source, :main, :java)
+  checkstyle.extra_dependencies << :javax_servlet
+
+end
+{% endhighlight %}
+
+By default checkstyle will look for all configuration files in the src/main/etc/checkstyle directory but this can be overriden by the setting the "checkstyle.config_directory" parameter. The "checkstyle:xml" task will be defined if the checkstyle rules file is found. The rules file is typically named "checks.xml" but can be overridden by setting the "checkstyle.configuration_file" parameter. If a suppressions file or import control file is included in the directory, these will also be used by the extension. These names of these files will default to "suppressions.xml" and "import-control.xml" but these can be overriden by the parameters "checkstyle.suppressions_file" and "checkstyle.import_control_file".
+
+The extension will include the source and test directories of the project aswell as the compile and test dependencies when invoking the checkstyle tool. These can be added to by the parameters "checkstyle.source_paths" and "checkstyle.extra_dependencies" as appropriate.
+
+If the xsl file named "checkstyle-report.xsl" is present in the configuration directory then a "checkstyle:html" task will be defined. The name of the xsl file can be overridden by the parameter "checkstyle.style_file".
+
 h2(#jaxb_xjc). JAXB Xjc Compiler
 
 Buildr includes an extension that provides the ability to invoke jaxb xjc binding compiler. A typical project that uses the extension may look something like;