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