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  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Set;
30  
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugins.annotations.Component;
34  import org.apache.maven.plugins.annotations.LifecyclePhase;
35  import org.apache.maven.plugins.annotations.Mojo;
36  import org.apache.maven.plugins.annotations.Parameter;
37  import org.apache.maven.plugins.annotations.ResolutionScope;
38  import org.codehaus.plexus.archiver.UnArchiver;
39  import org.codehaus.plexus.archiver.manager.ArchiverManager;
40  
41  /**
42   * Unpack any .inczip dependencies to be included as system include path
43   * @since 1.0-alpha-4
44   */
45  @Mojo(name = "unzipinc", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE)
46  public class NativeUnZipIncMojo
47      extends AbstractNativeMojo
48  {
49  
50      /**
51       * Internal
52       * @since 1.0-alpha-4
53       */
54      @Parameter(defaultValue = "${project.build.directory}/native/markers", required = true)
55      private File dependencyIncZipMarkerDirectory;
56  
57      /**
58       * Internal component for archiving purposes
59       * @since 1.0-alpha-4
60       */
61      @Component
62      private ArchiverManager archiverManager;
63  
64      public void execute()
65          throws MojoExecutionException
66      {
67          if ( unpackIncZipDepenedencies() )
68          {
69              this.getPluginContext().put( AbstractNativeMojo.INCZIP_FOUND, new Boolean( "true" ) );
70          }
71      }
72  
73      private boolean unpackIncZipDepenedencies()
74          throws MojoExecutionException
75      {
76          List files = getIncZipDependencies();
77  
78          Iterator iter = files.iterator();
79  
80          for ( int i = 0; i < files.size(); ++i )
81          {
82              Artifact artifact = (Artifact) iter.next();
83              File incZipFile = artifact.getFile();
84  
85              File marker =
86                  new File( this.dependencyIncZipMarkerDirectory, artifact.getGroupId() + "." + artifact.getArtifactId() );
87  
88              if ( !marker.exists() || marker.lastModified() < incZipFile.lastModified() )
89              {
90                  try
91                  {
92                      unpackZipFile( incZipFile );
93  
94                      marker.delete();
95  
96                      if ( !dependencyIncZipMarkerDirectory.exists() )
97                      {
98                          dependencyIncZipMarkerDirectory.mkdirs();
99                      }
100 
101                     marker.createNewFile();
102                 }
103                 catch ( IOException e )
104                 {
105                     throw new MojoExecutionException( e.getMessage(), e );
106                 }
107             }
108         }
109 
110         return files.size() != 0;
111 
112     }
113 
114     protected void unpackZipFile( File zipFile )
115         throws MojoExecutionException
116     {
117         this.getLog().info( "Unpacking: " + zipFile );
118 
119         try
120         {
121             if ( !dependencyIncludeDirectory.exists() )
122             {
123                 dependencyIncludeDirectory.mkdirs();
124             }
125 
126             UnArchiver archiver = this.archiverManager.getUnArchiver( "zip" );
127             archiver.setOverwrite( true );
128             archiver.setDestDirectory( this.dependencyIncludeDirectory );
129             archiver.setSourceFile( zipFile );
130             archiver.extract();
131         }
132         catch ( Exception e )
133         {
134             throw new MojoExecutionException( e.getMessage(), e );
135         }
136 
137     }
138 
139     /**
140      * Get all .inczip compile time dependencies
141      *
142      * @return
143      */
144     private List getIncZipDependencies()
145     {
146         List list = new ArrayList();
147 
148         Set artifacts = this.project.getDependencyArtifacts();
149 
150         if ( artifacts != null )
151         {
152             for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
153             {
154                 Artifact artifact = (Artifact) iter.next();
155 
156                 // pick up only native header archive
157                 if ( !INCZIP_TYPE.equals( artifact.getType() ) )
158                 {
159                     continue;
160                 }
161 
162                 list.add( artifact );
163             }
164         }
165 
166         return list;
167     }
168 
169 }