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.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   * @author Jason van Zyl
43   * @plexus.component
44   */
45  public class DefaultArchetypeManager
46      extends AbstractLogEnabled
47      implements ArchetypeManager
48  {
49      /**
50       * @plexus.requirement role-hint="fileset"
51       */
52      private ArchetypeCreator creator;
53  
54      /**
55       * @plexus.requirement
56       */
57      private ArchetypeGenerator generator;
58  
59      /**
60       * @plexus.requirement role="org.apache.maven.archetype.source.ArchetypeDataSource"
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             // add an empty directory
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 }