1 | |
package org.apache.maven.archetype; |
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.archetype.catalog.Archetype; |
23 | |
import org.apache.maven.archetype.catalog.ArchetypeCatalog; |
24 | |
import org.apache.maven.archetype.creator.ArchetypeCreator; |
25 | |
import org.apache.maven.archetype.generator.ArchetypeGenerator; |
26 | |
import org.apache.maven.archetype.source.ArchetypeDataSource; |
27 | |
import org.apache.maven.archetype.source.ArchetypeDataSourceException; |
28 | |
import org.apache.maven.artifact.DependencyResolutionRequiredException; |
29 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
30 | |
import org.codehaus.plexus.util.IOUtil; |
31 | |
|
32 | |
import java.io.File; |
33 | |
import java.io.FileInputStream; |
34 | |
import java.io.FileOutputStream; |
35 | |
import java.io.IOException; |
36 | |
import java.util.Map; |
37 | |
import java.util.Properties; |
38 | |
import java.util.zip.ZipEntry; |
39 | |
import java.util.zip.ZipOutputStream; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 6 | public class DefaultArchetypeManager |
46 | |
extends AbstractLogEnabled |
47 | |
implements ArchetypeManager |
48 | |
{ |
49 | |
|
50 | |
|
51 | |
|
52 | |
private ArchetypeCreator creator; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
private ArchetypeGenerator generator; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private Map<String, ArchetypeDataSource> archetypeSources; |
63 | |
|
64 | |
public ArchetypeCreationResult createArchetypeFromProject( ArchetypeCreationRequest request ) |
65 | |
{ |
66 | 3 | ArchetypeCreationResult result = new ArchetypeCreationResult(); |
67 | |
|
68 | 3 | creator.createArchetype( request, result ); |
69 | |
|
70 | 3 | return result; |
71 | |
} |
72 | |
|
73 | |
public ArchetypeGenerationResult generateProjectFromArchetype( ArchetypeGenerationRequest request ) |
74 | |
{ |
75 | 4 | ArchetypeGenerationResult result = new ArchetypeGenerationResult(); |
76 | |
|
77 | 4 | generator.generateArchetype( request, result ); |
78 | |
|
79 | 4 | return result; |
80 | |
} |
81 | |
|
82 | |
public File archiveArchetype( File archetypeDirectory, File outputDirectory, String finalName ) |
83 | |
throws DependencyResolutionRequiredException, IOException |
84 | |
{ |
85 | 3 | File jarFile = new File( outputDirectory, finalName + ".jar" ); |
86 | |
|
87 | 3 | zip( archetypeDirectory, jarFile ); |
88 | |
|
89 | 3 | return jarFile; |
90 | |
} |
91 | |
|
92 | |
public void zip( File sourceDirectory, File archive ) |
93 | |
throws IOException |
94 | |
{ |
95 | 3 | if ( !archive.getParentFile().exists() ) |
96 | |
{ |
97 | 0 | archive.getParentFile().mkdirs(); |
98 | |
} |
99 | |
|
100 | 3 | ZipOutputStream zos = null; |
101 | |
try |
102 | |
{ |
103 | 3 | zos = new ZipOutputStream( new FileOutputStream( archive ) ); |
104 | |
|
105 | 3 | zos.setLevel( 9 ); |
106 | |
|
107 | 3 | zipper( zos, sourceDirectory.getAbsolutePath().length(), sourceDirectory ); |
108 | |
} |
109 | |
finally |
110 | |
{ |
111 | 3 | IOUtil.close( zos ); |
112 | 3 | } |
113 | |
|
114 | 3 | } |
115 | |
|
116 | |
private void zipper( ZipOutputStream zos, int offset, File currentSourceDirectory ) |
117 | |
throws IOException |
118 | |
{ |
119 | 60 | File[] files = currentSourceDirectory.listFiles(); |
120 | |
|
121 | 60 | if ( files.length == 0 ) |
122 | |
{ |
123 | |
|
124 | 0 | String dirName = currentSourceDirectory.getAbsolutePath().substring( offset + 1 ); |
125 | |
|
126 | 0 | if ( File.separatorChar != '/' ) |
127 | |
{ |
128 | 0 | dirName = dirName.replace( '\\', '/' ); |
129 | |
} |
130 | |
|
131 | 0 | zos.putNextEntry( new ZipEntry( dirName + '/' ) ); |
132 | |
} |
133 | |
|
134 | 155 | for ( int i = 0; i < files.length; i++ ) |
135 | |
{ |
136 | 95 | if ( files[i].isDirectory() ) |
137 | |
{ |
138 | 57 | zipper( zos, offset, files[i] ); |
139 | |
} |
140 | |
else |
141 | |
{ |
142 | 38 | String fileName = files[i].getAbsolutePath().substring( offset + 1 ); |
143 | |
|
144 | 38 | if ( File.separatorChar != '/' ) |
145 | |
{ |
146 | 0 | fileName = fileName.replace( '\\', '/' ); |
147 | |
} |
148 | |
|
149 | 38 | ZipEntry e = new ZipEntry( fileName ); |
150 | |
|
151 | 38 | zos.putNextEntry( e ); |
152 | |
|
153 | 38 | FileInputStream is = null; |
154 | |
try |
155 | |
{ |
156 | 38 | is = new FileInputStream( files[i] ); |
157 | |
|
158 | 38 | IOUtil.copy( is, zos ); |
159 | |
} |
160 | |
finally |
161 | |
{ |
162 | 38 | IOUtil.close( is ); |
163 | 38 | } |
164 | |
|
165 | 38 | zos.flush(); |
166 | |
|
167 | 38 | zos.closeEntry(); |
168 | |
} |
169 | |
} |
170 | 60 | } |
171 | |
|
172 | |
public ArchetypeCatalog getInternalCatalog() |
173 | |
{ |
174 | |
try |
175 | |
{ |
176 | 0 | ArchetypeDataSource source = archetypeSources.get( "internal-catalog" ); |
177 | |
|
178 | 0 | return source.getArchetypeCatalog( new Properties() ); |
179 | |
} |
180 | 0 | catch ( ArchetypeDataSourceException e ) |
181 | |
{ |
182 | 0 | return new ArchetypeCatalog(); |
183 | |
} |
184 | |
} |
185 | |
|
186 | |
public ArchetypeCatalog getDefaultLocalCatalog() |
187 | |
{ |
188 | 0 | return getLocalCatalog( "${user.home}/.m2/archetype-catalog.xml" ); |
189 | |
} |
190 | |
|
191 | |
public ArchetypeCatalog getLocalCatalog( String path ) |
192 | |
{ |
193 | |
try |
194 | |
{ |
195 | 2 | Properties properties = new Properties(); |
196 | 2 | properties.setProperty( "file", path ); |
197 | 2 | ArchetypeDataSource source = archetypeSources.get( "catalog" ); |
198 | |
|
199 | 2 | return source.getArchetypeCatalog( properties ); |
200 | |
} |
201 | 0 | catch ( ArchetypeDataSourceException e ) |
202 | |
{ |
203 | 0 | return new ArchetypeCatalog(); |
204 | |
} |
205 | |
} |
206 | |
|
207 | |
public ArchetypeCatalog getRemoteCatalog() |
208 | |
{ |
209 | 0 | return getRemoteCatalog( "http://repo1.maven.org/maven2" ); |
210 | |
} |
211 | |
|
212 | |
public ArchetypeCatalog getRemoteCatalog( String url ) |
213 | |
{ |
214 | |
try |
215 | |
{ |
216 | 1 | Properties properties = new Properties(); |
217 | 1 | properties.setProperty( "repository", url ); |
218 | 1 | ArchetypeDataSource source = archetypeSources.get( "remote-catalog" ); |
219 | |
|
220 | 1 | return source.getArchetypeCatalog( properties ); |
221 | |
} |
222 | 0 | catch ( ArchetypeDataSourceException e ) |
223 | |
{ |
224 | 0 | return new ArchetypeCatalog(); |
225 | |
} |
226 | |
} |
227 | |
|
228 | |
public void updateLocalCatalog( Archetype archetype ) |
229 | |
{ |
230 | 0 | updateLocalCatalog( archetype, "${user.home}/.m2/archetype-catalog.xml" ); |
231 | 0 | } |
232 | |
|
233 | |
public void updateLocalCatalog( Archetype archetype, String path ) |
234 | |
{ |
235 | |
try |
236 | |
{ |
237 | 0 | Properties properties = new Properties(); |
238 | 0 | properties.setProperty( "file", path ); |
239 | 0 | ArchetypeDataSource source = archetypeSources.get( "catalog" ); |
240 | |
|
241 | 0 | source.updateCatalog( properties, archetype ); |
242 | |
} |
243 | 0 | catch ( ArchetypeDataSourceException e ) |
244 | |
{ |
245 | 0 | } |
246 | 0 | } |
247 | |
} |