View Javadoc
1   /*
2    * Copyright (C) 2009 Jayway AB
3    * Copyright (C) 2007-2008 JVending Masa
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package com.simpligility.maven.plugins.android.standalonemojos;
19  
20  import com.simpligility.maven.plugins.android.AbstractAndroidMojo;
21  import com.simpligility.maven.plugins.android.CommandExecutor;
22  import com.simpligility.maven.plugins.android.common.JarHelper;
23  import com.simpligility.maven.plugins.android.config.ConfigPojo;
24  import com.simpligility.maven.plugins.android.config.PullParameter;
25  import com.simpligility.maven.plugins.android.configuration.MetaInf;
26  import com.simpligility.maven.plugins.android.configuration.Unpack;
27  
28  import org.apache.commons.io.FileUtils;
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.MojoFailureException;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.plugins.annotations.ResolutionScope;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.util.jar.JarEntry;
39  import java.util.jar.JarFile;
40  
41  /**
42   * Unpack libraries code and dependencies into target.
43   * 
44   * This can be useful for using the proguard maven plugin to provide the input jars. Although it is encouraged to use
45   * the proguard mojo of the android maven plugin.
46   *
47   * @author hugo.josefson@jayway.com
48   * @author Manfred Moser
49   */
50  @Mojo( name = "unpack", requiresDependencyResolution = ResolutionScope.COMPILE )
51  public class UnpackMojo extends AbstractAndroidMojo
52  {
53      /**
54       * If true, the library will be unpacked only when outputDirectory doesn't
55       * exist, i.e, a clean build for most cases.
56       *
57       * @deprecated use ${android.unpack.lazy}
58       */
59      @Deprecated
60      @Parameter( property = "android.lazyLibraryUnpack" )
61      private boolean lazyLibraryUnpack;
62  
63      @PullParameter( defaultValueGetterMethod = "getDefaultMetaInf" )
64      private MetaInf unpackMetaInf;
65  
66      @Parameter( property = "android.unpack.lazy" )
67      @PullParameter( defaultValueGetterMethod = "getLazyLibraryUnpack" )
68      private Boolean unpackLazy;
69  
70      @Parameter( alias = "metaInf" )
71      private MetaInf pluginMetaInf;
72  
73      @Parameter
74      @ConfigPojo( prefix = "unpack" )
75      private Unpack unpack;
76  
77      @Parameter( defaultValue = "${project.build.directory}/android-classes", readonly = true )
78      private File unpackOutputDirectory;
79  
80      @Parameter( defaultValue = "false", readonly = true )
81      private boolean includeNonClassFiles;
82  
83      public void execute() throws MojoExecutionException, MojoFailureException
84      {
85  
86          CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
87          executor.setLogger( this.getLog() );
88  
89          if ( generateApk )
90          {
91              // Unpack all dependent and main classes
92              unpackClasses();
93          }
94      }
95  
96      private File unpackClasses() throws MojoExecutionException
97      {
98          File outputDirectory = unpackOutputDirectory;
99          if ( lazyLibraryUnpack && outputDirectory.exists() )
100         {
101             getLog().info( "skip library unpacking due to lazyLibraryUnpack policy" );
102         }
103         else
104         {
105             outputDirectory.mkdirs();
106 
107             for ( Artifact artifact : getRelevantCompileArtifacts() )
108             {
109 
110                 if ( artifact.getFile().isDirectory() )
111                 {
112                     try
113                     {
114                         FileUtils.copyDirectory( artifact.getFile(), outputDirectory );
115                     }
116                     catch ( IOException e )
117                     {
118                         throw new MojoExecutionException( "IOException while copying "
119                                 + artifact.getFile().getAbsolutePath() + " into " + outputDirectory.getAbsolutePath()
120                                 , e );
121                     }
122                 }
123                 else
124                 {
125                     try
126                     {
127                         JarHelper.unjar( new JarFile( artifact.getFile() ), outputDirectory,
128                                 new JarHelper.UnjarListener()
129                                 {
130                                     @Override
131                                     public boolean include( JarEntry jarEntry )
132                                     {
133                                          return isIncluded( jarEntry );
134                                     }
135                                 } );
136                     }
137                     catch ( IOException e )
138                     {
139                         throw new MojoExecutionException( "IOException while unjarring "
140                                 + artifact.getFile().getAbsolutePath() + " into " + outputDirectory.getAbsolutePath()
141                                 , e );
142                     }
143                 }
144 
145             }
146         }
147 
148         try
149         {
150             if ( !projectOutputDirectory.equals( outputDirectory ) )
151             {
152                 FileUtils.copyDirectory( projectOutputDirectory, outputDirectory );
153             }
154         }
155         catch ( IOException e )
156         {
157             throw new MojoExecutionException( "IOException while copying " + sourceDirectory.getAbsolutePath()
158                     + " into " + outputDirectory.getAbsolutePath(), e );
159         }
160         return outputDirectory;
161     }
162 
163     boolean isIncluded( JarEntry jarEntry )
164     {
165         String entName = jarEntry.getName();
166 
167         if ( entName.endsWith( ".class" ) )
168         {
169             return true;
170         }
171 
172         if ( includeNonClassFiles && !entName.startsWith( "META-INF/" ) )
173         {
174             return true;
175         }
176 
177         return this.unpackMetaInf != null && this.unpackMetaInf.isIncluded( entName );
178     }
179     
180     MetaInf getDefaultMetaInf()
181     {
182         return this.pluginMetaInf;
183     }
184 
185     boolean getLazyLibraryUnpack()
186     {
187         return this.lazyLibraryUnpack;
188     }
189 }