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     */
020    package org.sonar.maven3;
021    
022    import org.apache.maven.artifact.factory.ArtifactFactory;
023    import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
024    import org.apache.maven.artifact.repository.ArtifactRepository;
025    import org.apache.maven.artifact.resolver.ArtifactCollector;
026    import org.apache.maven.execution.MavenSession;
027    import org.apache.maven.execution.RuntimeInformation;
028    import org.apache.maven.lifecycle.LifecycleExecutor;
029    import org.apache.maven.plugin.AbstractMojo;
030    import org.apache.maven.plugin.MojoExecutionException;
031    import org.apache.maven.plugin.MojoFailureException;
032    import org.apache.maven.project.MavenProject;
033    import org.apache.maven.project.MavenProjectBuilder;
034    import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
035    import org.sonar.api.batch.bootstrap.ProjectDefinition;
036    import org.sonar.api.batch.bootstrap.ProjectReactor;
037    import org.sonar.batch.MavenProjectConverter;
038    import org.sonar.batch.bootstrapper.Batch;
039    import org.sonar.batch.bootstrapper.EnvironmentInformation;
040    import org.sonar.batch.bootstrapper.LoggingConfiguration;
041    
042    import java.util.Map;
043    
044    /**
045     * @goal sonar
046     * @aggregator
047     * @requiresDependencyResolution test
048     */
049    public final class SonarMojo extends AbstractMojo {
050    
051      /**
052       * @parameter expression="${session}"
053       * @required
054       * @readonly
055       */
056      private MavenSession session;
057    
058      /**
059       * @parameter expression="${project}"
060       * @required
061       * @readonly
062       */
063      private MavenProject project;
064    
065      /**
066       * @component
067       * @required
068       */
069      private LifecycleExecutor lifecycleExecutor;
070    
071      /**
072       * The artifact factory to use.
073       *
074       * @component
075       * @required
076       * @readonly
077       */
078      private ArtifactFactory artifactFactory;
079    
080      /**
081       * The artifact repository to use.
082       *
083       * @parameter expression="${localRepository}"
084       * @required
085       * @readonly
086       */
087      private ArtifactRepository localRepository;
088    
089      /**
090       * The artifact metadata source to use.
091       *
092       * @component
093       * @required
094       * @readonly
095       */
096      private ArtifactMetadataSource artifactMetadataSource;
097    
098      /**
099       * The artifact collector to use.
100       *
101       * @component
102       * @required
103       * @readonly
104       */
105      private ArtifactCollector artifactCollector;
106    
107      /**
108       * The dependency tree builder to use.
109       *
110       * @component
111       * @required
112       * @readonly
113       */
114      private DependencyTreeBuilder dependencyTreeBuilder;
115    
116      /**
117       * @component
118       * @required
119       * @readonly
120       */
121      private MavenProjectBuilder projectBuilder;
122    
123      /**
124       * @component
125       * @required
126       * @readonly
127       */
128      private RuntimeInformation runtimeInformation;
129    
130      public void execute() throws MojoExecutionException, MojoFailureException {
131        ProjectDefinition def = MavenProjectConverter.convert(session.getSortedProjects(), project);
132        ProjectReactor reactor = new ProjectReactor(def);
133    
134        Batch batch = Batch.builder()
135          .setEnvironment(getEnvironmentInformation())
136          .setProjectReactor(reactor)
137          .addComponents(
138            session, getLog(), lifecycleExecutor, artifactFactory, localRepository, artifactMetadataSource, artifactCollector,
139            dependencyTreeBuilder, projectBuilder, Maven3PluginExecutor.class)
140          .build();
141    
142        configureLogging(batch.getLoggingConfiguration());
143        batch.execute();
144      }
145    
146      private void configureLogging(LoggingConfiguration logging) {
147        logging.setProperties((Map) session.getSystemProperties());
148        logging.setFormat(LoggingConfiguration.FORMAT_MAVEN);
149        if (getLog().isDebugEnabled()) {
150          logging.setVerbose(true);
151        }
152      }
153    
154      private EnvironmentInformation getEnvironmentInformation() {
155        String mavenVersion = runtimeInformation.getApplicationVersion().toString();
156        return new EnvironmentInformation("Maven", mavenVersion);
157      }
158    
159    }