View Javadoc
1   package org.apache.maven.index.examples;
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 java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.security.NoSuchAlgorithmException;
27  import java.util.zip.ZipEntry;
28  import java.util.zip.ZipOutputStream;
29  
30  import org.apache.maven.model.Model;
31  import org.apache.maven.model.io.DefaultModelWriter;
32  import org.apache.maven.model.io.ModelWriter;
33  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34  
35  /**
36   * @author mtodorov
37   */
38  public class SimpleArtifactGenerator
39  {
40  
41  
42      public SimpleArtifactGenerator()
43      {
44          // no op
45      }
46  
47      public File generateArtifact( String repositoryBasedir,
48                                    String groupId,
49                                    String artifactId,
50                                    String version,
51                                    String classifier,
52                                    String extension )
53              throws IOException, NoSuchAlgorithmException, XmlPullParserException
54      {
55          File repositoryDir = new File( repositoryBasedir );
56          File artifactFile = new File( repositoryDir,
57                                        groupId.replaceAll( "\\.", File.separator ) + File.separatorChar +
58                                        artifactId + File.separatorChar + version + File.separatorChar +
59                                        artifactId + "-" + version +
60                                        ( classifier != null ? "-" + classifier + File.separatorChar : "" ) + "." +
61                                        extension );
62  
63          if ( !artifactFile.getParentFile().exists() )
64          {
65              //noinspection ResultOfMethodCallIgnored
66              artifactFile.getParentFile().mkdirs();
67          }
68  
69          createArchive( artifactFile, groupId, artifactId, version, extension );
70  
71          return artifactFile;
72      }
73  
74      private void createArchive( File artifactFile,
75                                  String groupId,
76                                  String artifactId,
77                                  String version,
78                                  String extension )
79              throws NoSuchAlgorithmException,
80                     IOException, XmlPullParserException
81      {
82          ZipOutputStream zos = null;
83  
84          try
85          {
86              // Make sure the artifact's parent directory exists before writing the model.
87              //noinspection ResultOfMethodCallIgnored
88              artifactFile.getParentFile().mkdirs();
89  
90              File pomFile = new File( artifactFile.getParent(),
91                                       artifactFile.getName().substring( 0, artifactFile.getName().lastIndexOf( "." ) ) +
92                                       ".pom" );
93  
94              zos = new ZipOutputStream( new FileOutputStream( artifactFile ) );
95  
96              generatePom( pomFile, groupId, artifactId, version, extension );
97  
98              addMavenPomFile( zos, pomFile, groupId, artifactId );
99          }
100         finally
101         {
102             if ( zos != null )
103             {
104                 zos.close();
105             }
106         }
107     }
108 
109     protected void generatePom( File pomFile,
110                                 String groupId,
111                                 String artifactId,
112                                 String version,
113                                 String type )
114             throws IOException,
115                    XmlPullParserException,
116                    NoSuchAlgorithmException
117     {
118 
119         // Make sure the artifact's parent directory exists before writing the model.
120         //noinspection ResultOfMethodCallIgnored
121         pomFile.getParentFile().mkdirs();
122 
123         Model model = new Model();
124         model.setGroupId( groupId );
125         model.setArtifactId( artifactId );
126         model.setVersion( version );
127         model.setPackaging( type ); // This is not exactly correct.
128 
129         ModelWriter writer = new DefaultModelWriter();
130         writer.write( pomFile, null, model );
131 
132     }
133 
134 
135     private void addMavenPomFile( ZipOutputStream zos,
136                                   File pomFile,
137                                   String groupId,
138                                   String artifactId )
139             throws IOException
140     {
141         ZipEntry ze = new ZipEntry( "META-INF/maven/" + groupId + "/" + artifactId + "/" + "pom.xml" );
142         zos.putNextEntry( ze );
143 
144         FileInputStream fis = new FileInputStream( pomFile );
145 
146         byte[] buffer = new byte[ 1024 ];
147         int len;
148         while ( ( len = fis.read( buffer ) ) > 0 )
149         {
150             zos.write( buffer, 0, len );
151         }
152 
153         fis.close();
154         zos.closeEntry();
155     }
156 
157     public static String convertGAVToPath( String groupId,
158                                            String artifactId,
159                                            String version,
160                                            String classifier,
161                                            String extension )
162     {
163         String path = "";
164 
165         path += groupId.replaceAll( "\\.", "/" ) + "/";
166         path += artifactId + "/";
167         path += version + "/";
168         path += artifactId + "-";
169         path += version;
170         path += classifier != null ? "-" + classifier : "";
171         path += "." + extension;
172 
173         return path;
174     }
175 
176 }