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 public class LoadResourceMojo
47 extends AbstractMojo
48 {
49
50
51
52
53
54
55 private WagonManager wagonManager;
56
57
58
59
60
61
62 private File wagonClassLoaderOutput;
63
64
65
66
67
68
69
70 private String wagonProtocol;
71
72
73
74
75
76
77 private String repositoryId;
78
79
80
81
82
83
84
85
86
87 private String[] resourcePaths;
88
89
90
91
92
93
94 public void execute()
95 throws MojoExecutionException, MojoFailureException
96 {
97 getLog().info( "[MAVEN-CORE-IT-LOG] Looking up wagon for protocol " + wagonProtocol );
98
99 Object wagon;
100 try
101 {
102 if ( repositoryId != null )
103 {
104 wagon = wagonManager.getWagon( new Repository( repositoryId, wagonProtocol + "://host/path" ) );
105 }
106 else
107 {
108 wagon = wagonManager.getWagon( wagonProtocol );
109 }
110 }
111 catch ( Exception e )
112 {
113 throw new MojoExecutionException( "Failed to load wagon for protocol " + wagonProtocol, e );
114 }
115
116 ClassLoader classLoader = wagon.getClass().getClassLoader();
117
118 getLog().info( "[MAVEN-CORE-IT-LOG] Using class loader " + classLoader );
119
120 Properties loaderProperties = new Properties();
121 loaderProperties.setProperty( "wagon.class", wagon.getClass().getName() );
122
123 if ( resourcePaths != null )
124 {
125 for ( String path : resourcePaths )
126 {
127 getLog().info( "[MAVEN-CORE-IT-LOG] Loading resource " + path );
128
129 URL url = classLoader.getResource( path );
130 getLog().info( "[MAVEN-CORE-IT-LOG] Loaded resource from " + url );
131 if ( url != null )
132 {
133 loaderProperties.setProperty( path, url.toString() );
134 }
135
136 try
137 {
138 List urls = Collections.list( classLoader.getResources( path ) );
139 loaderProperties.setProperty( path + ".count", "" + urls.size() );
140 for ( int j = 0; j < urls.size(); j++ )
141 {
142 loaderProperties.setProperty( path + "." + j, urls.get( j ).toString() );
143 }
144 }
145 catch ( IOException e )
146 {
147 throw new MojoExecutionException( "Resources could not be enumerated: " + path, e );
148 }
149 }
150 }
151
152 getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file " + wagonClassLoaderOutput );
153
154 OutputStream out = null;
155 try
156 {
157 wagonClassLoaderOutput.getParentFile().mkdirs();
158 out = new FileOutputStream( wagonClassLoaderOutput );
159 loaderProperties.store( out, "MAVEN-CORE-IT-LOG" );
160 }
161 catch ( IOException e )
162 {
163 throw new MojoExecutionException( "Output file could not be created: " + wagonClassLoaderOutput, e );
164 }
165 finally
166 {
167 if ( out != null )
168 {
169 try
170 {
171 out.close();
172 }
173 catch ( IOException e )
174 {
175
176 }
177 }
178 }
179
180 getLog().info( "[MAVEN-CORE-IT-LOG] Created output file " + wagonClassLoaderOutput );
181 }
182
183 }