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.

MavenPlugin.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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.batch.maven;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.apache.commons.lang.builder.ToStringBuilder;
  23. import org.apache.maven.model.Plugin;
  24. import org.apache.maven.model.ReportPlugin;
  25. import org.apache.maven.project.MavenProject;
  26. import org.codehaus.plexus.util.xml.Xpp3Dom;
  27. import java.util.Collection;
  28. import java.util.Iterator;
  29. import java.util.List;
  30. /**
  31. * A class to handle maven plugins
  32. *
  33. * @since 1.10
  34. * @deprecated since 4.5 we don't want any dependency on Maven anymore
  35. */
  36. @Deprecated
  37. public class MavenPlugin {
  38. private static final String CONFIGURATION_ELEMENT = "configuration";
  39. private Plugin plugin;
  40. private Xpp3Dom configuration;
  41. /**
  42. * Creates a MavenPlugin based on a Plugin
  43. *
  44. * @param plugin the plugin
  45. */
  46. public MavenPlugin(Plugin plugin) {
  47. this.plugin = plugin;
  48. this.configuration = (Xpp3Dom) plugin.getConfiguration();
  49. if (this.configuration == null) {
  50. configuration = new Xpp3Dom(CONFIGURATION_ELEMENT);
  51. plugin.setConfiguration(this.configuration);
  52. }
  53. }
  54. /**
  55. * Creates a Maven plugin based on artifact + group + version
  56. *
  57. * @param groupId the group id
  58. * @param artifactId the artifact id
  59. * @param version the version
  60. */
  61. public MavenPlugin(String groupId, String artifactId, String version) {
  62. this.plugin = new Plugin();
  63. plugin.setGroupId(groupId);
  64. plugin.setArtifactId(artifactId);
  65. plugin.setVersion(version);
  66. configuration = new Xpp3Dom(CONFIGURATION_ELEMENT);
  67. plugin.setConfiguration(this.configuration);
  68. }
  69. /**
  70. * @since 3.5 - see SONAR-4070
  71. * @return the XML node <configuration> of pom
  72. */
  73. public Xpp3Dom getConfigurationXmlNode() {
  74. return configuration;
  75. }
  76. /**
  77. * Sets the maven plugin version
  78. *
  79. * @param version the version
  80. * @return this
  81. */
  82. public MavenPlugin setVersion(String version) {
  83. this.plugin.setVersion(version);
  84. return this;
  85. }
  86. /**
  87. * @return the underlying plugin
  88. */
  89. public Plugin getPlugin() {
  90. return plugin;
  91. }
  92. /**
  93. * Gets a parameter of the plugin based on its key
  94. *
  95. * @param key the param key
  96. * @return the parameter if exist, null otherwise
  97. */
  98. public String getParameter(String key) {
  99. Xpp3Dom node = findNodeWith(key);
  100. return node == null ? null : node.getValue();
  101. }
  102. /**
  103. * Gets a list of parameters of the plugin from a param key
  104. *
  105. * @param key param key with option-index snippet: e.g. item[0], item[1]. If no index snippet is passed, then
  106. * 0 is default (index <=> index[0])
  107. * @return an array of parameters if any, an empty array otherwise
  108. */
  109. public String[] getParameters(String key) {
  110. String[] keyParts = StringUtils.split(key, "/");
  111. Xpp3Dom node = configuration;
  112. for (int i = 0; i < keyParts.length - 1; i++) {
  113. node = getOrCreateChild(node, keyParts[i]);
  114. }
  115. Xpp3Dom[] children = node.getChildren(keyParts[keyParts.length - 1]);
  116. String[] result = new String[children.length];
  117. for (int i = 0; i < children.length; i++) {
  118. result[i] = children[i].getValue();
  119. }
  120. return result;
  121. }
  122. /**
  123. * Sets a parameter for the maven plugin. This will overrides an existing parameter.
  124. *
  125. * @param key the param key
  126. * @param value the param value
  127. * @return this
  128. */
  129. public MavenPlugin setParameter(String key, String value) {
  130. checkKeyArgument(key);
  131. String[] keyParts = StringUtils.split(key, "/");
  132. Xpp3Dom node = configuration;
  133. for (String keyPart : keyParts) {
  134. node = getOrCreateChild(node, keyPart);
  135. }
  136. node.setValue(value);
  137. return this;
  138. }
  139. /**
  140. * Sets a parameter to the maven plugin. Overrides existing parameter only id specified.
  141. *
  142. * @param key the param key
  143. * @param value the param value
  144. * @param override whether to override existing parameter
  145. */
  146. public void setParameter(String key, String value, boolean override) {
  147. if (getParameter(key) == null || override) {
  148. setParameter(key, value);
  149. }
  150. }
  151. /**
  152. * Removes all parameters from the maven plugin
  153. */
  154. public void removeParameters() {
  155. configuration = new Xpp3Dom(CONFIGURATION_ELEMENT);
  156. plugin.setConfiguration(this.configuration);
  157. }
  158. /**
  159. * Adds a parameter to the maven plugin
  160. *
  161. * @param key the param key with option-index snippet: e.g. item[0], item[1]. If no index snippet is passed, then
  162. * 0 is default (index <=> index[0])
  163. * @param value the param value
  164. * @return this
  165. */
  166. public MavenPlugin addParameter(String key, String value) {
  167. String[] keyParts = StringUtils.split(key, "/");
  168. Xpp3Dom node = configuration;
  169. for (int i = 0; i < keyParts.length - 1; i++) {
  170. node = getOrCreateChild(node, keyParts[i]);
  171. }
  172. Xpp3Dom leaf = new Xpp3Dom(keyParts[keyParts.length - 1]);
  173. leaf.setValue(value);
  174. node.addChild(leaf);
  175. return this;
  176. }
  177. private static Xpp3Dom getOrCreateChild(Xpp3Dom node, String key) {
  178. int childIndex = getIndex(key);
  179. if (node.getChildren(removeIndexSnippet(key)).length <= childIndex) {
  180. Xpp3Dom child = new Xpp3Dom(removeIndexSnippet(key));
  181. node.addChild(child);
  182. return child;
  183. }
  184. return node.getChildren(removeIndexSnippet(key))[childIndex];
  185. }
  186. private static int getIndex(String key) {
  187. // parsing index-syntax (e.g. item[1])
  188. if (key.matches(".*?\\[\\d+\\]")) {
  189. return Integer.parseInt(StringUtils.substringBetween(key, "[", "]"));
  190. }
  191. // for down-compatibility of api we fallback to default 0
  192. return 0;
  193. }
  194. private static String removeIndexSnippet(String key) {
  195. return StringUtils.substringBefore(key, "[");
  196. }
  197. /**
  198. * Remove a parameter from the maven plugin based on its key
  199. *
  200. * @param key param key with option-index snippet: e.g. item[0], item[1]. If no index snippet is passed, then
  201. * 0 is default (index <=> index[0])
  202. */
  203. public void removeParameter(String key) {
  204. Xpp3Dom node = findNodeWith(key);
  205. if (node != null) {
  206. remove(node);
  207. }
  208. }
  209. private Xpp3Dom findNodeWith(String key) {
  210. checkKeyArgument(key);
  211. String[] keyParts = key.split("/");
  212. Xpp3Dom node = configuration;
  213. for (String keyPart : keyParts) {
  214. if (node.getChildren(removeIndexSnippet(keyPart)).length <= getIndex(keyPart)) {
  215. return null;
  216. }
  217. node = node.getChildren(removeIndexSnippet(keyPart))[getIndex(keyPart)];
  218. if (node == null) {
  219. return null;
  220. }
  221. }
  222. return node;
  223. }
  224. private static void remove(Xpp3Dom node) {
  225. Xpp3Dom parent = node.getParent();
  226. for (int i = 0; i < parent.getChildCount(); i++) {
  227. Xpp3Dom child = parent.getChild(i);
  228. if (child.equals(node)) {
  229. parent.removeChild(i);
  230. break;
  231. }
  232. }
  233. }
  234. /**
  235. * @return whether the maven plugin has got configuration
  236. */
  237. public boolean hasConfiguration() {
  238. return configuration.getChildCount() > 0;
  239. }
  240. private static void checkKeyArgument(String key) {
  241. if (key == null) {
  242. throw new IllegalArgumentException("Parameter 'key' should not be null.");
  243. }
  244. }
  245. /**
  246. * Registers a plugin in a project pom
  247. * <p/>
  248. * <p>Adds the plugin if it does not exist or amend its version if it does exist and specified</p>
  249. *
  250. * @param pom the project pom
  251. * @param groupId the plugin group id
  252. * @param artifactId the plugin artifact id
  253. * @param version the plugin version
  254. * @param overrideVersion whether to override the version if the plugin is already registered
  255. * @return the registered plugin
  256. */
  257. public static MavenPlugin registerPlugin(MavenProject pom, String groupId, String artifactId, String version, boolean overrideVersion) {
  258. MavenPlugin plugin = getPlugin(pom, groupId, artifactId);
  259. if (plugin == null) {
  260. plugin = new MavenPlugin(groupId, artifactId, version);
  261. } else if (overrideVersion) {
  262. plugin.setVersion(version);
  263. }
  264. // remove from pom
  265. unregisterPlugin(pom, groupId, artifactId);
  266. // register
  267. pom.getBuild().addPlugin(plugin.getPlugin());
  268. return plugin;
  269. }
  270. /**
  271. * Returns a plugin from a pom based on its group id and artifact id
  272. * <p/>
  273. * <p>It searches in the build section, then the reporting section and finally the pluginManagement section</p>
  274. *
  275. * @param pom the project pom
  276. * @param groupId the plugin group id
  277. * @param artifactId the plugin artifact id
  278. * @return the plugin if it exists, null otherwise
  279. */
  280. public static MavenPlugin getPlugin(MavenProject pom, String groupId, String artifactId) {
  281. if (pom == null) {
  282. return null;
  283. }
  284. // look for plugin in <build> section
  285. Plugin plugin = null;
  286. if (pom.getBuildPlugins() != null) {
  287. plugin = getPlugin(pom.getBuildPlugins(), groupId, artifactId);
  288. }
  289. // look for plugin in <report> section
  290. if (plugin == null && pom.getReportPlugins() != null) {
  291. plugin = getReportPlugin(pom.getReportPlugins(), groupId, artifactId);
  292. }
  293. // look for plugin in <pluginManagement> section
  294. if (pom.getPluginManagement() != null) {
  295. Plugin pluginManagement = getPlugin(pom.getPluginManagement().getPlugins(), groupId, artifactId);
  296. if (plugin == null) {
  297. plugin = pluginManagement;
  298. } else if (pluginManagement != null) {
  299. if (pluginManagement.getConfiguration() != null) {
  300. if (plugin.getConfiguration() == null) {
  301. plugin.setConfiguration(pluginManagement.getConfiguration());
  302. } else {
  303. Xpp3Dom.mergeXpp3Dom((Xpp3Dom) plugin.getConfiguration(), (Xpp3Dom) pluginManagement.getConfiguration());
  304. }
  305. }
  306. if (plugin.getDependencies() == null && pluginManagement.getDependencies() != null) {
  307. plugin.setDependencies(pluginManagement.getDependencies());
  308. }
  309. if (plugin.getVersion() == null) {
  310. plugin.setVersion(pluginManagement.getVersion());
  311. }
  312. }
  313. }
  314. if (plugin != null) {
  315. return new MavenPlugin(plugin);
  316. }
  317. return null;
  318. }
  319. private static Plugin getPlugin(Collection<Plugin> plugins, String groupId, String artifactId) {
  320. if (plugins == null) {
  321. return null;
  322. }
  323. for (Plugin plugin : plugins) {
  324. if (MavenUtils.equals(plugin, groupId, artifactId)) {
  325. return plugin;
  326. }
  327. }
  328. return null;
  329. }
  330. private static Plugin getReportPlugin(Collection<ReportPlugin> plugins, String groupId, String artifactId) {
  331. if (plugins == null) {
  332. return null;
  333. }
  334. for (ReportPlugin plugin : plugins) {
  335. if (MavenUtils.equals(plugin, groupId, artifactId)) {
  336. return cloneReportPluginToPlugin(plugin);
  337. }
  338. }
  339. return null;
  340. }
  341. private static Plugin cloneReportPluginToPlugin(ReportPlugin reportPlugin) {
  342. Plugin plugin = new Plugin();
  343. plugin.setGroupId(reportPlugin.getGroupId());
  344. plugin.setArtifactId(reportPlugin.getArtifactId());
  345. plugin.setVersion(reportPlugin.getVersion());
  346. plugin.setConfiguration(reportPlugin.getConfiguration());
  347. return plugin;
  348. }
  349. private static void unregisterPlugin(MavenProject pom, String groupId, String artifactId) {
  350. if (pom.getPluginManagement() != null && pom.getPluginManagement().getPlugins() != null) {
  351. unregisterPlugin(pom.getPluginManagement().getPlugins(), groupId, artifactId);
  352. }
  353. List plugins = pom.getBuildPlugins();
  354. if (plugins != null) {
  355. unregisterPlugin(plugins, groupId, artifactId);
  356. }
  357. plugins = pom.getReportPlugins();
  358. if (plugins != null) {
  359. unregisterReportPlugin(plugins, groupId, artifactId);
  360. }
  361. }
  362. private static void unregisterPlugin(List<Plugin> plugins, String groupId, String artifactId) {
  363. for (Iterator<Plugin> iterator = plugins.iterator(); iterator.hasNext();) {
  364. Plugin p = iterator.next();
  365. if (MavenUtils.equals(p, groupId, artifactId)) {
  366. iterator.remove();
  367. }
  368. }
  369. }
  370. private static void unregisterReportPlugin(List<ReportPlugin> plugins, String groupId, String artifactId) {
  371. for (Iterator<ReportPlugin> iterator = plugins.iterator(); iterator.hasNext();) {
  372. ReportPlugin p = iterator.next();
  373. if (MavenUtils.equals(p, groupId, artifactId)) {
  374. iterator.remove();
  375. }
  376. }
  377. }
  378. @Override
  379. public String toString() {
  380. return new ToStringBuilder(this)
  381. .append("groupId", plugin.getGroupId())
  382. .append("artifactId", plugin.getArtifactId())
  383. .append("version", plugin.getVersion())
  384. .toString();
  385. }
  386. }