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