1 package org.codehaus.mojo.natives.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
43
44
45 @Mojo(name = "unzipinc", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE)
46 public class NativeUnZipIncMojo
47 extends AbstractNativeMojo
48 {
49
50
51
52
53
54 @Parameter(defaultValue = "${project.build.directory}/native/markers", required = true)
55 private File dependencyIncZipMarkerDirectory;
56
57
58
59
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
141
142
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
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 }