View Javadoc
1   package org.apache.maven.archetype;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.component.annotations.Component;
30  import org.codehaus.plexus.component.annotations.Requirement;
31  import org.codehaus.plexus.logging.AbstractLogEnabled;
32  import org.codehaus.plexus.util.IOUtil;
33  
34  import java.io.File;
35  import java.io.FileInputStream;
36  import java.io.FileOutputStream;
37  import java.io.IOException;
38  import java.util.Map;
39  import java.util.Properties;
40  import java.util.zip.ZipEntry;
41  import java.util.zip.ZipOutputStream;
42  
43  /**
44   * @author Jason van Zyl
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             // add an empty directory
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( new Properties() );
175         }
176         catch ( ArchetypeDataSourceException e )
177         {
178             return new ArchetypeCatalog();
179         }
180     }
181 
182     public ArchetypeCatalog getDefaultLocalCatalog()
183     {
184         return getLocalCatalog( "${user.home}/.m2/archetype-catalog.xml" );
185     }
186 
187     public ArchetypeCatalog getLocalCatalog( String path )
188     {
189         try
190         {
191             Properties properties = new Properties();
192             properties.setProperty( "file", path );
193             ArchetypeDataSource source = archetypeSources.get( "catalog" );
194 
195             return source.getArchetypeCatalog( properties );
196         }
197         catch ( ArchetypeDataSourceException e )
198         {
199             return new ArchetypeCatalog();
200         }
201     }
202 
203     public ArchetypeCatalog getRemoteCatalog()
204     {
205         return getRemoteCatalog( "http://repo.maven.apache.org/maven2" );
206     }
207 
208     public ArchetypeCatalog getRemoteCatalog( String url )
209     {
210         try
211         {
212             Properties properties = new Properties();
213             properties.setProperty( "repository", url );
214             ArchetypeDataSource source = archetypeSources.get( "remote-catalog" );
215 
216             return source.getArchetypeCatalog( properties );
217         }
218         catch ( ArchetypeDataSourceException e )
219         {
220             return new ArchetypeCatalog();
221         }
222     }
223 
224     public void updateLocalCatalog( Archetype archetype )
225     {
226         updateLocalCatalog( archetype, "${user.home}/.m2/archetype-catalog.xml" );
227     }
228 
229     public void updateLocalCatalog( Archetype archetype, String path )
230     {
231         try
232         {
233             Properties properties = new Properties();
234             properties.setProperty( "file", path );
235             ArchetypeDataSource source = archetypeSources.get( "catalog" );
236 
237             source.updateCatalog( properties, archetype );
238         }
239         catch ( ArchetypeDataSourceException e )
240         {
241         }
242     }
243 }