1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
34
35
36
37
38
39
40
41
42
43 public class UnpackMojo extends AbstractAndroidMojo
44 {
45
46
47
48
49
50
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
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 }