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