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
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2013 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.api.resources;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.maven.project.MavenProject;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.ModuleLanguages;
import org.sonar.api.component.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* A class that manipulates Projects in the Sonar way.
*
* @since 1.10
*/
public class Project extends Resource implements Component {
/**
* Internal use
*/
public static final Language NONE_LANGUAGE = new AbstractLanguage("none", "None") {
@Override
public String[] getFileSuffixes() {
return null;
}
};
private static final String MAVEN_KEY_FORMAT = "%s:%s";
private static final String BRANCH_KEY_FORMAT = "%s:%s";
public static final String SCOPE = Scopes.PROJECT;
/**
* @deprecated since version 1.11. Constant moved to CoreProperties
*/
@Deprecated
public static final String PARAM_REUSE_RULES_CONFIG = CoreProperties.REUSE_RULES_CONFIGURATION_PROPERTY;
/**
* Enumerates the type of possible analysis
*/
public enum AnalysisType {
STATIC, DYNAMIC, REUSE_REPORTS;
/**
* @param includeReuseReportMode whether to count report reuse as dynamic or not
* @return whether this a dynamic analysis
*/
public boolean isDynamic(boolean includeReuseReportMode) {
return equals(Project.AnalysisType.DYNAMIC) ||
(equals(Project.AnalysisType.REUSE_REPORTS) && includeReuseReportMode);
}
}
private MavenProject pom;
private String branch;
private ProjectFileSystem fileSystem;
private Configuration configuration;
private String name;
private String description;
private String packaging;
private Language language;
private Date analysisDate;
private AnalysisType analysisType;
private String analysisVersion;
// modules tree
private Project parent;
private List<Project> modules = new ArrayList<Project>();
public Project(String key) {
setKey(key);
setDeprecatedKey(key);
setEffectiveKey(key);
}
public Project(String key, String branch, String name) {
if (StringUtils.isNotBlank(branch)) {
setKey(String.format(BRANCH_KEY_FORMAT, key, branch));
this.name = String.format("%s %s", name, branch);
} else {
setKey(key);
this.name = name;
}
setDeprecatedKey(getKey());
setEffectiveKey(getKey());
this.branch = branch;
}
public String getBranch() {
return branch;
}
/**
* For internal use only.
*/
public Project setBranch(String branch) {
this.branch = branch;
return this;
}
/**
* For internal use only.
*/
public final Project setPom(MavenProject pom) {
this.pom = pom;
return this;
}
/**
* @return the project's packaging
* @deprecated in 2.8. See http://jira.codehaus.org/browse/SONAR-2341
*/
@Deprecated
public String getPackaging() {
return packaging;
}
@Override
public String getName() {
return name;
}
@Override
public String getLongName() {
return name;
}
@Override
public String getDescription() {
return description;
}
/**
* For internal use only.
*/
public Project setName(String name) {
this.name = name;
return this;
}
/**
* For internal use only.
*/
public Project setDescription(String description) {
this.description = description;
return this;
}
/**
* For internal use only.
*
* @deprecated in 2.8. See http://jira.codehaus.org/browse/SONAR-2341
*/
@Deprecated
public Project setPackaging(String packaging) {
this.packaging = packaging;
return this;
}
/**
* @return whether the current project is root project
*/
public boolean isRoot() {
return getParent() == null;
}
public Project getRoot() {
return parent == null ? this : parent.getRoot();
}
/**
* @return whether the current project is a module
*/
public boolean isModule() {
return !isRoot();
}
/**
* @return the type of analysis of the project
*/
public AnalysisType getAnalysisType() {
return analysisType;
}
public Project setAnalysisType(AnalysisType at) {
this.analysisType = at;
return this;
}
/**
* whether it's the latest analysis done on this project (displayed in sonar dashboard) or an analysis on a past revision.
*
* @since 2.0
* @deprecated in 3.6. The analysis is now always the latest one (past analysis must be done in a chronological order). See http://jira.codehaus.org/browse/SONAR-4334
*/
@Deprecated
public boolean isLatestAnalysis() {
return true;
}
/**
* For internal use only.
*
* @deprecated in 3.6. It's not possible to analyze a project before the latest known quality snapshot.
* See http://jira.codehaus.org/browse/SONAR-4334
*/
@Deprecated
public Project setLatestAnalysis(boolean b) {
if (!b) {
throw new UnsupportedOperationException("The analysis is always the latest one. " +
"Past analysis must be done in a chronological order.");
}
return this;
}
/**
* @return the project language when there is only one language
* @deprecated since 4.2 use {@link ModuleLanguages}
*/
@Deprecated
@Override
public Language getLanguage() {
return language;
}
/**
* Internal use
*/
public Project setLanguage(Language language) {
this.language = language;
return this;
}
/**
* @return the language key or empty if no language is specified
* @deprecated since 4.2 use {@link ModuleLanguages}
*/
@Deprecated
public String getLanguageKey() {
return configuration.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "");
}
/**
* For internal use only.
*/
public Project setAnalysisDate(Date analysisDate) {
this.analysisDate = analysisDate;
return this;
}
/**
* For internal use only.
*/
public Project setAnalysisVersion(String analysisVersion) {
this.analysisVersion = analysisVersion;
return this;
}
/**
* @return the scope of the current object
*/
@Override
public String getScope() {
return Scopes.PROJECT;
}
/**
* @return the qualifier of the current object
*/
@Override
public String getQualifier() {
return isRoot() ? Qualifiers.PROJECT : Qualifiers.MODULE;
}
@Override
public boolean matchFilePattern(String antPattern) {
return false;
}
@Override
public Project getParent() {
return parent;
}
/**
* For internal use only.
*/
public Project setParent(Project parent) {
this.parent = parent;
if (parent != null) {
parent.modules.add(this);
}
return this;
}
/**
* For internal use only.
*/
public void removeFromParent() {
if (parent != null) {
parent.modules.remove(this);
}
}
/**
* @return the list of modules
*/
public List<Project> getModules() {
return modules;
}
/**
* @return whether to use external source for rules configuration
* @deprecated since 2.5. See discussion from http://jira.codehaus.org/browse/SONAR-1873
*/
@Deprecated
public boolean getReuseExistingRulesConfig() {
return configuration != null && configuration.getBoolean(CoreProperties.REUSE_RULES_CONFIGURATION_PROPERTY, false);
}
/**
* @return the current version of the project
*/
public String getAnalysisVersion() {
return analysisVersion;
}
/**
* @return the analysis date, i.e. the date that will be used to store the snapshot
*/
public Date getAnalysisDate() {
return analysisDate;
}
/**
* Patterns of resource exclusion as defined in project settings page.
*
* @since 3.3 also applies exclusions in general settings page and global exclusions.
* @deprecated replaced by {@link org.sonar.api.scan.filesystem.FileExclusions} in version 3.5
*/
@Deprecated
public String[] getExclusionPatterns() {
return trimExclusions(ImmutableList.<String>builder()
.add(configuration.getStringArray(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY))
.add(configuration.getStringArray(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY)).build());
}
/**
* Patterns of test exclusion as defined in project settings page.
* Also applies exclusions in general settings page and global exclusions.
*
* @since 3.3
* @deprecated replaced by {@link org.sonar.api.scan.filesystem.FileExclusions} in version 3.5
*/
@Deprecated
public String[] getTestExclusionPatterns() {
return trimExclusions(ImmutableList.<String>builder()
.add(configuration.getStringArray(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY))
.add(configuration.getStringArray(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY)).build());
}
// http://jira.codehaus.org/browse/SONAR-2261 - exclusion must be trimmed
private static String[] trimExclusions(List<String> exclusions) {
List<String> trimmed = Lists.newArrayList();
for (String exclusion : exclusions) {
trimmed.add(StringUtils.trim(exclusion));
}
return trimmed.toArray(new String[trimmed.size()]);
}
/**
* Set exclusion patterns. Configuration is not saved, so this method must be used ONLY IN UNIT TESTS.
* @deprecated replaced by {@link org.sonar.api.scan.filesystem.FileExclusions} in version 3.5
*/
@Deprecated
public Project setExclusionPatterns(String[] s) {
throw new UnsupportedOperationException("deprecated in 3.5");
}
/**
* Note: it's better to get a reference on ProjectFileSystem as an IoC dependency (constructor parameter)
* @deprecated replaced by {@link org.sonar.api.scan.filesystem.ModuleFileSystem} in 3.5
*/
@Deprecated
public ProjectFileSystem getFileSystem() {
return fileSystem;
}
/**
* For internal use only.
*
* @deprecated since 2.6. See http://jira.codehaus.org/browse/SONAR-2126
*/
@Deprecated
public Project setFileSystem(ProjectFileSystem fs) {
this.fileSystem = fs;
return this;
}
/**
* @deprecated since 2.5. See http://jira.codehaus.org/browse/SONAR-2011
*/
@Deprecated
public String getGroupId() {
return pom.getGroupId();
}
/**
* @deprecated since 2.5. See http://jira.codehaus.org/browse/SONAR-2011
*/
@Deprecated
public String getArtifactId() {
return pom.getArtifactId();
}
/**
* @return the underlying Maven project
* @deprecated since 2.5. See http://jira.codehaus.org/browse/SONAR-2011 ,
* MavenProject can be retrieved as an IoC dependency
*/
@Deprecated
public MavenProject getPom() {
return pom;
}
/**
* @return the project configuration
* @deprecated since 2.12. The component org.sonar.api.config.Settings must be used.
*/
@Deprecated
public Configuration getConfiguration() {
return configuration;
}
/**
* For internal use only.
*/
public final Project setConfiguration(Configuration configuration) {
this.configuration = configuration;
return this;
}
/**
* @deprecated since 3.6. Replaced by {@link org.sonar.api.config.Settings}.
*/
@Deprecated
public Object getProperty(String key) {
return configuration != null ? configuration.getProperty(key) : null;
}
public static Project createFromMavenIds(String groupId, String artifactId) {
return new Project(String.format(MAVEN_KEY_FORMAT, groupId, artifactId));
}
@Override
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.append("key", getKey())
.append("qualifier", getQualifier())
.toString();
}
@Override
public String key() {
return getKey();
}
@Override
public String name() {
return getName();
}
@Override
public String path() {
return getPath();
}
@Override
public String longName() {
return getLongName();
}
@Override
public String qualifier() {
return getQualifier();
}
}
|