2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.cobertura;
22 import org.apache.commons.configuration.Configuration;
23 import org.sonar.api.CoreProperties;
24 import org.sonar.api.batch.CoverageExtension;
25 import org.sonar.api.batch.Initializer;
26 import org.sonar.api.batch.maven.DependsUponMavenPlugin;
27 import org.sonar.api.batch.maven.MavenPlugin;
28 import org.sonar.api.batch.maven.MavenPluginHandler;
29 import org.sonar.api.resources.Project;
30 import org.sonar.plugins.cobertura.api.CoberturaUtils;
33 * Provides {@link CoberturaMavenPluginHandler} and configures correct path to report.
34 * Enabled only in Maven environment.
36 public class CoberturaMavenInitializer extends Initializer implements CoverageExtension, DependsUponMavenPlugin {
38 private CoberturaMavenPluginHandler handler;
40 public CoberturaMavenInitializer(CoberturaMavenPluginHandler handler) {
41 this.handler = handler;
44 public MavenPluginHandler getMavenPluginHandler(Project project) {
45 if (project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC)) {
52 public boolean shouldExecuteOnProject(Project project) {
53 return project.getAnalysisType().isDynamic(true) &&
54 project.getFileSystem().hasJavaSourceFiles();
58 public void execute(Project project) {
59 Configuration conf = project.getConfiguration();
60 if (conf.containsKey(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY)) {
61 String report = getReportPathFromPluginConfiguration(project);
63 report = getDefaultReportPath(project);
65 conf.setProperty(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY, report);
69 private static String getDefaultReportPath(Project project) {
70 return project.getFileSystem().getReportOutputDir() + "/cobertura/coverage.xml";
73 private static String getReportPathFromPluginConfiguration(Project project) {
74 MavenPlugin mavenPlugin = MavenPlugin.getPlugin(
76 CoberturaUtils.COBERTURA_GROUP_ID,
77 CoberturaUtils.COBERTURA_ARTIFACT_ID);
78 if (mavenPlugin != null) {
79 String path = mavenPlugin.getParameter("outputDirectory");
81 return path + "/coverage.xml";