-
Notifications
You must be signed in to change notification settings - Fork 394
/
DexMojo.java
215 lines (191 loc) · 7.98 KB
/
DexMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (C) 2009 Jayway AB
* Copyright (C) 2007-2008 JVending Masa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jayway.maven.plugins.android.phase08preparepackage;
import com.jayway.maven.plugins.android.AbstractAndroidMojo;
import com.jayway.maven.plugins.android.CommandExecutor;
import com.jayway.maven.plugins.android.ExecutionException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.jar.JarArchiver;
import org.codehaus.plexus.archiver.util.DefaultFileSet;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Converts compiled Java classes to the Android dex format.
*
* @author hugo.josefson@jayway.com
* @goal dex
* @phase prepare-package
* @requiresDependencyResolution compile
*/
public class DexMojo extends AbstractAndroidMojo {
/**
* Extra JVM Arguments
*
* @parameter
* @optional
*/
private String[] jvmArguments;
/**
* Decides whether to pass the --core-library flag to dx.
*
* @parameter default-value="false"
*/
private boolean coreLibrary;
/**
* Decides whether to pass the --no-locals flag to dx.
*
* @parameter default-value="false"
*/
private boolean noLocals;
public void execute() throws MojoExecutionException, MojoFailureException {
CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
executor.setLogger(this.getLog());
File outputFile = new File(project.getBuild().getDirectory() + File.separator + "classes.dex");
File inputFile = new File(project.getBuild().getDirectory() + File.separator + project.getBuild().getFinalName() + ".jar");
if (generateApk) {
runDex(executor, outputFile, inputFile);
}
if (attachJar) {
projectHelper.attachArtifact(project, "jar", project.getArtifact().getClassifier(), inputFile);
}
if (attachSources) {
// Also attach an .apksources, containing sources from this project.
final File apksources = createApkSourcesFile();
projectHelper.attachArtifact(project, "apksources", apksources);
}
}
private void runDex(CommandExecutor executor, File outputFile,
File inputFile) throws MojoExecutionException {
File classesOutputDirectory = new File(project.getBuild().getDirectory(), "android-classes");
List<String> commands = new ArrayList<String>();
if (jvmArguments != null) {
for (String jvmArgument : jvmArguments) {
// preserve backward compatibility allowing argument with or without dash (e.g. Xmx512m as well as
// -Xmx512m should work) (see http://code.google.com/p/maven-android-plugin/issues/detail?id=153)
if (!jvmArgument.startsWith("-")) {
jvmArgument = "-" + jvmArgument;
}
commands.add(jvmArgument);
}
}
commands.add("-jar");
commands.add(getAndroidSdk().getPathForTool("dx.jar"));
commands.add("--dex");
if (coreLibrary) {
commands.add("--core-library");
}
commands.add("--output=" + outputFile.getAbsolutePath());
if (noLocals) {
commands.add("--no-locals");
}
commands.add(classesOutputDirectory.getAbsolutePath());
final String javaExecutable = getJavaExecutable().getAbsolutePath();
getLog().info(javaExecutable + " " + commands.toString());
try {
executor.executeCommand(javaExecutable, commands, project.getBasedir(), false);
} catch (ExecutionException e) {
throw new MojoExecutionException("", e);
}
}
/**
* Figure out the full path to the current java executable.
*
* @return the full path to the current java executable.
*/
private static File getJavaExecutable() {
final String javaHome = System.getProperty("java.home");
final String slash = File.separator;
return new File(javaHome + slash + "bin" + slash + "java");
}
protected File createApkSourcesFile() throws MojoExecutionException {
final File apksources = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".apksources");
FileUtils.deleteQuietly(apksources);
try {
JarArchiver jarArchiver = new JarArchiver();
jarArchiver.setDestFile(apksources);
addDirectory(jarArchiver, assetsDirectory, "assets");
addDirectory(jarArchiver, resourceDirectory, "res");
addDirectory(jarArchiver, sourceDirectory, "src/main/java");
addJavaResources(jarArchiver, project.getBuild().getResources());
jarArchiver.createArchive();
} catch (ArchiverException e) {
throw new MojoExecutionException("ArchiverException while creating .apksource file.", e);
} catch (IOException e) {
throw new MojoExecutionException("IOException while creating .apksource file.", e);
}
return apksources;
}
/**
* Makes sure the string ends with "/"
*
* @param prefix any string, or null.
* @return the prefix with a "/" at the end, never null.
*/
protected String endWithSlash(String prefix) {
prefix = StringUtils.defaultIfEmpty(prefix, "/");
if (!prefix.endsWith("/")) {
prefix = prefix + "/";
}
return prefix;
}
/**
* Adds a directory to a {@link JarArchiver} with a directory prefix.
*
* @param jarArchiver
* @param directory The directory to add.
* @param prefix An optional prefix for where in the Jar file the directory's contents should go.
* @throws ArchiverException
*/
protected void addDirectory(JarArchiver jarArchiver, File directory, String prefix) throws ArchiverException {
if (directory != null && directory.exists()) {
final DefaultFileSet fileSet = new DefaultFileSet();
fileSet.setPrefix(endWithSlash(prefix));
fileSet.setDirectory(directory);
jarArchiver.addFileSet(fileSet);
}
}
protected void addJavaResources(JarArchiver jarArchiver, List<Resource> javaResources) throws ArchiverException {
for (Resource javaResource : javaResources) {
addJavaResource(jarArchiver, javaResource);
}
}
/**
* Adds a Java Resources directory (typically "src/main/resources") to a {@link JarArchiver}.
*
* @param jarArchiver
* @param javaResource The Java resource to add.
* @throws ArchiverException
*/
protected void addJavaResource(JarArchiver jarArchiver, Resource javaResource) throws ArchiverException {
if (javaResource != null) {
final File javaResourceDirectory = new File(javaResource.getDirectory());
if (javaResourceDirectory.exists()) {
final DefaultFileSet javaResourceFileSet = new DefaultFileSet();
javaResourceFileSet.setDirectory(javaResourceDirectory);
javaResourceFileSet.setPrefix(endWithSlash("src/main/resources"));
jarArchiver.addFileSet(javaResourceFileSet);
}
}
}
}