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.

CloverMavenPluginHandler.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar 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. * Sonar 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
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.plugins.clover;
  21. import org.apache.commons.configuration.Configuration;
  22. import org.apache.commons.io.FileUtils;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.sonar.api.CoreProperties;
  25. import org.sonar.api.batch.maven.MavenPlugin;
  26. import org.sonar.api.batch.maven.MavenPluginHandler;
  27. import org.sonar.api.batch.maven.MavenSurefireUtils;
  28. import org.sonar.api.resources.Project;
  29. import org.sonar.api.resources.ProjectUtils;
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.util.Collection;
  33. public class CloverMavenPluginHandler implements MavenPluginHandler {
  34. private Configuration conf;
  35. public CloverMavenPluginHandler(Configuration conf) {
  36. this.conf = conf;
  37. }
  38. public String getGroupId() {
  39. return CloverConstants.MAVEN_GROUP_ID;
  40. }
  41. public String getArtifactId() {
  42. return CloverConstants.MAVEN_ARTIFACT_ID;
  43. }
  44. public String getVersion() {
  45. return conf.getString(CloverConstants.VERSION_PROPERTY, CloverConstants.DEFAULT_VERSION);
  46. }
  47. public boolean isFixedVersion() {
  48. return false;
  49. }
  50. public String[] getGoals() {
  51. return new String[]{"instrument", "clover"};
  52. }
  53. public void configure(Project project, MavenPlugin cloverPlugin) {
  54. configureParameters(project, cloverPlugin);
  55. configureLicense(project, cloverPlugin);
  56. configureSurefire(project, cloverPlugin);
  57. }
  58. private void configureSurefire(Project project, MavenPlugin cloverPlugin) {
  59. MavenSurefireUtils.configure(project);
  60. boolean skipCloverLaunch = project.getConfiguration().getBoolean("maven.clover.skip", false);
  61. if (!skipCloverLaunch) {
  62. String skipInPomConfig = cloverPlugin.getParameter("skip");
  63. skipCloverLaunch = StringUtils.isNotBlank(skipInPomConfig) ? Boolean.parseBoolean(skipInPomConfig) : false;
  64. }
  65. if (!project.getConfiguration().containsKey(CoreProperties.SUREFIRE_REPORTS_PATH_PROPERTY) && !skipCloverLaunch) {
  66. project.getConfiguration().setProperty(CoreProperties.SUREFIRE_REPORTS_PATH_PROPERTY, new File(project.getFileSystem().getBuildDir(), "clover/surefire-reports").getAbsolutePath());
  67. }
  68. }
  69. protected void configureParameters(Project project, MavenPlugin cloverPlugin) {
  70. cloverPlugin.setParameter("generateXml", "true");
  71. String javaVersion = ProjectUtils.getJavaVersion(project);
  72. if (javaVersion != null) {
  73. cloverPlugin.setParameter("jdk", javaVersion);
  74. }
  75. if (project.getExclusionPatterns() != null) {
  76. for (String pattern : project.getExclusionPatterns()) {
  77. cloverPlugin.addParameter("excludes/exclude", pattern);
  78. }
  79. }
  80. }
  81. protected void configureLicense(Project project, MavenPlugin cloverPlugin) {
  82. if (!hasLicense(cloverPlugin) && getGlobalLicense(project) != null) {
  83. String license = getGlobalLicense(project);
  84. File licenseFile = writeLicenseToDisk(project, license);
  85. cloverPlugin.setParameter("licenseLocation", licenseFile.getAbsolutePath());
  86. }
  87. }
  88. private boolean hasLicense(MavenPlugin cloverPlugin) {
  89. return StringUtils.isNotBlank(cloverPlugin.getParameter("license")) || StringUtils.isNotBlank(cloverPlugin.getParameter("licenseLocation"));
  90. }
  91. private File writeLicenseToDisk(Project project, String license) {
  92. try {
  93. File file = new File(project.getFileSystem().getSonarWorkingDirectory(), "clover.license");
  94. FileUtils.writeStringToFile(file, license);
  95. return file;
  96. } catch (IOException e) {
  97. throw new RuntimeException("can not write the clover license", e);
  98. }
  99. }
  100. private String getGlobalLicense(Project project) {
  101. Object value = project.getProperty(CloverConstants.LICENSE_PROPERTY);
  102. if (value != null) {
  103. if (value instanceof String) {
  104. return (String) value;
  105. } else if (value instanceof Collection) {
  106. return StringUtils.join(((Collection) value), "");
  107. }
  108. }
  109. return null;
  110. }
  111. }