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.

MavenUtils.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.maven.model.Plugin;
  23. import org.apache.maven.model.ReportPlugin;
  24. import org.apache.maven.project.MavenProject;
  25. import org.sonar.api.utils.log.Loggers;
  26. import java.nio.charset.Charset;
  27. import java.util.Collection;
  28. /**
  29. * An utility class to manipulate Maven concepts
  30. *
  31. * @since 1.10
  32. * @deprecated since 4.5 we don't want any dependency on Maven anymore
  33. */
  34. @Deprecated
  35. public final class MavenUtils {
  36. private static final String MAVEN_COMPILER_PLUGIN = "maven-compiler-plugin";
  37. public static final String GROUP_ID_APACHE_MAVEN = "org.apache.maven.plugins";
  38. public static final String GROUP_ID_CODEHAUS_MOJO = "org.codehaus.mojo";
  39. private MavenUtils() {
  40. // utility class with only static methods
  41. }
  42. /**
  43. * Returns the version of Java used by the maven compiler plugin
  44. *
  45. * @param pom the project pom
  46. * @return the java version
  47. */
  48. public static String getJavaVersion(MavenProject pom) {
  49. MavenPlugin compilerPlugin = MavenPlugin.getPlugin(pom, GROUP_ID_APACHE_MAVEN, MAVEN_COMPILER_PLUGIN);
  50. if (compilerPlugin != null) {
  51. return compilerPlugin.getParameter("target");
  52. }
  53. return null;
  54. }
  55. public static String getJavaSourceVersion(MavenProject pom) {
  56. MavenPlugin compilerPlugin = MavenPlugin.getPlugin(pom, GROUP_ID_APACHE_MAVEN, MAVEN_COMPILER_PLUGIN);
  57. if (compilerPlugin != null) {
  58. return compilerPlugin.getParameter("source");
  59. }
  60. return null;
  61. }
  62. /**
  63. * Queries a collection of plugins based on a group id and an artifact id and returns the plugin if it exists
  64. *
  65. * @param plugins the plugins collection
  66. * @param groupId the group id
  67. * @param artifactId the artifact id
  68. * @return the corresponding plugin if it exists, null otherwise
  69. */
  70. public static Plugin getPlugin(Collection<Plugin> plugins, String groupId, String artifactId) {
  71. if (plugins != null) {
  72. for (Plugin plugin : plugins) {
  73. if (equals(plugin, groupId, artifactId)) {
  74. return plugin;
  75. }
  76. }
  77. }
  78. return null;
  79. }
  80. /**
  81. * Tests whether a plugin has got a given artifact id and group id
  82. *
  83. * @param plugin the plugin to test
  84. * @param groupId the group id
  85. * @param artifactId the artifact id
  86. * @return whether the plugin has got group + artifact ids
  87. */
  88. public static boolean equals(Plugin plugin, String groupId, String artifactId) {
  89. if (plugin != null && plugin.getArtifactId().equals(artifactId)) {
  90. if (plugin.getGroupId() == null) {
  91. return groupId == null || groupId.equals(MavenUtils.GROUP_ID_APACHE_MAVEN) || groupId.equals(MavenUtils.GROUP_ID_CODEHAUS_MOJO);
  92. }
  93. return plugin.getGroupId().equals(groupId);
  94. }
  95. return false;
  96. }
  97. /**
  98. * Tests whether a ReportPlugin has got a given artifact id and group id
  99. *
  100. * @param plugin the ReportPlugin to test
  101. * @param groupId the group id
  102. * @param artifactId the artifact id
  103. * @return whether the ReportPlugin has got group + artifact ids
  104. */
  105. public static boolean equals(ReportPlugin plugin, String groupId, String artifactId) {
  106. if (plugin != null && plugin.getArtifactId().equals(artifactId)) {
  107. if (plugin.getGroupId() == null) {
  108. return groupId == null || groupId.equals(MavenUtils.GROUP_ID_APACHE_MAVEN) || groupId.equals(MavenUtils.GROUP_ID_CODEHAUS_MOJO);
  109. }
  110. return plugin.getGroupId().equals(groupId);
  111. }
  112. return false;
  113. }
  114. /**
  115. * @return source encoding
  116. */
  117. public static String getSourceEncoding(MavenProject pom) {
  118. return pom.getProperties().getProperty("project.build.sourceEncoding");
  119. }
  120. /**
  121. * Returns the charset of a pom
  122. *
  123. * @param pom the project pom
  124. * @return the charset
  125. */
  126. public static Charset getSourceCharset(MavenProject pom) {
  127. String encoding = getSourceEncoding(pom);
  128. if (StringUtils.isNotEmpty(encoding)) {
  129. try {
  130. return Charset.forName(encoding);
  131. } catch (Exception e) {
  132. Loggers.get(MavenUtils.class).warn("Can not get project charset", e);
  133. }
  134. }
  135. return Charset.defaultCharset();
  136. }
  137. }