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.

ProjectDefinition.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.batch.bootstrap;
  21. import java.io.File;
  22. import java.util.ArrayList;
  23. import java.util.LinkedHashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Map.Entry;
  27. import java.util.Properties;
  28. import javax.annotation.CheckForNull;
  29. import org.apache.commons.lang.ObjectUtils;
  30. import org.apache.commons.lang.StringUtils;
  31. import org.sonar.api.CoreProperties;
  32. /**
  33. * Defines project metadata (key, name, source directories, ...). It's generally used by the
  34. * {@link org.sonar.api.batch.bootstrap.ProjectBuilder extension point} and must not be used
  35. * by other standard extensions.
  36. *
  37. * Since 6.5, plugins should no longer manipulate the project's structure.
  38. *
  39. * @since 2.9
  40. * @deprecated since 7.6 use {@link org.sonar.api.scanner.fs.InputProject}
  41. */
  42. @Deprecated
  43. public class ProjectDefinition {
  44. public static final String SOURCES_PROPERTY = "sonar.sources";
  45. public static final String TESTS_PROPERTY = "sonar.tests";
  46. private static final char SEPARATOR = ',';
  47. private File baseDir;
  48. private File workDir;
  49. private File buildDir;
  50. private Map<String, String> properties = new LinkedHashMap<>();
  51. private ProjectDefinition parent = null;
  52. private List<ProjectDefinition> subProjects = new ArrayList<>();
  53. private ProjectDefinition(Properties p) {
  54. for (Entry<Object, Object> entry : p.entrySet()) {
  55. this.properties.put(entry.getKey().toString(), entry.getValue().toString());
  56. }
  57. }
  58. public static ProjectDefinition create() {
  59. return new ProjectDefinition(new Properties());
  60. }
  61. public ProjectDefinition setBaseDir(File baseDir) {
  62. this.baseDir = baseDir;
  63. return this;
  64. }
  65. public File getBaseDir() {
  66. return baseDir;
  67. }
  68. public ProjectDefinition setWorkDir(File workDir) {
  69. this.workDir = workDir;
  70. return this;
  71. }
  72. public File getWorkDir() {
  73. return workDir;
  74. }
  75. /**
  76. * @deprecated since 6.1 notion of buildDir is not well defined
  77. */
  78. @Deprecated
  79. public ProjectDefinition setBuildDir(File d) {
  80. this.buildDir = d;
  81. return this;
  82. }
  83. /**
  84. * @deprecated since 6.1 notion of buildDir is not well defined
  85. */
  86. @Deprecated
  87. public File getBuildDir() {
  88. return buildDir;
  89. }
  90. /**
  91. * @deprecated since 5.0 use {@link #properties()}
  92. */
  93. @Deprecated
  94. public Properties getProperties() {
  95. Properties result = new Properties();
  96. for (Map.Entry<String, String> entry : properties.entrySet()) {
  97. result.setProperty(entry.getKey(), entry.getValue());
  98. }
  99. return result;
  100. }
  101. public Map<String, String> properties() {
  102. return properties;
  103. }
  104. /**
  105. * Copies specified properties into this object.
  106. *
  107. * @since 2.12
  108. * @deprecated since 5.0 use {@link #setProperties(Map)}
  109. */
  110. @Deprecated
  111. public ProjectDefinition setProperties(Properties properties) {
  112. for (Entry<Object, Object> entry : properties.entrySet()) {
  113. this.properties.put(entry.getKey().toString(), entry.getValue().toString());
  114. }
  115. return this;
  116. }
  117. public ProjectDefinition setProperties(Map<String, String> properties) {
  118. this.properties.putAll(properties);
  119. return this;
  120. }
  121. public ProjectDefinition setProperty(String key, String value) {
  122. properties.put(key, value);
  123. return this;
  124. }
  125. public ProjectDefinition setKey(String key) {
  126. properties.put(CoreProperties.PROJECT_KEY_PROPERTY, key);
  127. return this;
  128. }
  129. public ProjectDefinition setProjectVersion(String s) {
  130. properties.put(CoreProperties.PROJECT_VERSION_PROPERTY, StringUtils.defaultString(s));
  131. return this;
  132. }
  133. public ProjectDefinition setName(String s) {
  134. properties.put(CoreProperties.PROJECT_NAME_PROPERTY, StringUtils.defaultString(s));
  135. return this;
  136. }
  137. public ProjectDefinition setDescription(String s) {
  138. properties.put(CoreProperties.PROJECT_DESCRIPTION_PROPERTY, StringUtils.defaultString(s));
  139. return this;
  140. }
  141. public String getKey() {
  142. return properties.get(CoreProperties.PROJECT_KEY_PROPERTY);
  143. }
  144. /**
  145. * @deprecated since 7.7, use {@link #getOriginalProjectVersion()} instead
  146. */
  147. @Deprecated
  148. @CheckForNull
  149. public String getOriginalVersion() {
  150. return getOriginalProjectVersion();
  151. }
  152. /**
  153. * @deprecated since 7.7, use {@link #getProjectVersion()} instead
  154. */
  155. @Deprecated
  156. public String getVersion() {
  157. return getProjectVersion();
  158. }
  159. @CheckForNull
  160. public String getOriginalProjectVersion() {
  161. return properties.get(CoreProperties.PROJECT_VERSION_PROPERTY);
  162. }
  163. public String getProjectVersion() {
  164. String version = properties.get(CoreProperties.PROJECT_VERSION_PROPERTY);
  165. if (StringUtils.isBlank(version)) {
  166. version = "not provided";
  167. }
  168. return version;
  169. }
  170. @CheckForNull
  171. public String getOriginalName() {
  172. return properties.get(CoreProperties.PROJECT_NAME_PROPERTY);
  173. }
  174. public String getName() {
  175. String name = properties.get(CoreProperties.PROJECT_NAME_PROPERTY);
  176. if (StringUtils.isBlank(name)) {
  177. name = getKey();
  178. }
  179. return name;
  180. }
  181. public String getDescription() {
  182. return properties.get(CoreProperties.PROJECT_DESCRIPTION_PROPERTY);
  183. }
  184. private void appendProperty(String key, String value) {
  185. String current = (String) ObjectUtils.defaultIfNull(properties.get(key), "");
  186. if (StringUtils.isBlank(current)) {
  187. properties.put(key, value);
  188. } else {
  189. properties.put(key, current + SEPARATOR + value);
  190. }
  191. }
  192. /**
  193. * @return Source files and folders.
  194. */
  195. public List<String> sources() {
  196. String sources = (String) ObjectUtils.defaultIfNull(properties.get(SOURCES_PROPERTY), "");
  197. return trim(StringUtils.split(sources, SEPARATOR));
  198. }
  199. /**
  200. * @param paths paths to file or directory with main sources.
  201. * They can be absolute or relative to project base directory.
  202. */
  203. public ProjectDefinition addSources(String... paths) {
  204. for (String path : paths) {
  205. appendProperty(SOURCES_PROPERTY, path);
  206. }
  207. return this;
  208. }
  209. public ProjectDefinition addSources(File... fileOrDirs) {
  210. for (File fileOrDir : fileOrDirs) {
  211. addSources(fileOrDir.getAbsolutePath());
  212. }
  213. return this;
  214. }
  215. public ProjectDefinition resetSources() {
  216. properties.remove(SOURCES_PROPERTY);
  217. return this;
  218. }
  219. public ProjectDefinition setSources(String... paths) {
  220. resetSources();
  221. return addSources(paths);
  222. }
  223. public ProjectDefinition setSources(File... filesOrDirs) {
  224. resetSources();
  225. for (File fileOrDir : filesOrDirs) {
  226. addSources(fileOrDir.getAbsolutePath());
  227. }
  228. return this;
  229. }
  230. public List<String> tests() {
  231. String sources = (String) ObjectUtils.defaultIfNull(properties.get(TESTS_PROPERTY), "");
  232. return trim(StringUtils.split(sources, SEPARATOR));
  233. }
  234. /**
  235. * @param paths path to files or directories with test sources.
  236. * It can be absolute or relative to project directory.
  237. */
  238. public ProjectDefinition addTests(String... paths) {
  239. for (String path : paths) {
  240. appendProperty(TESTS_PROPERTY, path);
  241. }
  242. return this;
  243. }
  244. public ProjectDefinition addTests(File... fileOrDirs) {
  245. for (File fileOrDir : fileOrDirs) {
  246. addTests(fileOrDir.getAbsolutePath());
  247. }
  248. return this;
  249. }
  250. public ProjectDefinition setTests(String... paths) {
  251. resetTests();
  252. return addTests(paths);
  253. }
  254. public ProjectDefinition setTests(File... fileOrDirs) {
  255. resetTests();
  256. for (File dir : fileOrDirs) {
  257. addTests(dir.getAbsolutePath());
  258. }
  259. return this;
  260. }
  261. public ProjectDefinition resetTests() {
  262. properties.remove(TESTS_PROPERTY);
  263. return this;
  264. }
  265. /**
  266. * @since 2.8
  267. */
  268. public ProjectDefinition addSubProject(ProjectDefinition child) {
  269. subProjects.add(child);
  270. child.setParent(this);
  271. return this;
  272. }
  273. @CheckForNull
  274. public ProjectDefinition getParent() {
  275. return parent;
  276. }
  277. public void remove() {
  278. if (parent != null) {
  279. parent.subProjects.remove(this);
  280. parent = null;
  281. subProjects.clear();
  282. }
  283. }
  284. private void setParent(ProjectDefinition parent) {
  285. this.parent = parent;
  286. }
  287. /**
  288. * @since 2.8
  289. */
  290. public List<ProjectDefinition> getSubProjects() {
  291. return subProjects;
  292. }
  293. private static List<String> trim(String[] strings) {
  294. List<String> result = new ArrayList<>();
  295. for (String s : strings) {
  296. result.add(StringUtils.trim(s));
  297. }
  298. return result;
  299. }
  300. @Override
  301. public boolean equals(Object o) {
  302. if (this == o) {
  303. return true;
  304. }
  305. if (o == null || getClass() != o.getClass()) {
  306. return false;
  307. }
  308. ProjectDefinition that = (ProjectDefinition) o;
  309. String key = getKey();
  310. return !((key != null) ? !key.equals(that.getKey()) : (that.getKey() != null));
  311. }
  312. @Override
  313. public int hashCode() {
  314. String key = getKey();
  315. return key != null ? key.hashCode() : 0;
  316. }
  317. }