001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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.Batch;
038    import org.sonar.batch.MavenProjectConverter;
039    import org.sonar.batch.bootstrapper.EnvironmentInformation;
040    import org.sonar.core.config.Logback;
041    
042    /**
043     * @goal sonar
044     * @aggregator
045     * @requiresDependencyResolution test
046     */
047    public final class SonarMojo extends AbstractMojo {
048    
049      /**
050       * @parameter expression="${session}"
051       * @required
052       * @readonly
053       */
054      private MavenSession session;
055    
056      /**
057       * @parameter expression="${project}"
058       * @required
059       * @readonly
060       */
061      private MavenProject project;
062    
063      /**
064       * @component
065       * @required
066       */
067      private LifecycleExecutor lifecycleExecutor;
068    
069      /**
070       * The artifact factory to use.
071       *
072       * @component
073       * @required
074       * @readonly
075       */
076      private ArtifactFactory artifactFactory;
077    
078      /**
079       * The artifact repository to use.
080       *
081       * @parameter expression="${localRepository}"
082       * @required
083       * @readonly
084       */
085      private ArtifactRepository localRepository;
086    
087      /**
088       * The artifact metadata source to use.
089       *
090       * @component
091       * @required
092       * @readonly
093       */
094      private ArtifactMetadataSource artifactMetadataSource;
095    
096      /**
097       * The artifact collector to use.
098       *
099       * @component
100       * @required
101       * @readonly
102       */
103      private ArtifactCollector artifactCollector;
104    
105      /**
106       * The dependency tree builder to use.
107       *
108       * @component
109       * @required
110       * @readonly
111       */
112      private DependencyTreeBuilder dependencyTreeBuilder;
113    
114      /**
115       * @component
116       * @required
117       * @readonly
118       */
119      private MavenProjectBuilder projectBuilder;
120    
121      /**
122       * @component
123       * @required
124       * @readonly
125       */
126      private RuntimeInformation runtimeInformation;
127    
128      /**
129       * @parameter expression="${sonar.verbose}"  default-value="false"
130       */
131      private boolean verbose;
132    
133    
134      public void execute() throws MojoExecutionException, MojoFailureException {
135        configureLogback();
136        executeBatch();
137      }
138    
139      private void executeBatch() throws MojoExecutionException {
140        ProjectDefinition def = MavenProjectConverter.convert(session.getSortedProjects(), project);
141        ProjectReactor reactor = new ProjectReactor(def);
142    
143        Batch batch = new Batch(reactor, session, getLog(), lifecycleExecutor, artifactFactory,
144            localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder,
145            projectBuilder, getEnvironmentInformation(), Maven3PluginExecutor.class);
146        batch.execute();
147      }
148    
149      private EnvironmentInformation getEnvironmentInformation() {
150        String mavenVersion = runtimeInformation.getApplicationVersion().toString();
151        return new EnvironmentInformation("Maven", mavenVersion);
152      }
153    
154      private void configureLogback() {
155        boolean debugMode = (verbose || getLog().isDebugEnabled());
156    
157        // this system property is required by the logback configuration
158        System.setProperty("ROOT_LOGGER_LEVEL", debugMode ? "DEBUG" : "INFO");
159        Logback.configure("/org/sonar/maven3/logback.xml");
160      }
161    }