]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3346 Add property "sonar.pmd.generateXml"
authorEvgeny Mandrikov <mandrikov@gmail.com>
Mon, 26 Mar 2012 18:54:42 +0000 (00:54 +0600)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Mon, 26 Mar 2012 18:54:48 +0000 (00:54 +0600)
plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConfiguration.java
plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConfigurationTest.java
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java

index d0e0228ecf358fa89f52e8700ee806b04b1f4b7a..6ec082908b189fa0fa0fc4387fca1d604a6c903a 100644 (file)
@@ -20,6 +20,8 @@
 package org.sonar.plugins.pmd;
 
 import org.sonar.api.BatchExtension;
+import org.sonar.api.Property;
+import org.sonar.api.config.Settings;
 import org.sonar.api.profiles.RulesProfile;
 import org.sonar.api.resources.Project;
 
@@ -29,16 +31,29 @@ import java.io.StringWriter;
 import java.util.Arrays;
 import java.util.List;
 
+@org.sonar.api.Properties({
+  @Property(
+    key = PmdConfiguration.PROPERTY_GENERATE_XML,
+    defaultValue = "false",
+    name = "Generate XML Report",
+    project = false,
+    global = false
+  )
+})
 public class PmdConfiguration implements BatchExtension {
 
+  public static final String PROPERTY_GENERATE_XML = "sonar.pmd.generateXml";
+
   private PmdProfileExporter pmdProfileExporter;
   private RulesProfile rulesProfile;
   private Project project;
+  private Settings settings;
 
-  public PmdConfiguration(PmdProfileExporter pmdRulesRepository, RulesProfile rulesProfile, Project project) {
+  public PmdConfiguration(PmdProfileExporter pmdRulesRepository, RulesProfile rulesProfile, Project project, Settings settings) {
     this.pmdProfileExporter = pmdRulesRepository;
     this.rulesProfile = rulesProfile;
     this.project = project;
+    this.settings = settings;
   }
 
   public List<String> getRulesets() {
@@ -55,4 +70,11 @@ public class PmdConfiguration implements BatchExtension {
       throw new RuntimeException("Fail to save the PMD configuration", e);
     }
   }
+
+  public File getTargetXMLReport() {
+    if (settings.getBoolean(PROPERTY_GENERATE_XML)) {
+      return new File(project.getFileSystem().getSonarWorkingDirectory(), "pmd-result.xml");
+    }
+    return null;
+  }
 }
index 9b5eb9862bdc403b7f82d1413f831937ac57685c..f0a5a3a00bd1f25a87dd95bbb5b34dab74159491 100644 (file)
@@ -80,7 +80,7 @@ public class PmdExecutor implements BatchExtension {
         }
       }
 
-      writeXmlReport(project, report);
+      writeXmlReport(report);
 
       return report;
 
@@ -129,18 +129,19 @@ public class PmdExecutor implements BatchExtension {
     }
   }
 
-  private File writeXmlReport(Project project, Report report) throws IOException {
-    Renderer xmlRenderer = new XMLRenderer();
-    Writer stringwriter = new StringWriter();
-    xmlRenderer.setWriter(stringwriter);
-    xmlRenderer.start();
-    xmlRenderer.renderFileReport(report);
-    xmlRenderer.end();
-
-    File xmlReport = new File(project.getFileSystem().getSonarWorkingDirectory(), "pmd-result.xml");
-    LOG.info("PMD output report: " + xmlReport.getAbsolutePath());
-    FileUtils.write(xmlReport, stringwriter.toString());
-    return xmlReport;
+  private void writeXmlReport(Report report) throws IOException {
+    File xmlReport = configuration.getTargetXMLReport();
+    if (xmlReport != null) {
+      Renderer xmlRenderer = new XMLRenderer();
+      Writer stringwriter = new StringWriter();
+      xmlRenderer.setWriter(stringwriter);
+      xmlRenderer.start();
+      xmlRenderer.renderFileReport(report);
+      xmlRenderer.end();
+
+      LOG.info("PMD output report: " + xmlReport.getAbsolutePath());
+      FileUtils.write(xmlReport, stringwriter.toString());
+    }
   }
 
   static String getNormalizedJavaVersion(String javaVersion) {
index 2ce2827bbacfd4dc00b397c36ce090489b0c5372..3475f2db96ebec5c30b0b23e4f8594e0dec92888 100644 (file)
@@ -38,7 +38,7 @@ public class PmdConfigurationTest {
   public void writeConfigurationToWorkingDir() throws IOException {
     Project project = MavenTestUtils.loadProjectFromPom(getClass(), "writeConfigurationToWorkingDir/pom.xml");
 
-    PmdConfiguration configuration = new PmdConfiguration(new PmdProfileExporter(), RulesProfile.create(), project);
+    PmdConfiguration configuration = new PmdConfiguration(new PmdProfileExporter(), RulesProfile.create(), project, null);
     List<String> rulesets = configuration.getRulesets();
 
     assertThat(rulesets.size(), is(1));
index 4ed43df39511e4bd0b85c917da5c19202bff862b..dd9feab2cd9aafc2024924cff3c9198c5f610548 100644 (file)
@@ -62,10 +62,11 @@ public class PmdExecutorTest {
     PmdConfiguration conf = mock(PmdConfiguration.class);
     File file = FileUtils.toFile(getClass().getResource("/org/sonar/plugins/pmd/PmdExecutorTest/executeOnManySourceDirs/pmd.xml").toURI().toURL());
     when(conf.getRulesets()).thenReturn(Arrays.asList(file.getAbsolutePath()));
+    File xmlReport = new File(workDir, "pmd-result.xml");
+    when(conf.getTargetXMLReport()).thenReturn(xmlReport);
 
     PmdExecutor executor = new PmdExecutor(project, conf);
     executor.execute();
-    File xmlReport = new File(workDir, "pmd-result.xml");
     assertThat(xmlReport.exists(), is(true));
 
     String xml = FileUtils.readFileToString(xmlReport);
@@ -88,10 +89,11 @@ public class PmdExecutorTest {
 
     PmdConfiguration conf = mock(PmdConfiguration.class);
     when(conf.getRulesets()).thenReturn(Arrays.asList(new File("test-resources/ignorePmdFailures/pmd.xml").getAbsolutePath()));
-
+    File xmlReport = new File(workDir, "pmd-result.xml");
+    when(conf.getTargetXMLReport()).thenReturn(xmlReport);
     PmdExecutor executor = new PmdExecutor(project, conf);
+
     executor.execute();
-    File xmlReport = new File(workDir, "pmd-result.xml");
     assertThat(xmlReport.exists(), is(true));
   }