View Javadoc
1   package org.codehaus.mojo.appassembler;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2006-2012, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.List;
31  import java.util.Set;
32  
33  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.plugin.MojoFailureException;
36  import org.apache.maven.plugins.annotations.LifecyclePhase;
37  import org.apache.maven.plugins.annotations.Mojo;
38  import org.apache.maven.plugins.annotations.Parameter;
39  import org.apache.maven.plugins.annotations.ResolutionScope;
40  import org.codehaus.mojo.appassembler.daemon.DaemonGenerationRequest;
41  import org.codehaus.mojo.appassembler.daemon.DaemonGeneratorException;
42  import org.codehaus.plexus.util.StringUtils;
43  
44  /**
45   * Generates JSW based daemon wrappers.
46   *
47   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
48   * @version $Id$
49   */
50  @Mojo( name = "generate-daemons", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true )
51  public class GenerateDaemonsMojo
52      extends AbstractScriptGeneratorMojo
53  {
54  
55      // -----------------------------------------------------------------------
56      // Parameters
57      // -----------------------------------------------------------------------
58  
59      /**
60       * The base directory of the project.
61       */
62      @Parameter( defaultValue = "${basedir}", required = true )
63      private File basedir;
64  
65      /**
66       * Set of {@linkplain Daemon}s to generate.
67       */
68      @Parameter( required = true )
69      private Set<Daemon> daemons;
70  
71      /**
72       * {@linkplain JvmSettings} describing min/max memory and stack size, system properties and extra arguments.
73       */
74      @Parameter
75      private JvmSettings defaultJvmSettings;
76  
77      /**
78       * Path (relative to <code>assembleDirectory</code>) of the desired output repository.
79       *
80       * @since 1.5
81       * @todo Synchronize the default value with the other mojos in 2.0
82       */
83      @Parameter( defaultValue = "lib" )
84      private String repositoryName;
85  
86      /**
87       * Target directory for generated daemons.
88       */
89      @Parameter( defaultValue = "${project.build.directory}/generated-resources/appassembler", required = true )
90      private File target;
91  
92      /**
93       * When enable, name wrapper configuration file as wrapper-${daemon.id}.conf
94       *
95       * @since 1.3
96       */
97      @Parameter( defaultValue = "false" )
98      private boolean useDaemonIdAsWrapperConfName;
99  
100     /**
101      * When enable, prefix the wrapper executable as ${daemon.id}. Otherwise, use the original name( ie wrapper )
102      *
103      * @since 1.8
104      */
105     @Parameter( defaultValue = "false" )
106     private boolean useDaemonIdAsWrapperExePrefixName;
107 
108     /**
109      * Use this option to override the current built-in delta pack binary. You will need to unpack your delta pack
110      * version to a known location set by this option
111      *
112      * @since 1.4.0
113      */
114     @Parameter( property = "externalDeltaPackDirectory" )
115     private File externalDeltaPackDirectory;
116 
117     /**
118      * Use this option to pre insert a content of a known file into the generated wrapper config file. For example:
119      * $include ../conf/another-wrapper.conf
120      *
121      * @since 1.7.0
122      */
123     @Parameter( property = "preWrapperConf" )
124     private File preWrapperConf;
125 
126     // -----------------------------------------------------------------------
127     // AbstractMojo Implementation
128     // -----------------------------------------------------------------------
129 
130     /**
131      * calling from Maven.
132      *
133      * @see org.apache.maven.plugin.AbstractMojo#execute()
134      */
135     public void execute()
136         throws MojoExecutionException, MojoFailureException
137     {
138 
139         if ( useWildcardClassPath && !repositoryLayout.equalsIgnoreCase( "flat" ) )
140         {
141             throw new MojoExecutionException( "The useWildcardClassPath works only in"
142                 + " combination with repositoryLayout flat." );
143         }
144 
145         for ( Daemon daemon : daemons )
146         {
147             // -----------------------------------------------------------------------
148             // Load the optional template daemon descriptor
149             // -----------------------------------------------------------------------
150 
151             File descriptor = null;
152 
153             if ( !StringUtils.isEmpty( daemon.getDescriptor() ) )
154             {
155                 descriptor = new File( basedir, daemon.getDescriptor() );
156             }
157 
158             // -----------------------------------------------------------------------
159             //
160             // -----------------------------------------------------------------------
161 
162             org.codehaus.mojo.appassembler.model.JvmSettings modelJvmSettings = null;
163 
164             if ( defaultJvmSettings != null )
165             {
166                 modelJvmSettings = convertJvmSettings( defaultJvmSettings );
167             }
168 
169             ArtifactRepositoryLayout artifactRepositoryLayout = getArtifactRepositoryLayout();
170 
171             // -----------------------------------------------------------------------
172             // Create a daemon object from the POM configuration
173             // -----------------------------------------------------------------------
174 
175             org.codehaus.mojo.appassembler.model.Daemon modelDaemon = convertDaemon( daemon, modelJvmSettings );
176 
177             // -----------------------------------------------------------------------
178             // Default Handling for license file
179             // -----------------------------------------------------------------------
180             if ( this.licenseHeaderFile != null )
181             {
182                 // Allow overwrite if not set otherwise the set license header file will be used.
183                 if ( modelDaemon.getLicenseHeaderFile() == null )
184                 {
185                     modelDaemon.setLicenseHeaderFile( this.licenseHeaderFile.toString() );
186                 }
187             }
188 
189             modelDaemon.setConfigurationDirectory( configurationDirectory );
190             modelDaemon.setEnvironmentSetupFileName( environmentSetupFileName );
191             modelDaemon.setRepositoryName( repositoryName );
192             modelDaemon.setUseTimestampInSnapshotFileName( useTimestampInSnapshotFileName );
193             modelDaemon.setUseDaemonIdAsWrapperConfName( useDaemonIdAsWrapperConfName );
194             modelDaemon.setUseDaemonIdAsWrapperExePrefixName( useDaemonIdAsWrapperExePrefixName );
195             modelDaemon.setUseWildcardClassPath( useWildcardClassPath );
196 
197             if ( endorsedDir != null )
198             {
199                 modelDaemon.setEndorsedDir( endorsedDir );
200             }
201 
202             if ( preWrapperConf != null )
203             {
204                 modelDaemon.setPreWrapperConf( preWrapperConf.getAbsolutePath() );
205             }
206 
207             if ( this.unixScriptTemplate != null )
208             {
209                 modelDaemon.setUnixScriptTemplate( unixScriptTemplate );
210             }
211             if ( this.windowsScriptTemplate != null )
212             {
213                 modelDaemon.setWindowsScriptTemplate( windowsScriptTemplate );
214             }
215 
216             if ( this.externalDeltaPackDirectory != null )
217             {
218                 modelDaemon.setExternalDeltaPackDirectory( this.externalDeltaPackDirectory.getAbsolutePath() );
219             }
220 
221             // -----------------------------------------------------------------------
222             //
223             // -----------------------------------------------------------------------
224 
225             for ( String platform : daemon.getPlatforms() )
226             {
227                 File output = new File( target, platform );
228 
229                 DaemonGenerationRequest request = new DaemonGenerationRequest();
230 
231                 // TODO: split platform from generator (platform = operating systems, generator = jsw, booter,
232                 // standard). Generator is a property of the daemon itself
233                 request.setPlatform( platform );
234                 request.setStubDescriptor( descriptor );
235                 request.setStubDaemon( modelDaemon );
236                 request.setOutputDirectory( output );
237                 request.setMavenProject( mavenProject );
238                 request.setLocalRepository( localRepository );
239                 request.setRepositoryLayout( artifactRepositoryLayout );
240                 request.setOutputFileNameMapping( this.outputFileNameMapping );
241 
242                 try
243                 {
244                     daemonGeneratorService.generateDaemon( request );
245                 }
246                 catch ( DaemonGeneratorException e )
247                 {
248                     throw new MojoExecutionException( "Error while generating daemon.", e );
249                 }
250 
251                 File outputDirectory = new File( request.getOutputDirectory(), daemon.getId() );
252 
253                 // ----------------------------------------------------------------------
254                 // Install dependencies in the new repository
255                 // ----------------------------------------------------------------------
256                 super.installDependencies( outputDirectory.getAbsolutePath(), repositoryName );
257 
258                 // ----------------------------------------------------------------------
259                 // Copy configuration directory
260                 // ----------------------------------------------------------------------
261 
262                 if ( this.preAssembleDirectory != null && this.preAssembleDirectory.isDirectory() )
263                 {
264                     doCopyPreAssembleDirectory( outputDirectory.getAbsolutePath() );
265                 }
266 
267                 if ( this.copyConfigurationDirectory && configurationSourceDirectory.isDirectory() )
268                 {
269                     doCopyConfigurationDirectory( outputDirectory.getAbsolutePath() );
270                 }
271 
272                 // ----------------------------------------------------------------------
273                 // Create logs and temp dirs if specified
274                 // ----------------------------------------------------------------------
275 
276                 doCreateExtraDirectories( outputDirectory );
277             }
278 
279         }
280     }
281 
282     // TODO: see if it is possible to just inherit from the model daemon
283     private org.codehaus.mojo.appassembler.model.Daemon convertDaemon( Daemon daemon,
284                                                                        org.codehaus.mojo.appassembler.model.JvmSettings modelJvmSettings )
285     {
286         org.codehaus.mojo.appassembler.model.Daemon modelDaemon;
287 
288         modelDaemon = new org.codehaus.mojo.appassembler.model.Daemon();
289 
290         modelDaemon.setId( daemon.getId() );
291         modelDaemon.setMainClass( daemon.getMainClass() );
292         modelDaemon.setWrapperMainClass( daemon.getWrapperMainClass() );
293         modelDaemon.setWrapperLogFile( daemon.getWrapperLogFile() );
294         modelDaemon.setCommandLineArguments( daemon.getCommandLineArguments() );
295         modelDaemon.setConfigurationDirectory( daemon.getConfigurationDirectory() );
296         modelDaemon.setLicenseHeaderFile( daemon.getLicenseHeaderFile() );
297         modelDaemon.setShowConsoleWindow( daemon.isShowConsoleWindow() );
298         modelDaemon.setEnvironmentSetupFileName( daemon.getEnvironmentSetupFileName() );
299         modelDaemon.setRepositoryName( daemon.getRepositoryName() );
300         modelDaemon.setEndorsedDir( daemon.getEndorsedDir() );
301         modelDaemon.setPreWrapperConf( daemon.getPreWrapperConf() );
302 
303         if ( daemon.getJvmSettings() != null )
304         {
305             modelDaemon.setJvmSettings( convertJvmSettings( daemon.getJvmSettings() ) );
306         }
307         else
308         {
309             modelDaemon.setJvmSettings( modelJvmSettings );
310         }
311 
312         if ( daemon.getGeneratorConfigurations() != null )
313         {
314             modelDaemon.setGeneratorConfigurations( convertGeneratorConfigurations( daemon.getGeneratorConfigurations() ) );
315         }
316 
317         return modelDaemon;
318     }
319 
320     private List<org.codehaus.mojo.appassembler.model.GeneratorConfiguration> convertGeneratorConfigurations( List<GeneratorConfiguration> generatorConfigurations )
321     {
322         List<org.codehaus.mojo.appassembler.model.GeneratorConfiguration> value =
323             new ArrayList<org.codehaus.mojo.appassembler.model.GeneratorConfiguration>( generatorConfigurations.size() );
324         for ( GeneratorConfiguration config : generatorConfigurations )
325         {
326             value.add( convertGeneratorConfiguration( config ) );
327         }
328         return value;
329     }
330 
331     private org.codehaus.mojo.appassembler.model.GeneratorConfiguration convertGeneratorConfiguration( GeneratorConfiguration config )
332     {
333         org.codehaus.mojo.appassembler.model.GeneratorConfiguration value =
334             new org.codehaus.mojo.appassembler.model.GeneratorConfiguration();
335         value.setGenerator( config.getGenerator() );
336         value.setConfiguration( config.getConfiguration() );
337         value.setIncludes( config.getIncludes() );
338 
339         return value;
340     }
341 
342     // TODO: see if it is possible to just inherit from the model JVM Settings
343     private org.codehaus.mojo.appassembler.model.JvmSettings convertJvmSettings( JvmSettings jvmSettings )
344     {
345         org.codehaus.mojo.appassembler.model.JvmSettings modelJvmSettings =
346             new org.codehaus.mojo.appassembler.model.JvmSettings();
347 
348         modelJvmSettings.setInitialMemorySize( jvmSettings.getInitialMemorySize() );
349         modelJvmSettings.setMaxMemorySize( jvmSettings.getMaxMemorySize() );
350         modelJvmSettings.setMaxStackSize( jvmSettings.getMaxStackSize() );
351         if ( jvmSettings.getSystemProperties() == null )
352         {
353             modelJvmSettings.setSystemProperties( new ArrayList<String>() );
354         }
355         else
356         {
357             modelJvmSettings.setSystemProperties( Arrays.asList( jvmSettings.getSystemProperties() ) );
358         }
359         if ( jvmSettings.getExtraArguments() == null )
360         {
361             modelJvmSettings.setExtraArguments( new ArrayList<String>() );
362         }
363         else
364         {
365             modelJvmSettings.setExtraArguments( Arrays.asList( jvmSettings.getExtraArguments() ) );
366         }
367 
368         return modelJvmSettings;
369     }
370 
371 }