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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/*
* Sonar Runner
* Copyright (C) 2011 SonarSource
* dev@sonar.codehaus.org
*
* This program 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.
*
* This program 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 02
*/
package org.sonar.runner.internal.batch;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.FileFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.runner.RunnerException;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
/**
* Class that creates a Sonar project definition based on a set of properties.
*
* @since 1.5
*/
public final class SonarProjectBuilder {
private static final Logger LOG = LoggerFactory.getLogger(SonarProjectBuilder.class);
private static final String PROPERTY_PROJECT_BASEDIR = "sonar.projectBaseDir";
private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
private static final String PROPERTY_PROJECT_KEY = "sonar.projectKey";
private static final String PROPERTY_PROJECT_NAME = "sonar.projectName";
private static final String PROPERTY_PROJECT_DESCRIPTION = "sonar.projectDescription";
private static final String PROPERTY_PROJECT_VERSION = "sonar.projectVersion";
private static final String PROPERTY_MODULES = "sonar.modules";
/**
* New properties, to be consistent with Sonar naming conventions
* @since 1.5
*/
private static final String PROPERTY_SOURCES = "sonar.sources";
private static final String PROPERTY_TESTS = "sonar.tests";
private static final String PROPERTY_BINARIES = "sonar.binaries";
private static final String PROPERTY_LIBRARIES = "sonar.libraries";
/**
* Old deprecated properties, replaced by the same ones preceded by "sonar."
*/
private static final String PROPERTY_OLD_SOURCES = "sources";
private static final String PROPERTY_OLD_TESTS = "tests";
private static final String PROPERTY_OLD_BINARIES = "binaries";
private static final String PROPERTY_OLD_LIBRARIES = "libraries";
private static final Map<String, String> DEPRECATED_PROPS_TO_NEW_PROPS = new HashMap<String, String>() {
{
put(PROPERTY_OLD_SOURCES, PROPERTY_SOURCES);
put(PROPERTY_OLD_TESTS, PROPERTY_TESTS);
put(PROPERTY_OLD_BINARIES, PROPERTY_BINARIES);
put(PROPERTY_OLD_LIBRARIES, PROPERTY_LIBRARIES);
}
};
/**
* @since 1.4
*/
private static final String PROPERTY_WORK_DIRECTORY = "sonar.working.directory";
private static final String DEF_VALUE_WORK_DIRECTORY = ".sonar";
/**
* Array of all mandatory properties required for a project without child.
*/
private static final String[] MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT = {PROPERTY_PROJECT_BASEDIR, PROPERTY_PROJECT_KEY, PROPERTY_PROJECT_NAME, PROPERTY_PROJECT_VERSION,
PROPERTY_SOURCES};
/**
* Array of all mandatory properties required for a project with children.
*/
private static final String[] MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT = {PROPERTY_PROJECT_BASEDIR, PROPERTY_PROJECT_KEY, PROPERTY_PROJECT_NAME, PROPERTY_PROJECT_VERSION};
/**
* Array of all mandatory properties required for a child project before its properties get merged with its parent ones.
*/
private static final String[] MANDATORY_PROPERTIES_FOR_CHILD = {PROPERTY_PROJECT_KEY, PROPERTY_PROJECT_NAME};
/**
* Properties that must not be passed from the parent project to its children.
*/
private static final List<String> NON_HERITED_PROPERTIES_FOR_CHILD = Lists.newArrayList(PROPERTY_PROJECT_BASEDIR, PROPERTY_MODULES, PROPERTY_PROJECT_DESCRIPTION);
private String command;
private Properties properties;
private File rootProjectWorkDir;
private SonarProjectBuilder(String command, Properties properties) {
this.command = command;
this.properties = properties;
}
public static SonarProjectBuilder create(Properties properties) {
return create(null, properties);
}
public static SonarProjectBuilder create(String command, Properties properties) {
return new SonarProjectBuilder(command, properties);
}
public ProjectDefinition generateProjectDefinition() {
if (StringUtils.isBlank(command) || "inspect".equals(command)) {
ProjectDefinition rootProject = defineProject(properties, null);
rootProjectWorkDir = rootProject.getWorkDir();
defineChildren(rootProject);
cleanAndCheckProjectDefinitions(rootProject);
return rootProject;
}
else if (properties.containsKey(PROPERTY_PROJECT_KEY)) {
return defineTaskContext();
}
else {
return null;
}
}
private ProjectDefinition defineTaskContext() {
File baseDir = new File(System.getProperty("user.home"));
File workDir = initRootProjectWorkDir(baseDir);
ProjectDefinition definition = ProjectDefinition.create().setProperties(properties)
.setBaseDir(baseDir)
.setWorkDir(workDir);
return definition;
}
private ProjectDefinition defineProject(Properties properties, ProjectDefinition parent) {
if (properties.containsKey(PROPERTY_MODULES)) {
checkMandatoryProperties(properties, MANDATORY_PROPERTIES_FOR_MULTIMODULE_PROJECT);
}
else {
checkMandatoryProperties(properties, MANDATORY_PROPERTIES_FOR_SIMPLE_PROJECT);
}
File baseDir = new File(properties.getProperty(PROPERTY_PROJECT_BASEDIR));
File workDir = null;
if (parent == null) {
workDir = initRootProjectWorkDir(baseDir);
} else {
workDir = initModuleWorkDir(properties);
}
ProjectDefinition definition = ProjectDefinition.create().setProperties(properties)
.setBaseDir(baseDir)
.setWorkDir(workDir);
return definition;
}
@VisibleForTesting
protected File initRootProjectWorkDir(File baseDir) {
String workDir = properties.getProperty(PROPERTY_WORK_DIRECTORY);
if (StringUtils.isBlank(workDir)) {
return new File(baseDir, DEF_VALUE_WORK_DIRECTORY);
}
File customWorkDir = new File(workDir);
if (customWorkDir.isAbsolute()) {
return customWorkDir;
}
return new File(baseDir, customWorkDir.getPath());
}
@VisibleForTesting
protected File initModuleWorkDir(Properties properties) {
String cleanKey = StringUtils.deleteWhitespace(properties.getProperty(PROPERTY_PROJECT_KEY));
cleanKey = StringUtils.replace(cleanKey, ":", "_");
return new File(rootProjectWorkDir, cleanKey);
}
private void defineChildren(ProjectDefinition parentProject) {
Properties parentProps = parentProject.getProperties();
if (parentProps.containsKey(PROPERTY_MODULES)) {
for (String module : SonarRunnerUtils.getListFromProperty(parentProps, PROPERTY_MODULES)) {
Properties moduleProps = extractModuleProperties(module, parentProps);
ProjectDefinition childProject = loadChildProject(parentProject, moduleProps, module);
// check the uniqueness of the child key
checkUniquenessOfChildKey(childProject, parentProject);
// the child project may have children as well
defineChildren(childProject);
// and finally add this child project to its parent
parentProject.addSubProject(childProject);
}
}
}
private ProjectDefinition loadChildProject(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
setProjectKeyAndNameIfNotDefined(moduleProps, moduleId);
final File baseDir;
if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
baseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), parentProject.getBaseDir());
setProjectBaseDir(baseDir, moduleProps, moduleId);
try {
if (!parentProject.getBaseDir().getCanonicalFile().equals(baseDir.getCanonicalFile())) {
tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
}
} catch (IOException e) {
throw new RunnerException("Error when resolving baseDir", e);
}
} else if (moduleProps.containsKey(PROPERTY_PROJECT_CONFIG_FILE)) {
baseDir = loadPropsFile(parentProject, moduleProps, moduleId);
} else {
baseDir = new File(parentProject.getBaseDir(), moduleId);
setProjectBaseDir(baseDir, moduleProps, moduleId);
tryToFindAndLoadPropsFile(baseDir, moduleProps, moduleId);
}
// and finish
checkMandatoryProperties(moduleProps, MANDATORY_PROPERTIES_FOR_CHILD);
if (!moduleProps.containsKey(PROPERTY_MODULES)) {
// SONARPLUGINS-2285 Not an aggreator project so we can validate that paths are correct if defined
// We need to resolve patterns that may have been used in "sonar.libraries"
for (String pattern : SonarRunnerUtils.getListFromProperty(moduleProps, PROPERTY_LIBRARIES)) {
File[] files = getLibraries(baseDir, pattern);
if (files == null || files.length == 0) {
LOG.error("Invalid value of " + PROPERTY_LIBRARIES + " for module " + moduleId);
throw new RunnerException("No files matching pattern \"" + pattern + "\" in directory \"" + baseDir + "\"");
}
}
// Check sonar.tests
String[] testDirs = SonarRunnerUtils.getListFromProperty(moduleProps, PROPERTY_TESTS);
checkExistenceOfDirectories(moduleId, baseDir, testDirs, PROPERTY_TESTS);
// Check sonar.binaries
String[] binDirs = SonarRunnerUtils.getListFromProperty(moduleProps, PROPERTY_BINARIES);
checkExistenceOfDirectories(moduleId, baseDir, binDirs, PROPERTY_BINARIES);
}
mergeParentProperties(moduleProps, parentProject.getProperties());
prefixProjectKeyWithParentKey(moduleProps, parentProject.getKey());
return defineProject(moduleProps, parentProject);
}
/**
* @return baseDir
*/
protected File loadPropsFile(ProjectDefinition parentProject, Properties moduleProps, String moduleId) {
File propertyFile = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_CONFIG_FILE), parentProject.getBaseDir());
if (propertyFile.isFile()) {
Properties propsFromFile = toProperties(propertyFile);
for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
moduleProps.put(entry.getKey(), entry.getValue());
}
File baseDir = null;
if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
baseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParentFile());
} else {
baseDir = propertyFile.getParentFile();
}
setProjectBaseDir(baseDir, moduleProps, moduleId);
return baseDir;
} else {
throw new RunnerException("The properties file of the module '" + moduleId + "' does not exist: " + propertyFile.getAbsolutePath());
}
}
private void tryToFindAndLoadPropsFile(File baseDir, Properties moduleProps, String moduleId) {
File propertyFile = new File(baseDir, "sonar-project.properties");
if (propertyFile.isFile()) {
Properties propsFromFile = toProperties(propertyFile);
for (Entry<Object, Object> entry : propsFromFile.entrySet()) {
moduleProps.put(entry.getKey(), entry.getValue());
}
if (moduleProps.containsKey(PROPERTY_PROJECT_BASEDIR)) {
File overwrittenBaseDir = getFileFromPath(moduleProps.getProperty(PROPERTY_PROJECT_BASEDIR), propertyFile.getParentFile());
setProjectBaseDir(overwrittenBaseDir, moduleProps, moduleId);
}
}
}
@VisibleForTesting
protected static Properties toProperties(File propertyFile) {
Properties propsFromFile = new Properties();
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(propertyFile);
propsFromFile.load(fileInputStream);
} catch (IOException e) {
throw new RunnerException("Impossible to read the property file: " + propertyFile.getAbsolutePath(), e);
} finally {
IOUtils.closeQuietly(fileInputStream);
}
return propsFromFile;
}
@VisibleForTesting
protected static void setProjectKeyAndNameIfNotDefined(Properties childProps, String moduleId) {
if (!childProps.containsKey(PROPERTY_PROJECT_KEY)) {
childProps.put(PROPERTY_PROJECT_KEY, moduleId);
}
if (!childProps.containsKey(PROPERTY_PROJECT_NAME)) {
childProps.put(PROPERTY_PROJECT_NAME, moduleId);
}
}
@VisibleForTesting
protected static void checkUniquenessOfChildKey(ProjectDefinition childProject, ProjectDefinition parentProject) {
for (ProjectDefinition definition : parentProject.getSubProjects()) {
if (definition.getKey().equals(childProject.getKey())) {
throw new RunnerException("Project '" + parentProject.getKey() + "' can't have 2 modules with the following key: " + childProject.getKey());
}
}
}
@VisibleForTesting
protected static void prefixProjectKeyWithParentKey(Properties childProps, String parentKey) {
String childKey = childProps.getProperty(PROPERTY_PROJECT_KEY);
childProps.put(PROPERTY_PROJECT_KEY, parentKey + ":" + childKey);
}
private static void setProjectBaseDir(File baseDir, Properties childProps, String moduleId) {
if (!baseDir.isDirectory()) {
throw new RunnerException("The base directory of the module '" + moduleId + "' does not exist: " + baseDir.getAbsolutePath());
}
childProps.put(PROPERTY_PROJECT_BASEDIR, baseDir.getAbsolutePath());
}
@VisibleForTesting
protected static void checkMandatoryProperties(Properties props, String[] mandatoryProps) {
replaceDeprecatedProperties(props);
StringBuilder missing = new StringBuilder();
for (String mandatoryProperty : mandatoryProps) {
if (!props.containsKey(mandatoryProperty)) {
if (missing.length() > 0) {
missing.append(", ");
}
missing.append(mandatoryProperty);
}
}
if (missing.length() != 0) {
String projectKey = props.getProperty(PROPERTY_PROJECT_KEY);
throw new RunnerException("You must define the following mandatory properties for '" + (projectKey == null ? "Unknown" : projectKey) + "': " + missing);
}
}
@VisibleForTesting
protected static void cleanAndCheckProjectDefinitions(ProjectDefinition project) {
if (project.getSubProjects().isEmpty()) {
cleanAndCheckModuleProperties(project);
} else {
cleanAndCheckAggregatorProjectProperties(project);
// clean modules properties as well
for (ProjectDefinition module : project.getSubProjects()) {
cleanAndCheckProjectDefinitions(module);
}
}
}
@VisibleForTesting
protected static void cleanAndCheckModuleProperties(ProjectDefinition project) {
Properties properties = project.getProperties();
// We need to check the existence of source directories
String[] sourceDirs = SonarRunnerUtils.getListFromProperty(properties, PROPERTY_SOURCES);
checkExistenceOfDirectories(project.getKey(), project.getBaseDir(), sourceDirs, PROPERTY_SOURCES);
// And we need to resolve patterns that may have been used in "sonar.libraries"
List<String> libPaths = Lists.newArrayList();
for (String pattern : SonarRunnerUtils.getListFromProperty(properties, PROPERTY_LIBRARIES)) {
for (File file : getLibraries(project.getBaseDir(), pattern)) {
libPaths.add(file.getAbsolutePath());
}
}
properties.remove(PROPERTY_LIBRARIES);
properties.put(PROPERTY_LIBRARIES, StringUtils.join(libPaths, ","));
}
@VisibleForTesting
protected static void cleanAndCheckAggregatorProjectProperties(ProjectDefinition project) {
Properties properties = project.getProperties();
// SONARPLUGINS-2295
String[] sourceDirs = SonarRunnerUtils.getListFromProperty(properties, PROPERTY_SOURCES);
for (String path : sourceDirs) {
File sourceFolder = getFileFromPath(path, project.getBaseDir());
if (sourceFolder.isDirectory()) {
LOG.warn("/!\\ A multi-module project can't have source folders, so '{}' won't be used for the analysis. " +
"If you want to analyse files of this folder, you should create another sub-module and move them inside it.",
sourceFolder.toString());
}
}
// "aggregator" project must not have the following properties:
properties.remove(PROPERTY_SOURCES);
properties.remove(PROPERTY_TESTS);
properties.remove(PROPERTY_BINARIES);
properties.remove(PROPERTY_LIBRARIES);
// and they don't need properties related to their modules either
Properties clone = (Properties) properties.clone();
List<String> moduleIds = Lists.newArrayList(SonarRunnerUtils.getListFromProperty(properties, PROPERTY_MODULES));
for (Entry<Object, Object> entry : clone.entrySet()) {
String key = (String) entry.getKey();
if (isKeyPrefixedByModuleId(key, moduleIds)) {
properties.remove(key);
}
}
}
/**
* Replaces the deprecated properties by the new ones, and logs a message to warn the users.
*/
@VisibleForTesting
protected static void replaceDeprecatedProperties(Properties props) {
for (Entry<String, String> entry : DEPRECATED_PROPS_TO_NEW_PROPS.entrySet()) {
String key = entry.getKey();
if (props.containsKey(key)) {
String newKey = entry.getValue();
LOG.warn("/!\\ The '{}' property is deprecated and is replaced by '{}'. Don't forget to update your files.", key, newKey);
String value = props.getProperty(key);
props.remove(key);
props.put(newKey, value);
}
}
}
@VisibleForTesting
protected static void mergeParentProperties(Properties childProps, Properties parentProps) {
List<String> moduleIds = Lists.newArrayList(SonarRunnerUtils.getListFromProperty(parentProps, PROPERTY_MODULES));
for (Map.Entry<Object, Object> entry : parentProps.entrySet()) {
String key = (String) entry.getKey();
if (!childProps.containsKey(key)
&& !NON_HERITED_PROPERTIES_FOR_CHILD.contains(key)
&& !isKeyPrefixedByModuleId(key, moduleIds)) {
childProps.put(entry.getKey(), entry.getValue());
}
}
}
private static boolean isKeyPrefixedByModuleId(String key, List<String> moduleIds) {
for (String moduleId : moduleIds) {
if (key.startsWith(moduleId + ".")) {
return true;
}
}
return false;
}
@VisibleForTesting
protected static Properties extractModuleProperties(String module, Properties properties) {
Properties moduleProps = new Properties();
String propertyPrefix = module + ".";
int prefixLength = propertyPrefix.length();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String key = (String) entry.getKey();
if (key.startsWith(propertyPrefix)) {
moduleProps.put(key.substring(prefixLength), entry.getValue());
}
}
return moduleProps;
}
@VisibleForTesting
protected static void checkExistenceOfDirectories(String moduleRef, File baseDir, String[] sourceDirs, String propName) {
for (String path : sourceDirs) {
File sourceFolder = getFileFromPath(path, baseDir);
if (!sourceFolder.isDirectory()) {
LOG.error("Invalid value of " + propName + " for " + moduleRef);
throw new RunnerException("The folder '" + path + "' does not exist for '" + moduleRef +
"' (base directory = " + baseDir.getAbsolutePath() + ")");
}
}
}
/**
* Returns files matching specified pattern.
*/
@VisibleForTesting
protected static File[] getLibraries(File baseDir, String pattern) {
final int i = Math.max(pattern.lastIndexOf('/'), pattern.lastIndexOf('\\'));
final String dirPath, filePattern;
if (i == -1) {
dirPath = ".";
filePattern = pattern;
} else {
dirPath = pattern.substring(0, i);
filePattern = pattern.substring(i + 1);
}
FileFilter fileFilter = new AndFileFilter(FileFileFilter.FILE, new WildcardFileFilter(filePattern));
File dir = resolvePath(baseDir, dirPath);
File[] files = dir.listFiles(fileFilter);
if (files == null) {
files = new File[0];
}
return files;
}
private static File resolvePath(File baseDir, String path) {
File file = new File(path);
if (!file.isAbsolute()) {
try {
file = new File(baseDir, path).getCanonicalFile();
} catch (IOException e) {
throw new RunnerException("Unable to resolve path \"" + path + "\"", e);
}
}
return file;
}
/**
* Returns the file denoted by the given path, may this path be relative to "baseDir" or absolute.
*/
@VisibleForTesting
protected static File getFileFromPath(String path, File baseDir) {
File propertyFile = new File(path.trim());
if (!propertyFile.isAbsolute()) {
propertyFile = new File(baseDir, propertyFile.getPath());
}
return propertyFile;
}
}
|