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 */
020package org.sonar.plugins.jacoco;
021
022import java.io.File;
023
024import org.apache.commons.lang.StringUtils;
025import org.sonar.api.batch.maven.MavenPlugin;
026import org.sonar.api.batch.maven.MavenPluginHandler;
027import org.sonar.api.batch.maven.MavenSurefireUtils;
028import org.sonar.api.resources.Project;
029import org.sonar.api.utils.SonarException;
030
031/**
032 * @author Evgeny Mandrikov
033 */
034public class JaCoCoMavenPluginHandler implements MavenPluginHandler {
035
036  private static final String ARG_LINE_PARAMETER = "argLine";
037  private static final String TEST_FAILURE_IGNORE_PARAMETER = "testFailureIgnore";
038
039  private final String groupId;
040  private final String artifactId;
041  private final String version;
042
043  private JacocoConfiguration configuration;
044
045  public JaCoCoMavenPluginHandler(JacocoConfiguration configuration) {
046    this.configuration = configuration;
047    groupId = MavenSurefireUtils.GROUP_ID;
048    artifactId = MavenSurefireUtils.ARTIFACT_ID;
049    version = MavenSurefireUtils.VERSION;
050  }
051
052  public String getGroupId() {
053    return groupId;
054  }
055
056  public String getArtifactId() {
057    return artifactId;
058  }
059
060  public String getVersion() {
061    return version;
062  }
063
064  public boolean isFixedVersion() {
065    return false;
066  }
067
068  public String[] getGoals() {
069    return new String[] { "test" };
070  }
071
072  public void configure(Project project, MavenPlugin plugin) {
073    // See SONARPLUGINS-600
074    String destfilePath = configuration.getReportPath();
075    File destfile = project.getFileSystem().resolvePath(destfilePath);
076    if (destfile.exists() && destfile.isFile()) {
077      JaCoCoUtils.LOG.info("Deleting {}", destfile);
078      if (!destfile.delete()) {
079        throw new SonarException("Unable to delete " + destfile);
080      }
081    }
082
083    String argument = configuration.getJvmArgument();
084
085    String argLine = plugin.getParameter(ARG_LINE_PARAMETER);
086    argLine = StringUtils.isBlank(argLine) ? argument : argument + " " + argLine;
087    JaCoCoUtils.LOG.info("JVM options: {}", argLine);
088    plugin.setParameter(ARG_LINE_PARAMETER, argLine);
089
090    plugin.setParameter(TEST_FAILURE_IGNORE_PARAMETER, "true");
091  }
092
093}