001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.plugins.cobertura;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.sonar.api.CoreProperties;
024    import org.sonar.api.batch.maven.MavenPlugin;
025    import org.sonar.api.batch.maven.MavenPluginHandler;
026    import org.sonar.api.batch.maven.MavenSurefireUtils;
027    import org.sonar.api.config.Settings;
028    import org.sonar.api.resources.Project;
029    import org.sonar.plugins.cobertura.api.CoberturaUtils;
030    
031    public class CoberturaMavenPluginHandler implements MavenPluginHandler {
032    
033      private Settings settings;
034    
035      public CoberturaMavenPluginHandler(Settings settings) {
036        this.settings = settings;
037      }
038    
039      public String getGroupId() {
040        return CoberturaUtils.COBERTURA_GROUP_ID;
041      }
042    
043      public String getArtifactId() {
044        return CoberturaUtils.COBERTURA_ARTIFACT_ID;
045      }
046    
047      public String getVersion() {
048        return "2.5.1";
049      }
050    
051      public boolean isFixedVersion() {
052        return false;
053      }
054    
055      public String[] getGoals() {
056        return new String[] { "cobertura" };
057      }
058    
059      public void configure(Project project, MavenPlugin coberturaPlugin) {
060        configureCobertura(project, coberturaPlugin);
061        MavenSurefireUtils.configure(project);
062      }
063    
064      private void configureCobertura(Project project, MavenPlugin coberturaPlugin) {
065        coberturaPlugin.setParameter("formats/format", "xml");
066        for (String pattern : project.getExclusionPatterns()) {
067          if (pattern.endsWith(".java")) {
068            pattern = StringUtils.substringBeforeLast(pattern, ".") + ".class";
069    
070          } else if (StringUtils.substringAfterLast(pattern, "/").indexOf(".") < 0) {
071            pattern = pattern + ".class";
072          }
073          coberturaPlugin.addParameter("instrumentation/excludes/exclude", pattern);
074        }
075        String maxmem = "";
076        // http://jira.codehaus.org/browse/SONAR-2897: there used to be a typo in the parameter name (was "sonar.cobertura.maxmen")
077        if (settings.hasKey("sonar.cobertura.maxmen")) {
078          maxmem = settings.getString("sonar.cobertura.maxmen");
079        } else {
080          // use the "normal" key
081          maxmem = settings.getString(CoreProperties.COBERTURA_MAXMEM_PROPERTY);
082        }
083        coberturaPlugin.setParameter("maxmem", maxmem);
084      }
085    }