You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Project.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.resources;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.apache.commons.lang.builder.ToStringBuilder;
  23. import org.apache.maven.project.MavenProject;
  24. import org.sonar.api.CoreProperties;
  25. import org.sonar.api.batch.fs.FileSystem;
  26. import org.sonar.api.component.Component;
  27. import org.sonar.api.config.Settings;
  28. import javax.annotation.CheckForNull;
  29. import javax.annotation.Nullable;
  30. import java.util.ArrayList;
  31. import java.util.Date;
  32. import java.util.List;
  33. /**
  34. * A class that manipulates Projects in the Sonar way.
  35. *
  36. * @since 1.10
  37. */
  38. public class Project extends Resource implements Component {
  39. /**
  40. * Internal use
  41. */
  42. public static final Language NONE_LANGUAGE = new AbstractLanguage("none", "None") {
  43. @Override
  44. public String[] getFileSuffixes() {
  45. return new String[0];
  46. }
  47. };
  48. static final String MAVEN_KEY_FORMAT = "%s:%s";
  49. private static final String BRANCH_KEY_FORMAT = "%s:%s";
  50. public static final String SCOPE = Scopes.PROJECT;
  51. /**
  52. * Enumerates the type of possible analysis
  53. * @deprecated since 4.4 Since 4.3 SQ will no more run tests. So basically it's always reuse report.
  54. */
  55. @Deprecated
  56. public enum AnalysisType {
  57. STATIC, DYNAMIC, REUSE_REPORTS;
  58. /**
  59. * @param includeReuseReportMode whether to count report reuse as dynamic or not
  60. * @return whether this a dynamic analysis
  61. */
  62. public boolean isDynamic(boolean includeReuseReportMode) {
  63. return equals(Project.AnalysisType.DYNAMIC) ||
  64. (equals(Project.AnalysisType.REUSE_REPORTS) && includeReuseReportMode);
  65. }
  66. }
  67. private MavenProject pom;
  68. private String branch;
  69. private ProjectFileSystem fileSystem;
  70. private String name;
  71. private String description;
  72. private String packaging;
  73. private Language language;
  74. private Date analysisDate;
  75. private AnalysisType analysisType;
  76. private String analysisVersion;
  77. private Settings settings;
  78. // For internal use
  79. private java.io.File baseDir;
  80. // modules tree
  81. private Project parent;
  82. private List<Project> modules = new ArrayList<>();
  83. public Project(String key) {
  84. setKey(key);
  85. setEffectiveKey(key);
  86. }
  87. public Project(String key, String branch, String name) {
  88. if (StringUtils.isNotBlank(branch)) {
  89. setKey(String.format(BRANCH_KEY_FORMAT, key, branch));
  90. this.name = String.format("%s %s", name, branch);
  91. } else {
  92. setKey(key);
  93. this.name = name;
  94. }
  95. setEffectiveKey(getKey());
  96. this.branch = branch;
  97. }
  98. public String getBranch() {
  99. return branch;
  100. }
  101. /**
  102. * For internal use only.
  103. */
  104. public Project setBranch(String branch) {
  105. this.branch = branch;
  106. return this;
  107. }
  108. /**
  109. * For internal use only.
  110. */
  111. public final Project setPom(MavenProject pom) {
  112. this.pom = pom;
  113. return this;
  114. }
  115. /**
  116. * @return the project's packaging
  117. * @deprecated in 2.8. See http://jira.sonarsource.com/browse/SONAR-2341
  118. */
  119. @Deprecated
  120. public String getPackaging() {
  121. return packaging;
  122. }
  123. @Override
  124. public String getName() {
  125. return name;
  126. }
  127. @Override
  128. public String getLongName() {
  129. return name;
  130. }
  131. @Override
  132. public String getDescription() {
  133. return description;
  134. }
  135. /**
  136. * For internal use only.
  137. */
  138. public Project setName(String name) {
  139. this.name = name;
  140. return this;
  141. }
  142. /**
  143. * For internal use only.
  144. */
  145. public Project setDescription(String description) {
  146. this.description = description;
  147. return this;
  148. }
  149. /**
  150. * For internal use only.
  151. *
  152. * @deprecated in 2.8. See http://jira.sonarsource.com/browse/SONAR-2341
  153. */
  154. @Deprecated
  155. public Project setPackaging(String packaging) {
  156. this.packaging = packaging;
  157. return this;
  158. }
  159. /**
  160. * @return whether the current project is root project
  161. */
  162. public boolean isRoot() {
  163. return getParent() == null;
  164. }
  165. public Project getRoot() {
  166. return parent == null ? this : parent.getRoot();
  167. }
  168. /**
  169. * @return whether the current project is a module
  170. */
  171. public boolean isModule() {
  172. return !isRoot();
  173. }
  174. /**
  175. * @deprecated since 4.4 Since 4.3 SQ will no more run tests. So basically it's always reuse report.
  176. */
  177. @Deprecated
  178. public AnalysisType getAnalysisType() {
  179. return analysisType;
  180. }
  181. /**
  182. * @deprecated since 4.4 Since 4.3 SQ will no more run tests. So basically it's always reuse report.
  183. */
  184. @Deprecated
  185. public Project setAnalysisType(AnalysisType at) {
  186. this.analysisType = at;
  187. return this;
  188. }
  189. /**
  190. * whether it's the latest analysis done on this project (displayed in sonar dashboard) or an analysis on a past revision.
  191. *
  192. * @since 2.0
  193. * @deprecated in 3.6. The analysis is now always the latest one (past analysis must be done in a chronological order). See http://jira.sonarsource.com/browse/SONAR-4334
  194. */
  195. @Deprecated
  196. public boolean isLatestAnalysis() {
  197. return true;
  198. }
  199. /**
  200. * For internal use only.
  201. *
  202. * @deprecated in 3.6. It's not possible to analyze a project before the latest known quality snapshot.
  203. * See http://jira.sonarsource.com/browse/SONAR-4334
  204. */
  205. @Deprecated
  206. public Project setLatestAnalysis(boolean b) {
  207. if (!b) {
  208. throw new UnsupportedOperationException("The analysis is always the latest one. " +
  209. "Past analysis must be done in a chronological order.");
  210. }
  211. return this;
  212. }
  213. /**
  214. * @return the project language when there is only one language
  215. * @deprecated since 4.2 use {@link org.sonar.api.batch.fs.FileSystem#languages()}
  216. */
  217. @Deprecated
  218. @Override
  219. public Language getLanguage() {
  220. return language;
  221. }
  222. /**
  223. * Internal use
  224. */
  225. public Project setLanguage(Language language) {
  226. this.language = language;
  227. return this;
  228. }
  229. /**
  230. * @return the language key or empty if no language is specified
  231. * @deprecated since 4.2 use {@link org.sonar.api.batch.fs.FileSystem#languages()}
  232. */
  233. @Deprecated
  234. public String getLanguageKey() {
  235. if (settings == null) {
  236. throw new IllegalStateException("Project is not yet initialized");
  237. }
  238. return StringUtils.defaultIfEmpty(settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY), "");
  239. }
  240. /**
  241. * Internal use
  242. */
  243. public Project setSettings(Settings settings) {
  244. this.settings = settings;
  245. return this;
  246. }
  247. /**
  248. * Internal use for backward compatibility. Settings should be retrieved as an IoC dependency.
  249. * @deprecated since 5.0
  250. */
  251. @Deprecated
  252. public Settings getSettings() {
  253. return settings;
  254. }
  255. /**
  256. * For internal use only.
  257. */
  258. public Project setAnalysisDate(Date analysisDate) {
  259. this.analysisDate = analysisDate;
  260. return this;
  261. }
  262. /**
  263. * For internal use only.
  264. */
  265. public Project setAnalysisVersion(String analysisVersion) {
  266. this.analysisVersion = analysisVersion;
  267. return this;
  268. }
  269. /**
  270. * @return the scope of the current object
  271. */
  272. @Override
  273. public String getScope() {
  274. return Scopes.PROJECT;
  275. }
  276. /**
  277. * @return the qualifier of the current object
  278. */
  279. @Override
  280. public String getQualifier() {
  281. return isRoot() ? Qualifiers.PROJECT : Qualifiers.MODULE;
  282. }
  283. @Override
  284. public boolean matchFilePattern(String antPattern) {
  285. return false;
  286. }
  287. @CheckForNull
  288. @Override
  289. public Project getParent() {
  290. return parent;
  291. }
  292. /**
  293. * For internal use only.
  294. */
  295. public Project setParent(Project parent) {
  296. this.parent = parent;
  297. if (parent != null) {
  298. parent.modules.add(this);
  299. }
  300. return this;
  301. }
  302. /**
  303. * For internal use only.
  304. */
  305. public void removeFromParent() {
  306. if (parent != null) {
  307. parent.modules.remove(this);
  308. }
  309. }
  310. /**
  311. * @return the list of modules
  312. */
  313. public List<Project> getModules() {
  314. return modules;
  315. }
  316. /**
  317. * @return the current version of the project
  318. */
  319. public String getAnalysisVersion() {
  320. return analysisVersion;
  321. }
  322. /**
  323. * @return the analysis date, i.e. the date that will be used to store the snapshot
  324. */
  325. public Date getAnalysisDate() {
  326. return analysisDate;
  327. }
  328. /**
  329. * Note: it's better to get a reference on ProjectFileSystem as an IoC dependency (constructor parameter)
  330. * @deprecated since 3.5 use {@link FileSystem} instead
  331. */
  332. @Deprecated
  333. public ProjectFileSystem getFileSystem() {
  334. return fileSystem;
  335. }
  336. /**
  337. * For internal use only.
  338. *
  339. * @deprecated since 2.6. See http://jira.sonarsource.com/browse/SONAR-2126
  340. */
  341. @Deprecated
  342. public Project setFileSystem(ProjectFileSystem fs) {
  343. this.fileSystem = fs;
  344. return this;
  345. }
  346. /**
  347. * @deprecated since 2.5. See http://jira.sonarsource.com/browse/SONAR-2011
  348. */
  349. @Deprecated
  350. public String getGroupId() {
  351. return pom.getGroupId();
  352. }
  353. /**
  354. * @deprecated since 2.5. See http://jira.sonarsource.com/browse/SONAR-2011
  355. */
  356. @Deprecated
  357. public String getArtifactId() {
  358. return pom.getArtifactId();
  359. }
  360. /**
  361. * @return the underlying Maven project
  362. * @deprecated since 2.5. See http://jira.sonarsource.com/browse/SONAR-2011 ,
  363. * MavenProject can be retrieved as an IoC dependency
  364. */
  365. @Deprecated
  366. public MavenProject getPom() {
  367. return pom;
  368. }
  369. public static Project createFromMavenIds(String groupId, String artifactId) {
  370. return createFromMavenIds(groupId, artifactId, null);
  371. }
  372. public static Project createFromMavenIds(String groupId, String artifactId, @Nullable String branch) {
  373. return new Project(String.format(MAVEN_KEY_FORMAT, groupId, artifactId), branch, "");
  374. }
  375. @Override
  376. public String toString() {
  377. return new ToStringBuilder(this)
  378. .append("id", getId())
  379. .append("key", getKey())
  380. .append("qualifier", getQualifier())
  381. .toString();
  382. }
  383. @Override
  384. public String key() {
  385. return getKey();
  386. }
  387. @Override
  388. public String name() {
  389. return getName();
  390. }
  391. @Override
  392. public String path() {
  393. return getPath();
  394. }
  395. @Override
  396. public String longName() {
  397. return getLongName();
  398. }
  399. @Override
  400. public String qualifier() {
  401. return getQualifier();
  402. }
  403. // For internal use
  404. public void setBaseDir(java.io.File baseDir) {
  405. this.baseDir = baseDir;
  406. }
  407. java.io.File getBaseDir() {
  408. return baseDir;
  409. }
  410. }