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 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 java.util.ArrayList;
  22. import java.util.Date;
  23. import java.util.List;
  24. import javax.annotation.CheckForNull;
  25. import javax.annotation.Nullable;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.apache.commons.lang.builder.ToStringBuilder;
  28. import org.sonar.api.CoreProperties;
  29. import org.sonar.api.batch.fs.FileSystem;
  30. import org.sonar.api.component.Component;
  31. import org.sonar.api.config.Settings;
  32. /**
  33. * A class that manipulates Projects in the Sonar way.
  34. *
  35. * @since 1.10
  36. */
  37. public class Project extends Resource implements Component {
  38. /**
  39. * Internal use
  40. */
  41. public static final Language NONE_LANGUAGE = new AbstractLanguage("none", "None") {
  42. @Override
  43. public String[] getFileSuffixes() {
  44. return new String[0];
  45. }
  46. };
  47. static final String MAVEN_KEY_FORMAT = "%s:%s";
  48. private static final String BRANCH_KEY_FORMAT = "%s:%s";
  49. public static final String SCOPE = Scopes.PROJECT;
  50. /**
  51. * Enumerates the type of possible analysis
  52. * @deprecated since 4.4 Since 4.3 SQ will no more run tests. So basically it's always reuse report.
  53. */
  54. @Deprecated
  55. public enum AnalysisType {
  56. STATIC, DYNAMIC, REUSE_REPORTS;
  57. /**
  58. * @param includeReuseReportMode whether to count report reuse as dynamic or not
  59. * @return whether this a dynamic analysis
  60. */
  61. public boolean isDynamic(boolean includeReuseReportMode) {
  62. return equals(Project.AnalysisType.DYNAMIC) ||
  63. (equals(Project.AnalysisType.REUSE_REPORTS) && includeReuseReportMode);
  64. }
  65. }
  66. private String branch;
  67. private ProjectFileSystem fileSystem;
  68. private String name;
  69. private String description;
  70. private Language language;
  71. private Date analysisDate;
  72. private AnalysisType analysisType;
  73. private String analysisVersion;
  74. private Settings settings;
  75. // For internal use
  76. private java.io.File baseDir;
  77. // modules tree
  78. private Project parent;
  79. private List<Project> modules = new ArrayList<>();
  80. public Project(String key) {
  81. setKey(key);
  82. setEffectiveKey(key);
  83. }
  84. public Project(String key, String branch, String name) {
  85. if (StringUtils.isNotBlank(branch)) {
  86. setKey(String.format(BRANCH_KEY_FORMAT, key, branch));
  87. this.name = String.format("%s %s", name, branch);
  88. } else {
  89. setKey(key);
  90. this.name = name;
  91. }
  92. setEffectiveKey(getKey());
  93. this.branch = branch;
  94. }
  95. public String getBranch() {
  96. return branch;
  97. }
  98. /**
  99. * For internal use only.
  100. */
  101. public Project setBranch(String branch) {
  102. this.branch = branch;
  103. return this;
  104. }
  105. @Override
  106. public String getName() {
  107. return name;
  108. }
  109. @Override
  110. public String getLongName() {
  111. return name;
  112. }
  113. @Override
  114. public String getDescription() {
  115. return description;
  116. }
  117. /**
  118. * For internal use only.
  119. */
  120. public Project setName(String name) {
  121. this.name = name;
  122. return this;
  123. }
  124. /**
  125. * For internal use only.
  126. */
  127. public Project setDescription(String description) {
  128. this.description = description;
  129. return this;
  130. }
  131. /**
  132. * @return whether the current project is root project
  133. */
  134. public boolean isRoot() {
  135. return getParent() == null;
  136. }
  137. public Project getRoot() {
  138. return parent == null ? this : parent.getRoot();
  139. }
  140. /**
  141. * @return whether the current project is a module
  142. */
  143. public boolean isModule() {
  144. return !isRoot();
  145. }
  146. /**
  147. * @deprecated since 4.4 Since 4.3 SQ will no more run tests. So basically it's always reuse report.
  148. */
  149. @Deprecated
  150. public AnalysisType getAnalysisType() {
  151. return analysisType;
  152. }
  153. /**
  154. * @deprecated since 4.4 Since 4.3 SQ will no more run tests. So basically it's always reuse report.
  155. */
  156. @Deprecated
  157. public Project setAnalysisType(AnalysisType at) {
  158. this.analysisType = at;
  159. return this;
  160. }
  161. /**
  162. * whether it's the latest analysis done on this project (displayed in sonar dashboard) or an analysis on a past revision.
  163. *
  164. * @since 2.0
  165. * @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
  166. */
  167. @Deprecated
  168. public boolean isLatestAnalysis() {
  169. return true;
  170. }
  171. /**
  172. * For internal use only.
  173. *
  174. * @deprecated in 3.6. It's not possible to analyze a project before the latest known quality snapshot.
  175. * See http://jira.sonarsource.com/browse/SONAR-4334
  176. */
  177. @Deprecated
  178. public Project setLatestAnalysis(boolean b) {
  179. if (!b) {
  180. throw new UnsupportedOperationException("The analysis is always the latest one. " +
  181. "Past analysis must be done in a chronological order.");
  182. }
  183. return this;
  184. }
  185. /**
  186. * @return the project language when there is only one language
  187. * @deprecated since 4.2 use {@link org.sonar.api.batch.fs.FileSystem#languages()}
  188. */
  189. @Deprecated
  190. @Override
  191. public Language getLanguage() {
  192. return language;
  193. }
  194. /**
  195. * Internal use
  196. */
  197. public Project setLanguage(Language language) {
  198. this.language = language;
  199. return this;
  200. }
  201. /**
  202. * @return the language key or empty if no language is specified
  203. * @deprecated since 4.2 use {@link org.sonar.api.batch.fs.FileSystem#languages()}
  204. */
  205. @Deprecated
  206. public String getLanguageKey() {
  207. if (settings == null) {
  208. throw new IllegalStateException("Project is not yet initialized");
  209. }
  210. return StringUtils.defaultIfEmpty(settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY), "");
  211. }
  212. /**
  213. * Internal use
  214. */
  215. public Project setSettings(Settings settings) {
  216. this.settings = settings;
  217. return this;
  218. }
  219. /**
  220. * Internal use for backward compatibility. Settings should be retrieved as an IoC dependency.
  221. * @deprecated since 5.0
  222. */
  223. @Deprecated
  224. public Settings getSettings() {
  225. return settings;
  226. }
  227. /**
  228. * For internal use only.
  229. */
  230. public Project setAnalysisDate(Date analysisDate) {
  231. this.analysisDate = analysisDate;
  232. return this;
  233. }
  234. /**
  235. * For internal use only.
  236. */
  237. public Project setAnalysisVersion(String analysisVersion) {
  238. this.analysisVersion = analysisVersion;
  239. return this;
  240. }
  241. /**
  242. * @return the scope of the current object
  243. */
  244. @Override
  245. public String getScope() {
  246. return Scopes.PROJECT;
  247. }
  248. /**
  249. * @return the qualifier of the current object
  250. */
  251. @Override
  252. public String getQualifier() {
  253. return isRoot() ? Qualifiers.PROJECT : Qualifiers.MODULE;
  254. }
  255. @Override
  256. public boolean matchFilePattern(String antPattern) {
  257. return false;
  258. }
  259. @CheckForNull
  260. @Override
  261. public Project getParent() {
  262. return parent;
  263. }
  264. /**
  265. * For internal use only.
  266. */
  267. public Project setParent(Project parent) {
  268. this.parent = parent;
  269. if (parent != null) {
  270. parent.modules.add(this);
  271. }
  272. return this;
  273. }
  274. /**
  275. * For internal use only.
  276. */
  277. public void removeFromParent() {
  278. if (parent != null) {
  279. parent.modules.remove(this);
  280. }
  281. }
  282. /**
  283. * @return the list of modules
  284. */
  285. public List<Project> getModules() {
  286. return modules;
  287. }
  288. /**
  289. * @return the current version of the project
  290. */
  291. public String getAnalysisVersion() {
  292. return analysisVersion;
  293. }
  294. /**
  295. * @return the analysis date, i.e. the date that will be used to store the snapshot
  296. */
  297. public Date getAnalysisDate() {
  298. return analysisDate;
  299. }
  300. /**
  301. * Note: it's better to get a reference on ProjectFileSystem as an IoC dependency (constructor parameter)
  302. * @deprecated since 3.5 use {@link FileSystem} instead
  303. */
  304. @Deprecated
  305. public ProjectFileSystem getFileSystem() {
  306. return fileSystem;
  307. }
  308. /**
  309. * For internal use only.
  310. *
  311. * @deprecated since 2.6. See http://jira.sonarsource.com/browse/SONAR-2126
  312. */
  313. @Deprecated
  314. public Project setFileSystem(ProjectFileSystem fs) {
  315. this.fileSystem = fs;
  316. return this;
  317. }
  318. public static Project createFromMavenIds(String groupId, String artifactId) {
  319. return createFromMavenIds(groupId, artifactId, null);
  320. }
  321. public static Project createFromMavenIds(String groupId, String artifactId, @Nullable String branch) {
  322. return new Project(String.format(MAVEN_KEY_FORMAT, groupId, artifactId), branch, "");
  323. }
  324. @Override
  325. public String toString() {
  326. return new ToStringBuilder(this)
  327. .append("id", getId())
  328. .append("key", getKey())
  329. .append("qualifier", getQualifier())
  330. .toString();
  331. }
  332. @Override
  333. public String key() {
  334. return getKey();
  335. }
  336. @Override
  337. public String name() {
  338. return getName();
  339. }
  340. @Override
  341. public String path() {
  342. return getPath();
  343. }
  344. @Override
  345. public String longName() {
  346. return getLongName();
  347. }
  348. @Override
  349. public String qualifier() {
  350. return getQualifier();
  351. }
  352. // For internal use
  353. public void setBaseDir(java.io.File baseDir) {
  354. this.baseDir = baseDir;
  355. }
  356. java.io.File getBaseDir() {
  357. return baseDir;
  358. }
  359. }