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