View Javadoc

1   /**
2    * The MIT License
3    *
4    * Copyright 2006-2012 The Codehaus.
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package org.codehaus.mojo.appassembler;
25  
26  import java.io.File;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Iterator;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Set;
33  
34  import org.apache.maven.artifact.Artifact;
35  import org.apache.maven.artifact.installer.ArtifactInstallationException;
36  import org.apache.maven.artifact.installer.ArtifactInstaller;
37  import org.apache.maven.artifact.repository.ArtifactRepository;
38  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
39  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
40  import org.apache.maven.plugin.MojoExecutionException;
41  import org.apache.maven.plugin.MojoFailureException;
42  import org.apache.maven.project.MavenProject;
43  import org.codehaus.mojo.appassembler.daemon.DaemonGenerationRequest;
44  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorException;
45  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorService;
46  import org.codehaus.plexus.util.StringUtils;
47  
48  /**
49   * Generates JSW based daemon wrappers.
50   * 
51   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
52   * @version $Id: GenerateDaemonsMojo.java 16448 2012-04-22 15:47:18Z khmarbaise $
53   * @goal generate-daemons
54   * @requiresDependencyResolution runtime
55   * @phase package
56   * @threadsafe
57   */
58  public class GenerateDaemonsMojo
59          extends AbstractAppAssemblerMojo
60  {
61      // -----------------------------------------------------------------------
62      // Parameters
63      // -----------------------------------------------------------------------
64  
65      /**
66       * Set of {@linkplain Daemon}s to generate.
67       * 
68       * @parameter
69       * @required
70       */
71      private Set daemons;
72  
73      /**
74       * {@linkplain JvmSettings} describing min/max memory and stack size, system properties and extra arguments.
75       * 
76       * @parameter
77       */
78      private JvmSettings defaultJvmSettings;
79  
80      /**
81       * The base directory of the project.
82       * 
83       * @parameter expression="${basedir}"
84       * @required
85       */
86      private File basedir;
87  
88      /**
89       * Target directory for generated daemons.
90       * 
91       * @parameter expression="${project.build.directory}/generated-resources/appassembler"
92       * @required
93       */
94      private File target;
95  
96      /**
97       * The maven project in question.
98       * 
99       * @parameter expression="${project}"
100      * @required
101      * @readonly
102      */
103     private MavenProject project;
104 
105     /**
106      * The layout of the generated Maven repository. Supported types - "default" (Maven2) | "legacy" (Maven1) | "flat"
107      * (flat <code>lib/</code> style).
108      * The style "legacy" is only supported if you are running under Maven 2.2.1 and before.
109      * 
110      * @parameter default-value="default"
111      */
112     private String repositoryLayout;
113 
114     /**
115      * You can define a license header file which will be used
116      * instead the default header in the generated scripts.
117      * 
118      * @parameter
119      * @since 1.2
120      */
121     private File licenseHeaderFile;
122 
123     /**
124      * @component
125      */
126     private ArtifactRepositoryFactory artifactRepositoryFactory;
127 
128     /**
129      * @component
130      */
131     private ArtifactInstaller artifactInstaller;
132 
133     // -----------------------------------------------------------------------
134     // Read-only parameters
135     // -----------------------------------------------------------------------
136 
137     /**
138      * @readonly
139      * @parameter expression="${project.runtimeArtifacts}"
140      */
141     private List artifacts;
142 
143     /**
144      * @readonly
145      * @parameter expression="${localRepository}"
146      */
147     private ArtifactRepository localRepository;
148 
149     /**
150      * @readonly
151      * @parameter expression="${project.artifact}"
152      */
153     private Artifact projectArtifact;
154 
155     // -----------------------------------------------------------------------
156     // Components
157     // -----------------------------------------------------------------------
158 
159     /**
160      * @component
161      */
162     private DaemonGeneratorService daemonGeneratorService;
163 
164     /**
165      * @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
166      */
167     private Map availableRepositoryLayouts;
168 
169     // -----------------------------------------------------------------------
170     // AbstractMojo Implementation
171     // -----------------------------------------------------------------------
172 
173     public void execute ()
174             throws MojoExecutionException, MojoFailureException
175     {
176         for ( Iterator itd = daemons.iterator ( ); itd.hasNext ( ); )
177         {
178             Daemon daemon = ( Daemon ) itd.next ( );
179 
180             // -----------------------------------------------------------------------
181             // Load the optional template daemon descriptor
182             // -----------------------------------------------------------------------
183 
184             File descriptor = null;
185 
186             if ( !StringUtils.isEmpty ( daemon.getDescriptor ( ) ) )
187             {
188                 descriptor = new File ( basedir, daemon.getDescriptor ( ) );
189             }
190 
191             // -----------------------------------------------------------------------
192             //
193             // -----------------------------------------------------------------------
194 
195             org.codehaus.mojo.appassembler.model.JvmSettings modelJvmSettings = null;
196 
197             if ( defaultJvmSettings != null )
198             {
199                 modelJvmSettings = convertJvmSettings ( defaultJvmSettings );
200             }
201 
202             ArtifactRepositoryLayout artifactRepositoryLayout =
203                     ( ArtifactRepositoryLayout ) availableRepositoryLayouts.get ( repositoryLayout );
204             if ( artifactRepositoryLayout == null )
205             {
206                 throw new MojoFailureException ( "Unknown repository layout '" + repositoryLayout + "'." );
207             }
208 
209             // -----------------------------------------------------------------------
210             // Create a daemon object from the POM configuration
211             // -----------------------------------------------------------------------
212 
213             org.codehaus.mojo.appassembler.model.Daemon modelDaemon = convertDaemon ( daemon, modelJvmSettings );
214 
215             // -----------------------------------------------------------------------
216             // Default Handling for license file
217             // -----------------------------------------------------------------------
218             if ( this.licenseHeaderFile != null )
219             {
220                 // Allow overwrite if not set otherwise the set license header file will be used.
221                 if ( modelDaemon.getLicenseHeaderFile ( ) == null )
222                 {
223                     modelDaemon.setLicenseHeaderFile ( this.licenseHeaderFile.toString ( ) );
224                 }
225             }
226 
227             // -----------------------------------------------------------------------
228             //
229             // -----------------------------------------------------------------------
230 
231             for ( Iterator i = daemon.getPlatforms ( ).iterator ( ); i.hasNext ( ); )
232             {
233                 String platform = ( String ) i.next ( );
234 
235                 File output = new File ( target, platform );
236 
237                 DaemonGenerationRequest request = new DaemonGenerationRequest ( );
238 
239                 // TODO: split platform from generator (platform = operating systems, generator = jsw, booter,
240                 // standard). Generator is a property of the daemon itself
241                 request.setPlatform ( platform );
242                 request.setStubDescriptor ( descriptor );
243                 request.setStubDaemon ( modelDaemon );
244                 request.setOutputDirectory ( output );
245                 request.setMavenProject ( project );
246                 request.setLocalRepository ( localRepository );
247                 request.setRepositoryLayout ( artifactRepositoryLayout );
248 
249                 try
250                 {
251                     daemonGeneratorService.generateDaemon ( request );
252                 }
253                 catch ( DaemonGeneratorException e )
254                 {
255                     throw new MojoExecutionException ( "Error while generating daemon.", e );
256                 }
257 
258                 File outputDirectory = new File ( request.getOutputDirectory ( ), daemon.getId ( ) );
259 
260                 // The repo where the jar files will be installed
261                 // FIXME: /lib hard coded. Should be made configurable.
262                 // via repositoryName like in AssembleMojo ?
263                 // Might be refactored into AbstractAppAssemblerMojo?
264                 ArtifactRepository artifactRepository = artifactRepositoryFactory.createDeploymentArtifactRepository (
265                         "appassembler", "file://" + outputDirectory.getAbsolutePath ( ) + "/lib",
266                         artifactRepositoryLayout, false );
267 
268                 for ( Iterator it = artifacts.iterator ( ); it.hasNext ( ); )
269                 {
270                     Artifact artifact = ( Artifact ) it.next ( );
271 
272                     installArtifact ( artifactRepository, artifact );
273                 }
274 
275                 // install the project's artifact in the new repository
276                 installArtifact ( artifactRepository, projectArtifact );
277 
278             }
279 
280         }
281     }
282 
283     // ----------------------------------------------------------------------
284     // Install artifacts into the assemble repository
285     // ----------------------------------------------------------------------
286 
287     private void installArtifact ( ArtifactRepository artifactRepository, Artifact artifact )
288             throws MojoExecutionException
289     {
290         try
291         {
292             // Necessary for the artifact's baseVersion to be set correctly
293             // See: http://mail-archives.apache.org/mod_mbox/maven-dev/200511.mbox/%3c437288F4.4080003@apache.org%3e
294             artifact.isSnapshot ( );
295 
296             if ( artifact.getFile ( ) != null )
297             {
298                 artifactInstaller.install ( artifact.getFile ( ), artifact, artifactRepository );
299             }
300         }
301         catch ( ArtifactInstallationException e )
302         {
303             throw new MojoExecutionException ( "Failed to copy artifact.", e );
304         }
305     }
306 
307     // TODO: see if it is possible to just inherit from the model daemon
308     private org.codehaus.mojo.appassembler.model.Daemon convertDaemon ( Daemon daemon,
309             org.codehaus.mojo.appassembler.model.JvmSettings modelJvmSettings )
310     {
311         org.codehaus.mojo.appassembler.model.Daemon modelDaemon;
312 
313         modelDaemon = new org.codehaus.mojo.appassembler.model.Daemon ( );
314 
315         modelDaemon.setId ( daemon.getId ( ) );
316         modelDaemon.setMainClass ( daemon.getMainClass ( ) );
317         modelDaemon.setCommandLineArguments ( daemon.getCommandLineArguments ( ) );
318         modelDaemon.setShowConsoleWindow ( daemon.isShowConsoleWindow ( ) );
319         modelDaemon.setEnvironmentSetupFileName ( daemon.getEnvironmentSetupFileName ( ) );
320 
321         if ( daemon.getJvmSettings ( ) != null )
322         {
323             modelDaemon.setJvmSettings ( convertJvmSettings ( daemon.getJvmSettings ( ) ) );
324         }
325         else
326         {
327             modelDaemon.setJvmSettings ( modelJvmSettings );
328         }
329 
330         if ( daemon.getGeneratorConfigurations ( ) != null )
331         {
332             modelDaemon.setGeneratorConfigurations (
333                     convertGeneratorConfigurations ( daemon.getGeneratorConfigurations ( ) ) );
334         }
335 
336         return modelDaemon;
337     }
338 
339     private List convertGeneratorConfigurations ( List generatorConfigurations )
340     {
341         List value = new ArrayList ( generatorConfigurations.size ( ) );
342         for ( Iterator i = generatorConfigurations.iterator ( ); i.hasNext ( ); )
343         {
344             GeneratorConfiguration config = ( GeneratorConfiguration ) i.next ( );
345 
346             value.add ( convertGeneratorConfiguration ( config ) );
347         }
348         return value;
349     }
350 
351     private org.codehaus.mojo.appassembler.model.GeneratorConfiguration convertGeneratorConfiguration (
352             GeneratorConfiguration config )
353     {
354         org.codehaus.mojo.appassembler.model.GeneratorConfiguration value =
355                 new org.codehaus.mojo.appassembler.model.GeneratorConfiguration ( );
356         value.setGenerator ( config.getGenerator ( ) );
357         value.setConfiguration ( config.getConfiguration ( ) );
358         value.setIncludes ( config.getIncludes ( ) );
359 
360         return value;
361     }
362 
363     // TODO: see if it is possible to just inherit from the model JVM Settings
364     private org.codehaus.mojo.appassembler.model.JvmSettings convertJvmSettings ( JvmSettings jvmSettings )
365     {
366         org.codehaus.mojo.appassembler.model.JvmSettings modelJvmSettings =
367                 new org.codehaus.mojo.appassembler.model.JvmSettings ( );
368 
369         modelJvmSettings.setInitialMemorySize ( jvmSettings.getInitialMemorySize ( ) );
370         modelJvmSettings.setMaxMemorySize ( jvmSettings.getMaxMemorySize ( ) );
371         modelJvmSettings.setMaxStackSize ( jvmSettings.getMaxStackSize ( ) );
372         if ( jvmSettings.getSystemProperties ( ) == null )
373         {
374             modelJvmSettings.setSystemProperties ( new ArrayList ( ) );
375         }
376         else
377         {
378             modelJvmSettings.setSystemProperties ( Arrays.asList ( jvmSettings.getSystemProperties ( ) ) );
379         }
380         if ( jvmSettings.getExtraArguments ( ) == null )
381         {
382             modelJvmSettings.setExtraArguments ( new ArrayList ( ) );
383         }
384         else
385         {
386             modelJvmSettings.setExtraArguments ( Arrays.asList ( jvmSettings.getExtraArguments ( ) ) );
387         }
388 
389         return modelJvmSettings;
390     }
391 
392     public void setAvailableRepositoryLayouts ( Map availableRepositoryLayouts )
393     {
394         this.availableRepositoryLayouts = availableRepositoryLayouts;
395     }
396 
397     public void setDaemons ( Set daemons )
398     {
399         this.daemons = daemons;
400     }
401 }