001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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.batch;
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.batch.BatchExtensionDictionnary;
027    import org.sonar.api.batch.SensorContext;
028    import org.sonar.api.batch.maven.DependsUponCustomRules;
029    import org.sonar.api.batch.maven.MavenPlugin;
030    import org.sonar.api.batch.maven.MavenPluginHandler;
031    import org.sonar.api.resources.Project;
032    import org.sonar.api.utils.ServerHttpClient;
033    import org.sonar.api.utils.SonarException;
034    
035    import java.io.File;
036    import java.io.FileWriter;
037    import java.io.IOException;
038    
039    public class MavenPluginsConfigurator implements CoreJob {
040    
041      private BatchExtensionDictionnary dictionnary = null;
042      private ServerHttpClient server;
043    
044      public MavenPluginsConfigurator(BatchExtensionDictionnary dictionnary, ServerHttpClient server) {
045        this.dictionnary = dictionnary;
046        this.server = server;
047      }
048    
049      public void execute(Project project, SensorContext context) {
050        Logger logger = LoggerFactory.getLogger(getClass());
051        logger.info("Configure maven plugins...");
052    
053        for (MavenPluginHandler handler : dictionnary.selectMavenPluginHandlers(project)) {
054          logger.debug("Configure {}...", handler);
055          configureHandler(project, handler);
056        }
057        savePom(project);
058      }
059    
060      protected void configureHandler(Project project, MavenPluginHandler handler) {
061        MavenPlugin plugin = MavenPlugin.registerPlugin(project.getPom(), handler.getGroupId(), handler.getArtifactId(), handler.getVersion(), handler.isFixedVersion());
062        if (handler instanceof DependsUponCustomRules) {
063          plugin.addDependency("org.codehaus.sonar.runtime.rules-extensions", "parent", server.getId(), "pom");
064        }
065        handler.configure(project, plugin);
066      }
067    
068      protected void savePom(Project project) {
069        MavenProject pom = project.getPom();
070        File targetPom = new File(project.getFileSystem().getSonarWorkingDirectory(), "sonar-pom.xml");
071        FileWriter fileWriter = null;
072        try {
073          fileWriter = new FileWriter(targetPom, false);
074          pom.writeModel(fileWriter);
075    
076        } catch (IOException e) {
077          throw new SonarException("Can not save pom to " + targetPom, e);
078        } finally {
079          IOUtils.closeQuietly(fileWriter);
080        }
081      }
082    }