1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.devops.conga.tooling.maven.plugin.util;
21
22 import java.io.File;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.net.URLClassLoader;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.maven.artifact.DependencyResolutionRequiredException;
31 import org.apache.maven.model.Resource;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.project.MavenProject;
34
35 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
36 import io.wcm.devops.conga.generator.spi.context.PluginContextOptions;
37 import io.wcm.devops.conga.generator.spi.context.ValueProviderGlobalContext;
38 import io.wcm.devops.conga.generator.util.VariableMapResolver;
39 import io.wcm.devops.conga.generator.util.VariableStringResolver;
40
41
42
43
44 public final class ClassLoaderUtil {
45
46 private ClassLoaderUtil() {
47
48 }
49
50
51
52
53
54
55 @SuppressFBWarnings("DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED")
56 public static ClassLoader buildClassLoader(List<URL> classpathUrls) {
57 return new URLClassLoader(classpathUrls.toArray(new URL[0]));
58 }
59
60
61
62
63
64
65
66 public static List<URL> getMavenProjectClasspathUrls(MavenProject project) throws MojoExecutionException {
67 try {
68 List<URL> classpathUrls = new ArrayList<>();
69 for (String path : project.getCompileClasspathElements()) {
70 classpathUrls.add(new File(path).toURI().toURL());
71 }
72 for (Resource resource : project.getResources()) {
73 classpathUrls.add(new File(resource.getDirectory()).toURI().toURL());
74 }
75 return classpathUrls;
76 }
77 catch (MalformedURLException | DependencyResolutionRequiredException ex) {
78 throw new MojoExecutionException("Unable to get classpath elements for class loader.", ex);
79 }
80 }
81
82
83
84
85
86
87
88 public static String resolveDependencyUrl(String dependencyUrl, PluginContextOptions pluginContextOptions) {
89
90 ValueProviderGlobalContext valueProviderGlobalContext = new ValueProviderGlobalContext()
91 .pluginContextOptions(pluginContextOptions);
92 VariableMapResolver variableMapResolver = new VariableMapResolver(valueProviderGlobalContext);
93 VariableStringResolver variableStringResolver = new VariableStringResolver(valueProviderGlobalContext, variableMapResolver);
94
95
96 return variableStringResolver.resolveString(dependencyUrl, Map.of());
97 }
98
99 }