1 package org.apache.maven.plugins.acr;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54
55
56
57
58 @Mojo( name = "acr", requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true, defaultPhase = LifecyclePhase.PACKAGE )
59
60 public class AcrMojo
61 extends AbstractMojo
62 {
63
64 private static final String APP_CLIENT_XML = "META-INF/application-client.xml";
65
66
67 private static final String[] DEFAULT_INCLUDES = { "**/**" };
68
69 private static final String[] DEFAULT_EXCLUDES = { APP_CLIENT_XML };
70
71
72
73
74 @Parameter( defaultValue = "${project.build.directory}", required = true, readonly = true )
75 private File basedir;
76
77
78
79
80
81
82 @Parameter( property = "maven.acr.outputDirectory", defaultValue = "${project.build.outputDirectory}" )
83 private File outputDirectory;
84
85
86
87
88 @Parameter( defaultValue = "${project.build.finalName}" )
89 private String jarName;
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 @Parameter
105 private List<String> excludes;
106
107
108
109
110 @Parameter( defaultValue = "${project}", readonly = true, required = true )
111 private MavenProject project;
112
113
114
115
116 @Component( role = Archiver.class, hint = "jar" )
117 private JarArchiver jarArchiver;
118
119
120
121
122
123 @Parameter
124 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
125
126
127
128
129
130
131 @Parameter( property = "maven.acr.escapeBackslashesInFilePath", defaultValue = "false" )
132 private boolean escapeBackslashesInFilePath;
133
134
135
136
137
138
139 @Parameter( property = "maven.acr.escapeString" )
140 private String escapeString;
141
142
143
144
145
146 @Parameter( property = "maven.acr.filterDeploymentDescriptor", defaultValue = "false" )
147 private boolean filterDeploymentDescriptor;
148
149
150
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
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
200 getLog().info( "JAR will only contain the META-INF/application-client.xml as no content was marked for inclusion" );
201
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
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
223 FileUtils.forceDelete( unfilteredDeploymentDescriptor );
224 }
225 archiver.getArchiver().addFile( deploymentDescriptor, APP_CLIENT_XML );
226 }
227
228
229 archiver.createArchive( session, project, archive );
230
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
261 }
262
263
264
265
266
267
268
269
270 private static File getAppClientJarFile( File basedir, String finalName )
271 {
272 return new File( basedir, finalName + ".jar" );
273 }
274
275
276
277
278
279
280
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 }