1 package org.apache.maven.plugin.coreit;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.plugin.MojoExecutionException;
23
24 import java.io.File;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.net.URLClassLoader;
28 import java.util.List;
29
30 /**
31 * Loads classes and/or resources from a custom class loader that holds the project dependencies and records the results
32 * in a properties file.
33 *
34 * @goal load-dependencies
35 * @phase initialize
36 * @requiresDependencyResolution compile
37 *
38 * @author Benjamin Bentmann
39 *
40 */
41 public class LoadDependenciesMojo
42 extends AbstractLoadMojo
43 {
44
45 /**
46 * The project's class path to load classes/resources from.
47 *
48 * @parameter default-value="${project.compileClasspathElements}"
49 * @readonly
50 */
51 private List classPath;
52
53 /**
54 * The path to the properties file used to track the results of the class/resource loading via the project class
55 * loader.
56 *
57 * @parameter property="clsldr.projectClassLoaderOutput"
58 */
59 private File projectClassLoaderOutput;
60
61 /**
62 * Runs this mojo.
63 *
64 * @throws MojoExecutionException If the output file could not be created.
65 */
66 public void execute()
67 throws MojoExecutionException
68 {
69 URL[] urls = new URL[classPath.size()];
70 for ( int i = 0; i < urls.length; i++ )
71 {
72 try
73 {
74 urls[i] = new File( (String) classPath.get( i ) ).toURI().toURL();
75 getLog().info( "[MAVEN-CORE-IT-LOG] Using " + urls[i] );
76 }
77 catch ( MalformedURLException e )
78 {
79 getLog().error( "[MAVEN-CORE-IT-LOG] Failed to convert to URL " + classPath.get( i ), e );
80 }
81 }
82
83 ClassLoader projectClassLoader = new URLClassLoader( urls, getClass().getClassLoader() );
84
85 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
86 try
87 {
88 Thread.currentThread().setContextClassLoader( projectClassLoader );
89
90 execute( projectClassLoaderOutput, projectClassLoader );
91 }
92 finally
93 {
94 Thread.currentThread().setContextClassLoader( contextClassLoader );
95 }
96 }
97
98 }