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;
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() {
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;
+ }
}
}
}
- writeXmlReport(project, report);
+ writeXmlReport(report);
return report;
}
}
- 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) {
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));
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);
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));
}