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.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   * @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      @Override
61      public ArchetypeCreationResult createArchetypeFromProject( ArchetypeCreationRequest request )
62      {
63          ArchetypeCreationResult result = new ArchetypeCreationResult();
64  
65          creator.createArchetype( request, result );
66  
67          return result;
68      }
69  
70      @Override
71      public ArchetypeGenerationResult generateProjectFromArchetype( ArchetypeGenerationRequest request )
72      {
73          ArchetypeGenerationResult result = new ArchetypeGenerationResult();
74  
75          generator.generateArchetype( request, result );
76  
77          return result;
78      }
79  
80      @Override
81      public File archiveArchetype( File archetypeDirectory, File outputDirectory, String finalName )
82          throws DependencyResolutionRequiredException, IOException
83      {
84          File jarFile = new File( outputDirectory, finalName + ".jar" );
85  
86          zip( archetypeDirectory, jarFile );
87  
88          return jarFile;
89      }
90  
91      public void zip( File sourceDirectory, File archive )
92          throws IOException
93      {
94          if ( !archive.getParentFile().exists() )
95          {
96              archive.getParentFile().mkdirs();
97          }
98  
99  
100         if ( !archive.exists() && !archive.createNewFile() )
101         {
102             getLogger().warn( "Could not create new file \"" + archive.getPath() + "\" or the file already exists." );
103         }
104 
105         try ( ZipOutputStream zos = new ZipOutputStream( new FileOutputStream( archive ) ) )
106         {
107             zos.setLevel( 9 );
108 
109             zipper( zos, sourceDirectory.getAbsolutePath().length(), sourceDirectory );
110         }
111     }
112 
113     private void zipper( ZipOutputStream zos, int offset, File currentSourceDirectory )
114         throws IOException
115     {
116         File[] files = currentSourceDirectory.listFiles();
117 
118         if ( files.length == 0 )
119         {
120             // add an empty directory
121             String dirName = currentSourceDirectory.getAbsolutePath().substring( offset + 1 );
122 
123             if ( File.separatorChar != '/' )
124             {
125                 dirName = dirName.replace( '\\', '/' );
126             }
127 
128             zos.putNextEntry( new ZipEntry( dirName + '/' ) );
129         }
130 
131         for ( int i = 0; i < files.length; i++ )
132         {
133             if ( files[i].isDirectory() )
134             {
135                 zipper( zos, offset, files[i] );
136             }
137             else
138             {
139                 String fileName = files[i].getAbsolutePath().substring( offset + 1 );
140 
141                 if ( File.separatorChar != '/' )
142                 {
143                     fileName = fileName.replace( '\\', '/' );
144                 }
145 
146                 ZipEntry e = new ZipEntry( fileName );
147 
148                 zos.putNextEntry( e );
149 
150                 try ( FileInputStream is = new FileInputStream( files[i] ) )
151                 {
152                     IOUtil.copy( is, zos );
153                 }
154 
155                 zos.closeEntry();
156             }
157         }
158     }
159 
160     @Override
161     public ArchetypeCatalog getInternalCatalog()
162     {
163         try
164         {
165             ArchetypeDataSource source = archetypeSources.get( "internal-catalog" );
166 
167             return source.getArchetypeCatalog( null );
168         }
169         catch ( ArchetypeDataSourceException e )
170         {
171             return new ArchetypeCatalog();
172         }
173     }
174 
175     @Override
176     public ArchetypeCatalog getLocalCatalog( ProjectBuildingRequest buildingRequest )
177     {
178         try
179         {
180             ArchetypeDataSource source = archetypeSources.get( "catalog" );
181 
182             return source.getArchetypeCatalog( buildingRequest );
183         }
184         catch ( ArchetypeDataSourceException e )
185         {
186             return new ArchetypeCatalog();
187         }
188     }
189 
190     @Override
191     public ArchetypeCatalog getRemoteCatalog( ProjectBuildingRequest buildingRequest )
192     {
193         try
194         {
195             ArchetypeDataSource source = archetypeSources.get( "remote-catalog" );
196 
197             return source.getArchetypeCatalog( buildingRequest );
198         }
199         catch ( ArchetypeDataSourceException e )
200         {
201             return new ArchetypeCatalog();
202         }
203     }
204 
205     @Override
206     public void updateLocalCatalog( ProjectBuildingRequest buildingRequest, Archetype archetype )
207     {
208         try
209         {
210             ArchetypeDataSource source = archetypeSources.get( "catalog" );
211 
212             source.updateCatalog( buildingRequest, archetype );
213         }
214         catch ( ArchetypeDataSourceException e )
215         {
216         }
217     }
218 }