View Javadoc

1   package org.apache.maven.plugin.rar;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.maven.plugin.AbstractMojo;
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.archiver.MavenArchiver;
22  import org.apache.maven.archiver.MavenArchiveConfiguration;
23  import org.apache.maven.project.MavenProject;
24  import org.apache.maven.artifact.Artifact;
25  import org.codehaus.plexus.archiver.jar.JarArchiver;
26  import org.codehaus.plexus.util.FileUtils;
27  import org.codehaus.plexus.util.DirectoryScanner;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.util.Iterator;
32  import java.util.Set;
33  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
34  
35  /**
36   * Builds J2EE Resource Adapter Archive (RAR) files.
37   *
38   * @author <a href="stephane.nicoll@gmail.com">Stephane Nicoll</a>
39   * @version $Id: RarMojo.java 409347 2006-05-25 09:40:17Z aramirez $
40   * @goal rar
41   * @phase package
42   * @requiresDependencyResolution test
43   */
44  public class RarMojo
45      extends AbstractMojo
46  {
47      public static final String RA_XML_URI = "META-INF/ra.xml";
48  
49      private static final String[] DEFAULT_INCLUDES = {"**/**"};
50  
51      /**
52       * Single directory for extra files to include in the RAR.
53       *
54       * @parameter expression="${basedir}/src/main/rar"
55       * @required
56       */
57      private File rarSourceDirectory;
58  
59      /**
60       * The location of the ra.xml file to be used within the rar file.
61       *
62       * @parameter expression="${basedir}/src/main/rar/META-INF/ra.xml"
63       */
64      private File raXmlFile;
65  
66      /**
67       * Specify if the generated jar file of this project should be
68       * included in the rar file ; default is true.
69       *
70       * @parameter
71       */
72      private Boolean includeJar = Boolean.TRUE;
73  
74      /**
75       * The location of the manifest file to be used within the rar file.
76       *
77       * @parameter expression="${basedir}/src/main/rar/META-INF/MANIFEST.MF"
78       */
79      private File manifestFile;
80  
81      /**
82       * Directory that resources are copied to during the build.
83       *
84       * @parameter expression="${project.build.directory}/${project.build.finalName}"
85       * @required
86       */
87      private String workDirectory;
88  
89      /**
90       * The directory for the generated RAR.
91       *
92       * @parameter expression="${project.build.directory}"
93       * @required
94       */
95      private String outputDirectory;
96  
97      /**
98       * The name of the RAR file to generate.
99       *
100      * @parameter alias="rarName" expression="${project.build.finalName}"
101      * @required
102      */
103     private String finalName;
104 
105     /**
106      * The maven project.
107      *
108      * @parameter expression="${project}"
109      * @required
110      * @readonly
111      * @description "the maven project to use"
112      */
113     private MavenProject project;
114 
115     /**
116      * The Jar archiver.
117      *
118      * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#jar}"
119      * @required
120      */
121     private JarArchiver jarArchiver;
122 
123     /**
124      * The maven archiver to use.
125      *
126      * @parameter
127      */
128     private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
129 
130 
131     private File buildDir;
132 
133 
134     public void execute()
135         throws MojoExecutionException
136     {
137         getLog().debug( " ======= RarMojo settings =======" );
138         getLog().debug( "rarSourceDirectory[" + rarSourceDirectory + "]" );
139         getLog().debug( "manifestFile[" + manifestFile + "]" );
140         getLog().debug( "raXmlFile[" + raXmlFile + "]" );
141         getLog().debug( "workDirectory[" + workDirectory + "]" );
142         getLog().debug( "outputDirectory[" + outputDirectory + "]" );
143         getLog().debug( "finalName[" + finalName + "]" );
144 
145         // Check if jar file is there and if requested, copy it
146         try
147         {
148             if (includeJar.booleanValue()) {
149                 File generatedJarFile = new File( outputDirectory, finalName + ".jar" );
150                 if (generatedJarFile.exists()) {
151                     getLog().info( "Including generated jar file["+generatedJarFile.getName()+"]");
152                     FileUtils.copyFileToDirectory( generatedJarFile, getBuildDir());
153                 }
154             }
155         }
156         catch ( IOException e )
157         {
158             throw new MojoExecutionException( "Error copying generated Jar file", e );
159         }
160 
161         // Copy dependencies
162         try
163         {
164             Set artifacts = project.getArtifacts();
165             for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
166             {
167                 Artifact artifact = (Artifact) iter.next();
168                 
169                 ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
170                 if ( !artifact.isOptional() && filter.include( artifact ) )
171                 {
172                     getLog().info("Copying artifact[" + artifact.getGroupId() + ", " + artifact.getId() + ", " +
173                         artifact.getScope() + "]");
174                     FileUtils.copyFileToDirectory( artifact.getFile(), getBuildDir() );
175                 }
176             }
177         }
178         catch ( IOException e )
179         {
180             throw new MojoExecutionException( "Error copying RAR dependencies", e );
181         }
182 
183         // Copy source files
184         try
185         {
186             File rarSourceDir =  rarSourceDirectory;
187             if ( rarSourceDir.exists() )
188             {
189                 getLog().info( "Copy rar resources to " + getBuildDir().getAbsolutePath() );
190 
191                 DirectoryScanner scanner = new DirectoryScanner();
192                 scanner.setBasedir( rarSourceDir.getAbsolutePath() );
193                 scanner.setIncludes( DEFAULT_INCLUDES );
194                 scanner.addDefaultExcludes();
195                 scanner.scan();
196 
197                 String[] dirs = scanner.getIncludedDirectories();
198 
199                 for ( int j = 0; j < dirs.length; j++ )
200                 {
201                     new File( getBuildDir(), dirs[j] ).mkdirs();
202                 }
203 
204                 String[] files = scanner.getIncludedFiles();
205 
206                 for ( int j = 0; j < files.length; j++ )
207                 {
208                     File targetFile = new File( getBuildDir(), files[j] );
209 
210                     targetFile.getParentFile().mkdirs();
211 
212                     File file = new File( rarSourceDir, files[j] );
213                     FileUtils.copyFileToDirectory( file, targetFile.getParentFile() );
214                 }
215             }
216         }
217         catch ( Exception e )
218         {
219             throw new MojoExecutionException( "Error copying RAR resources", e );
220         }
221 
222         // Include custom manifest if necessary
223         try
224         {
225             includeCustomRaXmlFile();
226         }
227         catch ( IOException e )
228         {
229             throw new MojoExecutionException( "Error copying ra.xml file", e );
230         }
231 
232         // Check if connector deployment descriptor is there
233         File ddFile = new File( getBuildDir(), RA_XML_URI );
234         if ( !ddFile.exists() )
235         {
236             getLog().warn(
237                 "Connector deployment descriptor: " + ddFile.getAbsolutePath() + " does not exist." );
238         }
239 
240         try
241         {
242             File rarFile = new File( outputDirectory, finalName + ".rar" );
243             MavenArchiver archiver = new MavenArchiver();
244             archiver.setArchiver( jarArchiver );
245             archiver.setOutputFile( rarFile );
246 
247             // Include custom manifest if necessary
248             includeCustomManifestFile();
249 
250             archiver.getArchiver().addDirectory( getBuildDir() );
251             archiver.createArchive( project, archive );
252 	    
253             project.getArtifact().setFile( rarFile );
254         }
255         catch ( Exception e )
256         {
257             throw new MojoExecutionException( "Error assembling RAR", e );
258         }
259     }
260 
261     protected File getBuildDir()
262     {
263         if ( buildDir == null )
264         {
265             buildDir = new File( workDirectory );
266         }
267         return buildDir;
268     }
269 
270     private void includeCustomManifestFile()
271         throws IOException
272     {
273         File customManifestFile = manifestFile;
274         if ( !customManifestFile.exists() )
275         {
276             getLog().info( "Could not find manifest file: " + manifestFile +" - Generating one");
277         }
278         else
279         {
280             getLog().info( "Including custom manifest file[" + customManifestFile + "]" );
281             archive.setManifestFile( customManifestFile );
282             File metaInfDir = new File(getBuildDir(), "META-INF");
283             FileUtils.copyFileToDirectory( customManifestFile, metaInfDir );
284         }
285     }
286 
287     private void includeCustomRaXmlFile()
288         throws IOException
289     {
290         if (raXmlFile == null) {
291 
292         }
293         File raXml = raXmlFile;
294         if (raXml.exists()) {
295             getLog().info( "Using ra.xml "+ raXmlFile);
296             File metaInfDir = new File(getBuildDir(), "META-INF");
297             FileUtils.copyFileToDirectory( raXml, metaInfDir);
298         }
299     }
300 }