View Javadoc

1   package org.apache.maven.plugin.acr;
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.archiver.MavenArchiveConfiguration;
23  import org.apache.maven.archiver.MavenArchiver;
24  import org.apache.maven.artifact.DependencyResolutionRequiredException;
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.project.MavenProject;
29  import org.apache.maven.shared.filtering.MavenFileFilter;
30  import org.apache.maven.shared.filtering.MavenFilteringException;
31  import org.apache.maven.shared.filtering.MavenResourcesExecution;
32  import org.codehaus.plexus.archiver.ArchiverException;
33  import org.codehaus.plexus.archiver.jar.JarArchiver;
34  import org.codehaus.plexus.archiver.jar.ManifestException;
35  import org.codehaus.plexus.util.FileUtils;
36  import org.codehaus.plexus.util.xml.XmlStreamReader;
37  
38  import java.io.File;
39  import java.io.IOException;
40  import java.util.List;
41  
42  /**
43   * Build a JavaEE Application Client jar file from the current project.
44   *
45   * @author <a href="pablo@anahata-it.com">Pablo Rodriguez</a>
46   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
47   * @version $Id:
48   * @goal acr
49   * @requiresDependencyResolution runtime
50   * @threadSafe
51   * @phase package
52   */
53  public class AcrMojo
54      extends AbstractMojo
55  {
56  
57      private static final String APP_CLIENT_XML = "META-INF/application-client.xml";
58  
59      // TODO: will null work instead?
60      private static final String[] DEFAULT_INCLUDES = new String[]{ "**/**" };
61  
62      private static final String[] DEFAULT_EXCLUDES = new String[]{ APP_CLIENT_XML };
63  
64      private static final String[] EMPTY_STRING_ARRAY = new String[0];
65  
66      /**
67       * The directory for the generated jar.
68       *
69       * @parameter default-value="${project.build.directory}"
70       * @required
71       * @readonly
72       */
73      private File basedir;
74  
75      /**
76       * Directory that resources are copied to during the build.
77       *
78       * @parameter default-value="${project.build.outputDirectory}" expression="${outputDirectory}"
79       */
80      private File outputDirectory;
81  
82      /**
83       * The name of the Application client JAR file to generate.
84       *
85       * @parameter default-value="${project.build.finalName}" expression="${jarName}"
86       */
87      private String jarName;
88  
89      /**
90       * The files and directories to exclude from the main Application Client jar. Usage:
91       * <p/>
92       * <pre>
93       * &lt;excludes&gt;
94       *   &lt;exclude&gt;**&#47;*DevOnly.class&lt;&#47;exclude&gt;
95       * &lt;&#47;excludes&gt;
96       * </pre>
97       * <br/>Default exclusions: META-INF&#47;application-client.xml,
98       *
99       * @parameter
100      */
101     private List excludes;
102 
103     /**
104      * The Maven project.
105      *
106      * @parameter default-value="${project}"
107      * @required
108      * @readonly
109      */
110     private MavenProject project;
111 
112     /**
113      * The Jar archiver.
114      *
115      * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
116      */
117     private JarArchiver jarArchiver;
118 
119     /**
120      * The archive configuration to use.
121      * See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
122      *
123      * @parameter
124      */
125     private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
126 
127     /**
128      * To escape interpolated value with windows path.
129      * c:\foo\bar will be replaced with c:\\foo\\bar.
130      *
131      * @parameter default-value="false" expression="${acr.escapeBackslashesInFilePath}"
132      */
133     private boolean escapeBackslashesInFilePath;
134 
135     /**
136      * An expression preceded with this String won't be interpolated.
137      * \${foo} will be replaced with ${foo}.
138      *
139      * @parameter expression="${acr.escapeString}"
140      */
141     protected String escapeString;
142 
143     /**
144      * To filter the deployment descriptor.
145      *
146      * @parameter default-value="false" expression="${acr.filterDeploymentDescriptor}"
147      */
148     private boolean filterDeploymentDescriptor;
149 
150     /**
151      * Filters (properties files) to include during the interpolation of the deployment descriptor.
152      *
153      * @parameter
154      */
155     private List filters;
156 
157     /**
158      * @component role="org.apache.maven.shared.filtering.MavenFileFilter" role-hint="default"
159      * @required
160      */
161     private MavenFileFilter mavenFileFilter;
162 
163     /**
164      * @parameter expression="${session}"
165      * @readonly
166      * @required
167      */
168     private MavenSession session;
169 
170     /**
171      * Generates the application client jar file.
172      *
173      */
174     public void execute()
175         throws MojoExecutionException
176     {
177         //todo: Add license files in META-INF directory
178 
179         if ( getLog().isInfoEnabled() )
180         {
181             getLog().info( "Building JavaEE Application client: " + jarName );
182         }
183 
184         File jarFile = getAppClientJarFile( basedir, jarName );
185 
186         MavenArchiver archiver = new MavenArchiver();
187 
188         archiver.setArchiver( jarArchiver );
189 
190         archiver.setOutputFile( jarFile );
191 
192         File deploymentDescriptor = new File( outputDirectory, APP_CLIENT_XML );
193         try
194         {
195             String[] mainJarExcludes = DEFAULT_EXCLUDES;
196 
197             if ( excludes != null && !excludes.isEmpty() )
198             {
199                 excludes.add( APP_CLIENT_XML );
200                 mainJarExcludes = (String[]) excludes.toArray( EMPTY_STRING_ARRAY );
201             }
202 
203             if ( !outputDirectory.exists() )
204             {
205                 getLog().info(
206                     "JAR will only contain the META-INF/application-client.xml as no content was marked for inclusion" );
207             }
208             else
209             {
210                 archiver.getArchiver().addDirectory( outputDirectory, DEFAULT_INCLUDES, mainJarExcludes );
211             }
212 
213             if ( deploymentDescriptor.exists() )
214             {
215                 if ( filterDeploymentDescriptor )
216                 {
217                     getLog().debug( "Filtering deployment descriptor." );
218                     MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution();
219                     mavenResourcesExecution.setEscapeString( escapeString );
220                     List filterWrappers =
221                         mavenFileFilter.getDefaultFilterWrappers( project, filters, escapeBackslashesInFilePath,
222                                                                   this.session, mavenResourcesExecution );
223 
224                     // Create a temporary file that we can copy-and-filter
225                     File unfilteredDeploymentDescriptor = new File( outputDirectory, APP_CLIENT_XML + ".unfiltered" );
226                     FileUtils.copyFile( deploymentDescriptor, unfilteredDeploymentDescriptor );
227                     mavenFileFilter.copyFile( unfilteredDeploymentDescriptor, deploymentDescriptor, true,
228                                               filterWrappers, getEncoding( unfilteredDeploymentDescriptor ) );
229                     // Remove the temporary file
230                     FileUtils.forceDelete( unfilteredDeploymentDescriptor );
231                 }
232                 archiver.getArchiver().addFile( deploymentDescriptor, APP_CLIENT_XML );
233             }
234 
235             // create archive
236             archiver.createArchive( project, archive );
237         }
238         catch ( ArchiverException e )
239         {
240             throw new MojoExecutionException(
241                 "There was a problem creating the JavaEE Application Client  archive: " + e.getMessage(), e );
242         }
243         catch ( ManifestException e )
244         {
245             throw new MojoExecutionException(
246                 "There was a problem reading / creating the manifest for the JavaEE Application Client  archive: " +
247                     e.getMessage(), e );
248         }
249         catch ( IOException e )
250         {
251             throw new MojoExecutionException(
252                 "There was a I/O problem creating the JavaEE Application Client archive: " + e.getMessage(), e );
253         }
254         catch ( DependencyResolutionRequiredException e )
255         {
256             throw new MojoExecutionException(
257                 "There was a problem resolving dependencies while creating the JavaEE Application Client archive: " +
258                     e.getMessage(), e );
259         }
260         catch ( MavenFilteringException e )
261         {
262             throw new MojoExecutionException(
263                 "There was a problem filtering the deployment descriptor: " + e.getMessage(), e );
264         }
265 
266         project.getArtifact().setFile( jarFile );
267 
268 
269     }
270 
271     /**
272      * Returns the App-client Jar file to generate.
273      *
274      * @param basedir   the output directory
275      * @param finalName the name of the ear file
276      * @return the Application client JAR file to generate
277      */
278     private static File getAppClientJarFile( File basedir, String finalName )
279     {
280         return new File( basedir, finalName + ".jar" );
281     }
282 
283     /**
284      * Get the encoding from an XML-file.
285      *
286      * @param xmlFile the XML-file
287      * @return The encoding of the XML-file, or UTF-8 if it's not specified in the file
288      * @throws IOException if an error occurred while reading the file
289      */
290     private String getEncoding( File xmlFile )
291         throws IOException
292     {
293         XmlStreamReader xmlReader = new XmlStreamReader( xmlFile );
294         return xmlReader.getEncoding();
295     }
296 }