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.batch.phases;
021
022import org.apache.commons.io.IOUtils;
023import org.apache.maven.project.MavenProject;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026import org.sonar.api.BatchComponent;
027import org.sonar.api.batch.BatchExtensionDictionnary;
028import org.sonar.api.batch.maven.MavenPlugin;
029import org.sonar.api.batch.maven.MavenPluginHandler;
030import org.sonar.api.resources.Project;
031import org.sonar.api.utils.SonarException;
032
033import java.io.File;
034import java.io.FileWriter;
035import java.io.IOException;
036
037public class MavenPluginsConfigurator implements BatchComponent {
038
039  private BatchExtensionDictionnary dictionnary = null;
040
041  public MavenPluginsConfigurator(BatchExtensionDictionnary dictionnary) {
042    this.dictionnary = dictionnary;
043  }
044
045  public void execute(Project project) {
046    Logger logger = LoggerFactory.getLogger(getClass());
047    logger.info("Configure maven plugins...");
048
049    for (MavenPluginHandler handler : dictionnary.selectMavenPluginHandlers(project)) {
050      logger.debug("Configure {}...", handler);
051      configureHandler(project, handler);
052    }
053    savePom(project);
054  }
055
056  protected void configureHandler(Project project, MavenPluginHandler handler) {
057    MavenPlugin plugin = MavenPlugin.registerPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId(), handler.getVersion(), handler.isFixedVersion());
058    handler.configure(project, plugin);
059  }
060
061  protected void savePom(Project project) {
062    MavenProject pom = project.getPom();
063    if (pom != null) {
064      File targetPom = new File(project.getFileSystem().getSonarWorkingDirectory(), "sonar-pom.xml");
065      FileWriter fileWriter = null;
066      try {
067        fileWriter = new FileWriter(targetPom, false);
068        pom.writeModel(fileWriter);
069
070      } catch (IOException e) {
071        throw new SonarException("Can not save pom to " + targetPom, e);
072      } finally {
073        IOUtils.closeQuietly(fileWriter);
074      }
075    }
076  }
077}