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