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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 Map<String, String> properties = new LinkedHashMap<>();
  50. private ProjectDefinition parent = null;
  51. private List<ProjectDefinition> subProjects = new ArrayList<>();
  52. private ProjectDefinition(Properties p) {
  53. for (Entry<Object, Object> entry : p.entrySet()) {
  54. this.properties.put(entry.getKey().toString(), entry.getValue().toString());
  55. }
  56. }
  57. public static ProjectDefinition create() {
  58. return new ProjectDefinition(new Properties());
  59. }
  60. public ProjectDefinition setBaseDir(File baseDir) {
  61. this.baseDir = baseDir;
  62. return this;
  63. }
  64. public File getBaseDir() {
  65. return baseDir;
  66. }
  67. public ProjectDefinition setWorkDir(File workDir) {
  68. this.workDir = workDir;
  69. return this;
  70. }
  71. public File getWorkDir() {
  72. return workDir;
  73. }
  74. public Map<String, String> properties() {
  75. return properties;
  76. }
  77. public ProjectDefinition setProperties(Map<String, String> properties) {
  78. this.properties.putAll(properties);
  79. return this;
  80. }
  81. public ProjectDefinition setProperty(String key, String value) {
  82. properties.put(key, value);
  83. return this;
  84. }
  85. public ProjectDefinition setKey(String key) {
  86. properties.put(CoreProperties.PROJECT_KEY_PROPERTY, key);
  87. return this;
  88. }
  89. public ProjectDefinition setProjectVersion(String s) {
  90. properties.put(CoreProperties.PROJECT_VERSION_PROPERTY, StringUtils.defaultString(s));
  91. return this;
  92. }
  93. public ProjectDefinition setName(String s) {
  94. properties.put(CoreProperties.PROJECT_NAME_PROPERTY, StringUtils.defaultString(s));
  95. return this;
  96. }
  97. public ProjectDefinition setDescription(String s) {
  98. properties.put(CoreProperties.PROJECT_DESCRIPTION_PROPERTY, StringUtils.defaultString(s));
  99. return this;
  100. }
  101. public String getKey() {
  102. return properties.get(CoreProperties.PROJECT_KEY_PROPERTY);
  103. }
  104. /**
  105. * @deprecated since 7.7, use {@link #getOriginalProjectVersion()} instead
  106. */
  107. @Deprecated
  108. @CheckForNull
  109. public String getOriginalVersion() {
  110. return getOriginalProjectVersion();
  111. }
  112. /**
  113. * @deprecated since 7.7, use {@link #getProjectVersion()} instead
  114. */
  115. @Deprecated
  116. public String getVersion() {
  117. return getProjectVersion();
  118. }
  119. @CheckForNull
  120. public String getOriginalProjectVersion() {
  121. return properties.get(CoreProperties.PROJECT_VERSION_PROPERTY);
  122. }
  123. public String getProjectVersion() {
  124. String version = properties.get(CoreProperties.PROJECT_VERSION_PROPERTY);
  125. if (StringUtils.isBlank(version)) {
  126. version = "not provided";
  127. }
  128. return version;
  129. }
  130. @CheckForNull
  131. public String getOriginalName() {
  132. return properties.get(CoreProperties.PROJECT_NAME_PROPERTY);
  133. }
  134. public String getName() {
  135. String name = properties.get(CoreProperties.PROJECT_NAME_PROPERTY);
  136. if (StringUtils.isBlank(name)) {
  137. name = getKey();
  138. }
  139. return name;
  140. }
  141. public String getDescription() {
  142. return properties.get(CoreProperties.PROJECT_DESCRIPTION_PROPERTY);
  143. }
  144. private void appendProperty(String key, String value) {
  145. String current = (String) ObjectUtils.defaultIfNull(properties.get(key), "");
  146. if (StringUtils.isBlank(current)) {
  147. properties.put(key, value);
  148. } else {
  149. properties.put(key, current + SEPARATOR + value);
  150. }
  151. }
  152. /**
  153. * @return Source files and folders.
  154. */
  155. public List<String> sources() {
  156. String sources = (String) ObjectUtils.defaultIfNull(properties.get(SOURCES_PROPERTY), "");
  157. return trim(StringUtils.split(sources, SEPARATOR));
  158. }
  159. /**
  160. * @param paths paths to file or directory with main sources.
  161. * They can be absolute or relative to project base directory.
  162. */
  163. public ProjectDefinition addSources(String... paths) {
  164. for (String path : paths) {
  165. appendProperty(SOURCES_PROPERTY, path);
  166. }
  167. return this;
  168. }
  169. public ProjectDefinition addSources(File... fileOrDirs) {
  170. for (File fileOrDir : fileOrDirs) {
  171. addSources(fileOrDir.getAbsolutePath());
  172. }
  173. return this;
  174. }
  175. public ProjectDefinition resetSources() {
  176. properties.remove(SOURCES_PROPERTY);
  177. return this;
  178. }
  179. public ProjectDefinition setSources(String... paths) {
  180. resetSources();
  181. return addSources(paths);
  182. }
  183. public ProjectDefinition setSources(File... filesOrDirs) {
  184. resetSources();
  185. for (File fileOrDir : filesOrDirs) {
  186. addSources(fileOrDir.getAbsolutePath());
  187. }
  188. return this;
  189. }
  190. public List<String> tests() {
  191. String sources = (String) ObjectUtils.defaultIfNull(properties.get(TESTS_PROPERTY), "");
  192. return trim(StringUtils.split(sources, SEPARATOR));
  193. }
  194. /**
  195. * @param paths path to files or directories with test sources.
  196. * It can be absolute or relative to project directory.
  197. */
  198. public ProjectDefinition addTests(String... paths) {
  199. for (String path : paths) {
  200. appendProperty(TESTS_PROPERTY, path);
  201. }
  202. return this;
  203. }
  204. public ProjectDefinition addTests(File... fileOrDirs) {
  205. for (File fileOrDir : fileOrDirs) {
  206. addTests(fileOrDir.getAbsolutePath());
  207. }
  208. return this;
  209. }
  210. public ProjectDefinition setTests(String... paths) {
  211. resetTests();
  212. return addTests(paths);
  213. }
  214. public ProjectDefinition setTests(File... fileOrDirs) {
  215. resetTests();
  216. for (File dir : fileOrDirs) {
  217. addTests(dir.getAbsolutePath());
  218. }
  219. return this;
  220. }
  221. public ProjectDefinition resetTests() {
  222. properties.remove(TESTS_PROPERTY);
  223. return this;
  224. }
  225. /**
  226. * @since 2.8
  227. */
  228. public ProjectDefinition addSubProject(ProjectDefinition child) {
  229. subProjects.add(child);
  230. child.setParent(this);
  231. return this;
  232. }
  233. @CheckForNull
  234. public ProjectDefinition getParent() {
  235. return parent;
  236. }
  237. public void remove() {
  238. if (parent != null) {
  239. parent.subProjects.remove(this);
  240. parent = null;
  241. subProjects.clear();
  242. }
  243. }
  244. private void setParent(ProjectDefinition parent) {
  245. this.parent = parent;
  246. }
  247. /**
  248. * @since 2.8
  249. */
  250. public List<ProjectDefinition> getSubProjects() {
  251. return subProjects;
  252. }
  253. private static List<String> trim(String[] strings) {
  254. List<String> result = new ArrayList<>();
  255. for (String s : strings) {
  256. result.add(StringUtils.trim(s));
  257. }
  258. return result;
  259. }
  260. @Override
  261. public boolean equals(Object o) {
  262. if (this == o) {
  263. return true;
  264. }
  265. if (o == null || getClass() != o.getClass()) {
  266. return false;
  267. }
  268. ProjectDefinition that = (ProjectDefinition) o;
  269. String key = getKey();
  270. return !((key != null) ? !key.equals(that.getKey()) : (that.getKey() != null));
  271. }
  272. @Override
  273. public int hashCode() {
  274. String key = getKey();
  275. return key != null ? key.hashCode() : 0;
  276. }
  277. }