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.maven;
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.plugin.PluginManager;
033    import org.apache.maven.project.MavenProject;
034    import org.apache.maven.project.MavenProjectBuilder;
035    import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
036    import org.sonar.api.batch.bootstrap.ProjectDefinition;
037    import org.sonar.api.batch.bootstrap.ProjectReactor;
038    import org.sonar.batch.Batch;
039    import org.sonar.batch.MavenProjectConverter;
040    import org.sonar.batch.bootstrapper.EnvironmentInformation;
041    import org.sonar.core.config.Logback;
042    
043    /**
044     * @goal sonar
045     * @aggregator
046     * @requiresDependencyResolution test
047     */
048    public 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       * @component
072       * @required
073       */
074      private PluginManager pluginManager;
075    
076      /**
077       * The artifact factory to use.
078       *
079       * @component
080       * @required
081       * @readonly
082       */
083      private ArtifactFactory artifactFactory;
084    
085      /**
086       * The artifact repository to use.
087       *
088       * @parameter expression="${localRepository}"
089       * @required
090       * @readonly
091       */
092      private ArtifactRepository localRepository;
093    
094      /**
095       * The artifact metadata source to use.
096       *
097       * @component
098       * @required
099       * @readonly
100       */
101      private ArtifactMetadataSource artifactMetadataSource;
102    
103      /**
104       * The artifact collector to use.
105       *
106       * @component
107       * @required
108       * @readonly
109       */
110      private ArtifactCollector artifactCollector;
111    
112      /**
113       * The dependency tree builder to use.
114       *
115       * @component
116       * @required
117       * @readonly
118       */
119      private DependencyTreeBuilder dependencyTreeBuilder;
120    
121      /**
122       * @component
123       * @required
124       * @readonly
125       */
126      private MavenProjectBuilder projectBuilder;
127    
128      /**
129       * @component
130       * @required
131       * @readonly
132       */
133      private RuntimeInformation runtimeInformation;
134    
135      /**
136       * @parameter expression="${sonar.verbose}"  default-value="false"
137       */
138      private boolean verbose;
139    
140      public void execute() throws MojoExecutionException, MojoFailureException {
141        configureLogback();
142        executeBatch();
143      }
144    
145      private void executeBatch() throws MojoExecutionException {
146        ProjectDefinition def = MavenProjectConverter.convert(session.getSortedProjects(), project);
147        ProjectReactor reactor = new ProjectReactor(def);
148    
149        Batch batch = new Batch(reactor, session, getLog(), lifecycleExecutor, pluginManager, artifactFactory,
150            localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder,
151            projectBuilder, getEnvironmentInformation(), Maven2PluginExecutor.class);
152        batch.execute();
153      }
154    
155      private EnvironmentInformation getEnvironmentInformation() {
156        String mavenVersion = runtimeInformation.getApplicationVersion().toString();
157        return new EnvironmentInformation("Maven", mavenVersion);
158      }
159    
160      private void configureLogback() {
161        boolean debugMode = (verbose || getLog().isDebugEnabled());
162    
163        // this system property is required by the logback configuration
164        System.setProperty("ROOT_LOGGER_LEVEL", debugMode ? "DEBUG" : "INFO");
165        Logback.configure("/org/sonar/maven/logback.xml");
166      }
167    }