001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.scan.maven;
021    
022    import org.apache.commons.io.IOUtils;
023    import org.apache.maven.project.MavenProject;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    import org.sonar.api.BatchComponent;
027    import org.sonar.api.batch.BatchExtensionDictionnary;
028    import org.sonar.api.batch.maven.MavenPlugin;
029    import org.sonar.api.batch.maven.MavenPluginHandler;
030    import org.sonar.api.resources.Project;
031    
032    import java.io.File;
033    import java.io.FileWriter;
034    import java.io.IOException;
035    
036    public class MavenPluginsConfigurator implements BatchComponent {
037    
038      private BatchExtensionDictionnary dictionnary = null;
039    
040      public MavenPluginsConfigurator(BatchExtensionDictionnary dictionnary) {
041        this.dictionnary = dictionnary;
042      }
043    
044      public void execute(Project project) {
045        Logger logger = LoggerFactory.getLogger(getClass());
046        logger.info("Configure Maven plugins");
047    
048        for (MavenPluginHandler handler : dictionnary.selectMavenPluginHandlers(project)) {
049          logger.debug("Configure {}...", handler);
050          configureHandler(project, handler);
051        }
052        savePom(project);
053      }
054    
055      protected void configureHandler(Project project, MavenPluginHandler handler) {
056        MavenPlugin plugin = MavenPlugin.registerPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId(), handler.getVersion(), handler.isFixedVersion());
057        handler.configure(project, plugin);
058      }
059    
060      protected void savePom(Project project) {
061        MavenProject pom = project.getPom();
062        if (pom != null) {
063          File targetPom = new File(project.getFileSystem().getSonarWorkingDirectory(), "sonar-pom.xml");
064          FileWriter fileWriter = null;
065          try {
066            fileWriter = new FileWriter(targetPom, false);
067            pom.writeModel(fileWriter);
068    
069          } catch (IOException e) {
070            throw new IllegalStateException("Can not save pom to " + targetPom, e);
071          } finally {
072            IOUtils.closeQuietly(fileWriter);
073          }
074        }
075      }
076    }