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