1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.maven;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactCollector;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.RuntimeInformation;
import org.apache.maven.lifecycle.LifecycleExecutor;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
import org.sonar.runner.api.EmbeddedRunner;
import org.sonar.runner.api.RunnerProperties;
import org.sonar.runner.api.ScanProperties;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
/**
* @goal sonar
* @aggregator
* @requiresDependencyResolution test
*/
public final class SonarMojo extends AbstractMojo {
/**
* @parameter property="session"
* @required
* @readonly
*/
private MavenSession session;
/**
* @parameter property="project"
* @required
* @readonly
*/
private MavenProject project;
/**
* @component
* @required
*/
private LifecycleExecutor lifecycleExecutor;
/**
* The artifact factory to use.
*
* @component
* @required
* @readonly
*/
private ArtifactFactory artifactFactory;
/**
* The artifact repository to use.
*
* @parameter property="localRepository"
* @required
* @readonly
*/
private ArtifactRepository localRepository;
/**
* The artifact metadata source to use.
*
* @component
* @required
* @readonly
*/
private ArtifactMetadataSource artifactMetadataSource;
/**
* The artifact collector to use.
*
* @component
* @required
* @readonly
*/
private ArtifactCollector artifactCollector;
/**
* The dependency tree builder to use.
*
* @component
* @required
* @readonly
*/
private DependencyTreeBuilder dependencyTreeBuilder;
/**
* @component
* @required
* @readonly
*/
private MavenProjectBuilder projectBuilder;
/**
* @component
* @required
* @readonly
* @VisibleForTesting
*/
RuntimeInformation runtimeInformation;
/**
* Plexus component for the SecDispatcher
* @component role="org.sonatype.plexus.components.sec.dispatcher.SecDispatcher"
* @required
*/
private SecDispatcher securityDispatcher;
@Override
public void execute() throws MojoExecutionException {
ArtifactVersion mavenVersion = getMavenVersion();
if (mavenVersion.getMajorVersion() == 2 && mavenVersion.getMinorVersion() < 2) {
ExceptionHandling.handle("Please use at least Maven 2.2.x to perform SonarQube analysis (current version is " + mavenVersion.toString() + ")", getLog());
}
try {
EmbeddedRunner runner = EmbeddedRunner.create()
.setApp("Maven", mavenVersion.toString())
.addProperties(session.getExecutionProperties())
.addProperties(project.getModel().getProperties())
// Add user properties (ie command line arguments -Dsonar.xxx=yyyy) in last position to override all other
.addProperties(session.getUserProperties());
String encoding = getSourceEncoding(project);
if (encoding != null) {
runner.setProperty(ScanProperties.PROJECT_SOURCE_ENCODING, encoding);
}
runner
.setProperty(ScanProperties.PROJECT_KEY, getSonarKey(project))
.setProperty(RunnerProperties.WORK_DIR, getSonarWorkDir(project).getAbsolutePath())
.setProperty(ScanProperties.PROJECT_BASEDIR, project.getBasedir().getAbsolutePath())
.setProperty(ScanProperties.PROJECT_VERSION, toString(project.getVersion()))
.setProperty(ScanProperties.PROJECT_NAME, toString(project.getName()))
.setProperty(ScanProperties.PROJECT_DESCRIPTION, toString(project.getDescription()))
.setProperty(ScanProperties.PROJECT_SOURCE_DIRS, ".");
// Exclude log implementation to not conflict with Maven 3.1 logging impl
runner.mask("org.slf4j.LoggerFactory")
// Include slf4j Logger that is exposed by some Sonar components
.unmask("org.slf4j.Logger")
.unmask("org.slf4j.ILoggerFactory")
// Exclude other slf4j classes
// .unmask("org.slf4j.impl.")
.mask("org.slf4j.")
// Exclude logback
.mask("ch.qos.logback.")
.mask("org.sonar.")
// Include everything else
.unmask("");
runner.addExtensions(session, getLog(), lifecycleExecutor, artifactFactory, localRepository, artifactMetadataSource, artifactCollector,
dependencyTreeBuilder, projectBuilder);
if (getLog().isDebugEnabled()) {
runner.setProperty("sonar.verbose", "true");
}
// Replace all properties by decrypted ones if applicable
runner.addProperties(decryptProperties(runner.properties()));
runner.execute();
} catch (Exception e) {
throw ExceptionHandling.handle(e, getLog());
}
}
private ArtifactVersion getMavenVersion() {
return runtimeInformation.getApplicationVersion();
}
public static String toString(Object obj) {
return obj == null ? "" : obj.toString();
}
public static String getSourceEncoding(MavenProject pom) {
return pom.getProperties().getProperty("project.build.sourceEncoding");
}
public static String getSonarKey(MavenProject pom) {
return new StringBuilder().append(pom.getGroupId()).append(":").append(pom.getArtifactId()).toString();
}
public static File getSonarWorkDir(MavenProject pom) {
return new File(getBuildDir(pom), "sonar");
}
private static File getBuildDir(MavenProject pom) {
return resolvePath(pom.getBuild().getDirectory(), pom.getBasedir());
}
static File resolvePath(String path, File basedir) {
if (path != null) {
File file = new File(path);
if (!file.isAbsolute()) {
try {
file = new File(basedir, path).getCanonicalFile();
} catch (IOException e) {
throw new IllegalStateException("Unable to resolve path '" + path + "'", e);
}
}
return file;
}
return null;
}
public Properties decryptProperties(Properties properties) {
Properties newProperties = new Properties();
try {
for (String key : properties.stringPropertyNames()) {
if (key.contains(".password")) {
decrypt(properties, newProperties, key);
}
}
} catch (Exception e) {
getLog().warn("Unable to decrypt properties", e);
}
return newProperties;
}
private void decrypt(Properties properties, Properties newProperties, String key) {
try {
String decrypted = securityDispatcher.decrypt(properties.getProperty(key));
newProperties.setProperty(key, decrypted);
} catch (SecDispatcherException e) {
getLog().warn("Unable to decrypt property " + key, e);
}
}
}
|