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