1 | |
package org.apache.maven.plugin.coreit; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.manager.WagonManager; |
23 | |
import org.apache.maven.plugin.AbstractMojo; |
24 | |
import org.apache.maven.plugin.MojoExecutionException; |
25 | |
import org.apache.maven.plugin.MojoFailureException; |
26 | |
import org.apache.maven.wagon.repository.Repository; |
27 | |
|
28 | |
import java.io.File; |
29 | |
import java.io.FileOutputStream; |
30 | |
import java.io.IOException; |
31 | |
import java.io.OutputStream; |
32 | |
import java.net.URL; |
33 | |
import java.util.Collections; |
34 | |
import java.util.List; |
35 | |
import java.util.Properties; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public class LoadResourceMojo |
48 | |
extends AbstractMojo |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
private WagonManager wagonManager; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private File wagonClassLoaderOutput; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private String wagonProtocol; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
private String repositoryId; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
private String[] resourcePaths; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public void execute() |
96 | |
throws MojoExecutionException, MojoFailureException |
97 | |
{ |
98 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Looking up wagon for protocol " + wagonProtocol ); |
99 | |
|
100 | |
Object wagon; |
101 | |
try |
102 | |
{ |
103 | 0 | if ( repositoryId != null ) |
104 | |
{ |
105 | 0 | wagon = wagonManager.getWagon( new Repository( repositoryId, wagonProtocol + "://host/path" ) ); |
106 | |
} |
107 | |
else |
108 | |
{ |
109 | 0 | wagon = wagonManager.getWagon( wagonProtocol ); |
110 | |
} |
111 | |
} |
112 | 0 | catch ( Exception e ) |
113 | |
{ |
114 | 0 | throw new MojoExecutionException( "Failed to load wagon for protocol " + wagonProtocol, e ); |
115 | 0 | } |
116 | |
|
117 | 0 | ClassLoader classLoader = wagon.getClass().getClassLoader(); |
118 | |
|
119 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Using class loader " + classLoader ); |
120 | |
|
121 | 0 | Properties loaderProperties = new Properties(); |
122 | 0 | loaderProperties.setProperty( "wagon.class", wagon.getClass().getName() ); |
123 | |
|
124 | 0 | if ( resourcePaths != null ) |
125 | |
{ |
126 | 0 | for ( int i = 0; i < resourcePaths.length; i++ ) |
127 | |
{ |
128 | 0 | String path = resourcePaths[i]; |
129 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Loading resource " + path ); |
130 | |
|
131 | 0 | URL url = classLoader.getResource( path ); |
132 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Loaded resource from " + url ); |
133 | 0 | if ( url != null ) |
134 | |
{ |
135 | 0 | loaderProperties.setProperty( path, url.toString() ); |
136 | |
} |
137 | |
|
138 | |
try |
139 | |
{ |
140 | 0 | List urls = Collections.list( classLoader.getResources( path ) ); |
141 | 0 | loaderProperties.setProperty( path + ".count", "" + urls.size() ); |
142 | 0 | for ( int j = 0; j < urls.size(); j++ ) |
143 | |
{ |
144 | 0 | loaderProperties.setProperty( path + "." + j, urls.get( j ).toString() ); |
145 | |
} |
146 | |
} |
147 | 0 | catch ( IOException e ) |
148 | |
{ |
149 | 0 | throw new MojoExecutionException( "Resources could not be enumerated: " + path, e ); |
150 | 0 | } |
151 | |
} |
152 | |
} |
153 | |
|
154 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + wagonClassLoaderOutput ); |
155 | |
|
156 | 0 | OutputStream out = null; |
157 | |
try |
158 | |
{ |
159 | 0 | wagonClassLoaderOutput.getParentFile().mkdirs(); |
160 | 0 | out = new FileOutputStream( wagonClassLoaderOutput ); |
161 | 0 | loaderProperties.store( out, "MAVEN-CORE-IT-LOG" ); |
162 | |
} |
163 | 0 | catch ( IOException e ) |
164 | |
{ |
165 | 0 | throw new MojoExecutionException( "Output file could not be created: " + wagonClassLoaderOutput, e ); |
166 | |
} |
167 | |
finally |
168 | |
{ |
169 | 0 | if ( out != null ) |
170 | |
{ |
171 | |
try |
172 | |
{ |
173 | 0 | out.close(); |
174 | |
} |
175 | 0 | catch ( IOException e ) |
176 | |
{ |
177 | |
|
178 | 0 | } |
179 | |
} |
180 | |
} |
181 | |
|
182 | 0 | getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + wagonClassLoaderOutput ); |
183 | 0 | } |
184 | |
|
185 | |
} |