All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.hortis.sonar.mvn.reports.CheckstyleHandler Maven / Gradle / Ivy

/*
 * This program is copyright (c) 2007 Hortis-GRC SA.
 * 
 * This file is part of Sonar.
 * Sonar is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * Sonar is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License 
 * along with Sonar; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package ch.hortis.sonar.mvn.reports;

import ch.hortis.sonar.mvn.PomUtils;
import ch.hortis.sonar.mvn.PrepareMojo;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

public class CheckstyleHandler extends ReportHandler {

  public void configurePlugin(MavenProject pom, Plugin plugin) throws MojoExecutionException {
    unsetConfigParameter( plugin, "outputDirectory" );
    unsetConfigParameter( plugin, "outputFile" );
    setConfigParameter( plugin, "outputFileFormat", "xml" );
    setConfigParameter( plugin, "consoleOutput", "false" );
    setConfigParameter( plugin, "enableRSS", "false" );
    setConfigParameter( plugin, "failsOnError", "false" );
    setConfigParameter( plugin, "linkXRef", "false" );
    
    String ckeckStylePath = pom.getBuild().getDirectory() + "/sonar/checkstyle.xml";
    setConfigParameter( plugin, "configLocation", ckeckStylePath );

    copyPluginDependencies(pom, plugin);
  }

  public boolean execute( PrepareMojo mojo, Log log ) throws MojoExecutionException {
    String ckeckStylePath = mojo.getExecutedProject().getBuild().getDirectory() + "/sonar/checkstyle.xml";
    File checkstyle = new File( ckeckStylePath );
    try {
      mojo.getWebInterfaceService().getRemoteCheckstyleConfiguration(checkstyle);
    } catch ( IOException e ) {
      throw new MojoExecutionException( "Error during sonar checkstyle config file download", e );
    }
    return true;
  }

  public List getGoals() {
    return Arrays.asList( new String[]{"checkstyle"} );
  }

  public boolean mustFailOnError() {
    return true;
  }

  private void copyPluginDependencies(MavenProject pom, Plugin plugin) {
    HashSet depKeys = new HashSet();
    Collection checkstylePlugins = PomUtils.findPluginDefinitions(pom, getReport().getGroupId(), getReport().getArtifactId());
    for (Plugin ckPlugin : checkstylePlugins) {
      if ( ckPlugin.getDependencies()!=null ) {
        if ( plugin.getDependencies()==null ) {
          plugin.setDependencies( new ArrayList() );
        }
        for (Dependency dependency : (Collection)ckPlugin.getDependencies()) {
          String depArtifactId = dependency.getArtifactId();
          if ( ! depKeys.contains( depArtifactId ) ) {
            depKeys.add( depArtifactId );
            plugin.getDependencies().add( dependency );
          }
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy