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