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          try ( ZipOutputStream zos = new ZipOutputStream( new FileOutputStream( archive ) ) )
100         {
101             zos.setLevel( 9 );
102 
103             zipper( zos, sourceDirectory.getAbsolutePath().length(), sourceDirectory );
104         }
105     }
106 
107     private void zipper( ZipOutputStream zos, int offset, File currentSourceDirectory )
108         throws IOException
109     {
110         File[] files = currentSourceDirectory.listFiles();
111 
112         if ( files.length == 0 )
113         {
114             // add an empty directory
115             String dirName = currentSourceDirectory.getAbsolutePath().substring( offset + 1 );
116 
117             if ( File.separatorChar != '/' )
118             {
119                 dirName = dirName.replace( '\\', '/' );
120             }
121 
122             zos.putNextEntry( new ZipEntry( dirName + '/' ) );
123         }
124 
125         for ( int i = 0; i < files.length; i++ )
126         {
127             if ( files[i].isDirectory() )
128             {
129                 zipper( zos, offset, files[i] );
130             }
131             else
132             {
133                 String fileName = files[i].getAbsolutePath().substring( offset + 1 );
134 
135                 if ( File.separatorChar != '/' )
136                 {
137                     fileName = fileName.replace( '\\', '/' );
138                 }
139 
140                 ZipEntry e = new ZipEntry( fileName );
141 
142                 zos.putNextEntry( e );
143 
144                 try ( FileInputStream is = new FileInputStream( files[i] ) )
145                 {
146                     IOUtil.copy( is, zos );
147                 }
148 
149                 zos.flush();
150 
151                 zos.closeEntry();
152             }
153         }
154     }
155 
156     @Override
157     public ArchetypeCatalog getInternalCatalog()
158     {
159         try
160         {
161             ArchetypeDataSource source = archetypeSources.get( "internal-catalog" );
162 
163             return source.getArchetypeCatalog( null );
164         }
165         catch ( ArchetypeDataSourceException e )
166         {
167             return new ArchetypeCatalog();
168         }
169     }
170 
171     @Override
172     public ArchetypeCatalog getLocalCatalog( ProjectBuildingRequest buildingRequest )
173     {
174         try
175         {
176             ArchetypeDataSource source = archetypeSources.get( "catalog" );
177 
178             return source.getArchetypeCatalog( buildingRequest );
179         }
180         catch ( ArchetypeDataSourceException e )
181         {
182             return new ArchetypeCatalog();
183         }
184     }
185 
186     @Override
187     public ArchetypeCatalog getRemoteCatalog( ProjectBuildingRequest buildingRequest )
188     {
189         try
190         {
191             ArchetypeDataSource source = archetypeSources.get( "remote-catalog" );
192 
193             return source.getArchetypeCatalog( buildingRequest );
194         }
195         catch ( ArchetypeDataSourceException e )
196         {
197             return new ArchetypeCatalog();
198         }
199     }
200 
201     @Override
202     public void updateLocalCatalog( ProjectBuildingRequest buildingRequest, Archetype archetype )
203     {
204         try
205         {
206             ArchetypeDataSource source = archetypeSources.get( "catalog" );
207 
208             source.updateCatalog( buildingRequest, archetype );
209         }
210         catch ( ArchetypeDataSourceException e )
211         {
212         }
213     }
214 }