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  package com.jayway.maven.plugins.android.standalonemojos;
18  
19  import com.jayway.maven.plugins.android.AbstractAndroidMojo;
20  import com.jayway.maven.plugins.android.CommandExecutor;
21  import com.jayway.maven.plugins.android.common.JarHelper;
22  import org.apache.commons.io.FileUtils;
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.jar.JarEntry;
30  import java.util.jar.JarFile;
31  
32  /**
33   * Unpack libraries code and dependencies into target.
34   * <p/>
35   * This can be useful for using the proguard maven plugin to provide the input jars. Although it is encouraged to use
36   * the proguard mojo of the android maven plugin.
37   *
38   * @author hugo.josefson@jayway.com
39   * @author Manfred Moser
40   * @goal unpack
41   * @requiresDependencyResolution compile
42   */
43  public class UnpackMojo extends AbstractAndroidMojo
44  {
45      /**
46       * If true, the library will be unpacked only when outputDirectory doesn't
47       * exist, i.e, a clean build for most cases.
48       *
49       * @parameter expression="${android.lazyLibraryUnpack}"
50       * default-value="false"
51       */
52      private boolean lazyLibraryUnpack;
53  
54      public void execute() throws MojoExecutionException, MojoFailureException
55      {
56  
57          CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
58          executor.setLogger( this.getLog() );
59  
60          if ( generateApk )
61          {
62              // Unpack all dependent and main classes
63              unpackClasses();
64          }
65      }
66  
67      private File unpackClasses() throws MojoExecutionException
68      {
69          File outputDirectory = new File( project.getBuild().getDirectory(), "android-classes" );
70          if ( lazyLibraryUnpack && outputDirectory.exists() )
71          {
72              getLog().info( "skip library unpacking due to lazyLibraryUnpack policy" );
73          }
74          else
75          {
76              for ( Artifact artifact : getRelevantCompileArtifacts() )
77              {
78  
79                  if ( artifact.getFile().isDirectory() )
80                  {
81                      try
82                      {
83                          FileUtils.copyDirectory( artifact.getFile(), outputDirectory );
84                      }
85                      catch ( IOException e )
86                      {
87                          throw new MojoExecutionException( "IOException while copying "
88                                  + artifact.getFile().getAbsolutePath() + " into " + outputDirectory.getAbsolutePath()
89                                  , e );
90                      }
91                  }
92                  else
93                  {
94                      try
95                      {
96                          JarHelper.unjar( new JarFile( artifact.getFile() ), outputDirectory,
97                                  new JarHelper.UnjarListener()
98                                  {
99                                      @Override
100                                     public boolean include( JarEntry jarEntry )
101                                     {
102                                         return ! jarEntry.getName().startsWith( "META-INF" ) && jarEntry.getName()
103                                                 .endsWith( ".class" );
104                                     }
105                                 } );
106                     }
107                     catch ( IOException e )
108                     {
109                         throw new MojoExecutionException( "IOException while unjarring "
110                                 + artifact.getFile().getAbsolutePath() + " into " + outputDirectory.getAbsolutePath()
111                                 , e );
112                     }
113                 }
114 
115             }
116         }
117 
118         try
119         {
120             File sourceDirectory = new File( project.getBuild().getOutputDirectory() );
121             FileUtils.copyDirectory( sourceDirectory, outputDirectory );
122         }
123         catch ( IOException e )
124         {
125             throw new MojoExecutionException( "IOException while copying " + sourceDirectory.getAbsolutePath()
126                     + " into " + outputDirectory.getAbsolutePath(), e );
127         }
128         return outputDirectory;
129     }
130 
131 }