View Javadoc
1   package org.codehaus.mojo.natives.plugin;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
9    * associated documentation files (the "Software"), to deal in the Software without restriction,
10   * including without limitation the rights to use, copy, modify, merge, publish, distribute,
11   * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in all copies or
15   * substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
18   * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20   * DAMAGES OR OTHER 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 SOFTWARE.
22   */
23  
24  import java.io.File;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.annotations.Component;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.project.MavenProjectHelper;
32  import org.codehaus.mojo.natives.NativeSources;
33  import org.codehaus.plexus.archiver.util.DefaultFileSet;
34  import org.codehaus.plexus.archiver.zip.ZipArchiver;
35  
36  /**
37   * Prepare include file bundle to be attached to maven for deployment purpose
38   * @since 1.0-alpha-4
39   */
40  @Mojo(name = "inczip", defaultPhase = LifecyclePhase.PACKAGE)
41  public class NativeBundleIncludeFilesMojo
42      extends AbstractNativeMojo
43  {
44  
45      /**
46       * Array of NativeSources containing include directories and source files.
47       * @since 1.0-alpha-4
48       */
49      @Parameter
50      private NativeSources[] sources = new NativeSources[0];
51  
52      /**
53       * Archive file to bundle all enable NativeSources
54       * @since 1.0-alpha-4
55       */
56      @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.inczip", required = true)
57      private File incZipFile;
58  
59      /**
60       * Option to skip include source bundle deployment
61       * @since 1.0-alpha-4
62       */
63      @Parameter(defaultValue = "false")
64      private boolean skipIncludeDeployment;
65  
66      /**
67       * Maven ProjectHelper.
68       * @since 1.0-alpha-4
69       */
70      @Component
71      private MavenProjectHelper projectHelper;
72  
73      public void execute()
74          throws MojoExecutionException
75      {
76          if ( skipIncludeDeployment )
77          {
78              return;
79          }
80  
81          if ( this.sources.length != 0 )
82          {
83              try
84              {
85                  ZipArchiver archiver = new ZipArchiver();
86  
87                  boolean zipIt = false;
88                  for ( int i = 0; i < sources.length; ++i )
89                  {
90                      if ( sources[i].isDeployable() )
91                      {
92                          DefaultFileSet fileSet = new DefaultFileSet();
93                          fileSet.setUsingDefaultExcludes( true );
94                          fileSet.setDirectory( sources[i].getDirectory() );
95                          fileSet.setIncludes( sources[i].getIncludes() );
96                          fileSet.setExcludes( sources[i].getExcludes() );
97                          archiver.addFileSet( fileSet );
98                          zipIt = true;
99                      }
100                 }
101 
102                 if ( zipIt )
103                 {
104                     archiver.setDestFile( this.incZipFile );
105                     archiver.createArchive();
106                     projectHelper.attachArtifact( this.project, INCZIP_TYPE, null, this.incZipFile );
107                 }
108             }
109             catch ( Exception e )
110             {
111                 throw new MojoExecutionException( e.getMessage(), e );
112             }
113         }
114     }
115 
116 }