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.

ProjectClasspath.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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;
  21. import org.apache.maven.artifact.DependencyResolutionRequiredException;
  22. import org.apache.maven.project.MavenProject;
  23. import org.sonar.api.utils.SonarException;
  24. import java.io.File;
  25. import java.net.MalformedURLException;
  26. import java.net.URL;
  27. import java.net.URLClassLoader;
  28. import java.util.ArrayList;
  29. import java.util.List;
  30. /**
  31. * @since 2.2
  32. * @deprecated since 4.5 this is some Java specific stuff that should by handled by SQ Java plugin
  33. */
  34. @Deprecated
  35. @BatchSide
  36. public class ProjectClasspath {
  37. protected MavenProject pom;
  38. private List<File> elements;
  39. private URLClassLoader classloader;
  40. public ProjectClasspath(MavenProject pom) {
  41. this.pom = pom;
  42. }
  43. public URLClassLoader getClassloader() {
  44. if (classloader == null) {
  45. classloader = createClassLoader();
  46. }
  47. return classloader;
  48. }
  49. /**
  50. * bytecode directory + JARs (dependencies)
  51. */
  52. public List<File> getElements() {
  53. if (elements == null) {
  54. elements = createElements();
  55. }
  56. return elements;
  57. }
  58. protected URLClassLoader createClassLoader() {
  59. try {
  60. List<URL> urls = new ArrayList<>();
  61. for (File file : getElements()) {
  62. urls.add(file.toURI().toURL());
  63. }
  64. return new URLClassLoader(urls.toArray(new URL[urls.size()]), null);
  65. } catch (MalformedURLException e) {
  66. throw new SonarException("Fail to create the project classloader. Classpath element is unvalid.", e);
  67. }
  68. }
  69. protected List<File> createElements() {
  70. try {
  71. List<File> files = new ArrayList<>();
  72. if (pom.getCompileClasspathElements() != null) {
  73. for (String classPathString : pom.getCompileClasspathElements()) {
  74. files.add(new File(classPathString));
  75. }
  76. }
  77. if (pom.getBuild().getOutputDirectory() != null) {
  78. File outputDirectoryFile = new File(pom.getBuild().getOutputDirectory());
  79. if (outputDirectoryFile.exists()) {
  80. files.add(outputDirectoryFile);
  81. }
  82. }
  83. return files;
  84. } catch (DependencyResolutionRequiredException e) {
  85. throw new SonarException("Fail to create the project classloader", e);
  86. }
  87. }
  88. }