Skip to content

Commit 53589de

Browse files
authored
Merge pull request #803 from danielbobbert/add_dependencies_to_aar
Add options to include dependencies (JARs) into the generated AAR
2 parents cca60c5 + 12e1da0 commit 53589de

File tree

1 file changed

+73
-0
lines changed
  • src/main/java/com/simpligility/maven/plugins/android/phase09package

1 file changed

+73
-0
lines changed

src/main/java/com/simpligility/maven/plugins/android/phase09package/AarMojo.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.simpligility.maven.plugins.android.phase09package;
1818

19+
import static com.simpligility.maven.plugins.android.InclusionExclusionResolver.filterArtifacts;
1920
import static com.simpligility.maven.plugins.android.common.AndroidExtension.AAR;
2021
import static com.simpligility.maven.plugins.android.common.AndroidExtension.APKLIB;
2122

@@ -27,6 +28,7 @@
2728
import java.util.ArrayList;
2829
import java.util.List;
2930

31+
import com.simpligility.maven.plugins.android.IncludeExcludeSet;
3032
import org.apache.commons.io.FileUtils;
3133
import org.apache.commons.io.IOUtils;
3234
import org.apache.commons.lang3.StringUtils;
@@ -70,6 +72,11 @@ public class AarMojo extends AbstractAndroidMojo
7072
*/
7173
public static final String NATIVE_LIBRARIES_FOLDER = "jni";
7274

75+
/**
76+
* The name of the top level folder in the AAR where JAR libraries are found.
77+
*/
78+
public static final String LIBRARIES_FOLDER = "libs";
79+
7380
/**
7481
* <p>Classifier to add to the artifact generated. If given, the artifact will be an attachment instead.</p>
7582
*/
@@ -124,6 +131,57 @@ public class AarMojo extends AbstractAndroidMojo
124131
)
125132
private String obfuscatedJar;
126133

134+
/**
135+
* Set to {@code false} to automatically include all dependency JARs in the generated AAR. These are placed in
136+
* a directory called |code libs} in the generated aar. The set of JARs to include can be restricted using the
137+
* {@code artifactSet} and {@code artifactTypeSet} parameters. If {@code skipDependencies} is {@code true}
138+
* (default), only those JARs explicitly selected by the {@code artifactSet} and {@code artifactTypeSet} parameters
139+
* will be included.
140+
*/
141+
@Parameter( property = "skipDependencies", defaultValue = "true" )
142+
private boolean skipDependencies;
143+
144+
/**
145+
* Allows to include or exclude artifacts by type. The {@code include} parameter has higher priority than the
146+
* {@code exclude} parameter. These two parameters can be overridden by the {@code artifactSet} parameter. Empty
147+
* strings are ignored. Example:
148+
* <pre>
149+
* &lt;artifactTypeSet&gt;
150+
* &lt;includes&gt;
151+
* &lt;include&gt;aar&lt;/include&gt;
152+
* &lt;includes&gt;
153+
* &lt;excludes&gt;
154+
* &lt;exclude&gt;jar&lt;/exclude&gt;
155+
* &lt;excludes&gt;
156+
* &lt;/artifactTypeSet&gt;
157+
* </pre>
158+
*/
159+
@Parameter( property = "artifactTypeSet" )
160+
private IncludeExcludeSet artifactTypeSet;
161+
162+
/**
163+
* Allows to include or exclude artifacts by {@code groupId}, {@code artifactId}, and {@code versionId}. The
164+
* {@code include} parameter has higher priority than the {@code exclude} parameter. These two parameters can
165+
* override the {@code artifactTypeSet} and {@code skipDependencies} parameters. Artifact {@code groupId},
166+
* {@code artifactId}, and {@code versionId} are specified by a string with the respective values separated using
167+
* a colon character {@code :}. {@code artifactId} and {@code versionId} can be optional covering an artifact
168+
* range. Empty strings are ignored. Example:
169+
* <pre>
170+
* &lt;artifactTypeSet&gt;
171+
* &lt;includes&gt;
172+
* &lt;include&gt;foo-group:foo-artifact:1.0-SNAPSHOT&lt;/include&gt;
173+
* &lt;include&gt;bar-group:bar-artifact&lt;/include&gt;
174+
* &lt;include&gt;baz-group&lt;/include&gt;
175+
* &lt;includes&gt;
176+
* &lt;excludes&gt;
177+
* &lt;exclude&gt;qux-group:qux-artifact&lt;/exclude&gt;
178+
* &lt;excludes&gt;
179+
* &lt;/artifactTypeSet&gt;
180+
* </pre>
181+
*/
182+
@Parameter( property = "artifactSet" )
183+
private IncludeExcludeSet artifactSet;
184+
127185
private List<String> sourceFolders = new ArrayList<String>();
128186

129187
/**
@@ -295,6 +353,9 @@ protected File createAarLibraryFile( File classesJar ) throws MojoExecutionExcep
295353
// Lastly, add any native libraries
296354
addNativeLibraries( zipArchiver );
297355

356+
// ... and JAR libraries
357+
addLibraries( zipArchiver );
358+
298359
zipArchiver.createArchive();
299360
}
300361
catch ( ArchiverException e )
@@ -365,6 +426,18 @@ private void addNativeLibraries( final ZipArchiver zipArchiver ) throws MojoExec
365426
// TODO: - But where is that directory configured?
366427
}
367428

429+
private void addLibraries( final ZipArchiver zipArchiver ) throws MojoExecutionException
430+
{
431+
for ( Artifact artifact : filterArtifacts( getRelevantCompileArtifacts(), skipDependencies,
432+
artifactTypeSet.getIncludes(), artifactTypeSet.getExcludes(), artifactSet.getIncludes(),
433+
artifactSet.getExcludes() ) )
434+
{
435+
getLog().debug( "Include library in AAR :" + artifact );
436+
437+
zipArchiver.addFile( artifact.getFile(), LIBRARIES_FOLDER + "/" + artifact.getFile().getName() );
438+
}
439+
}
440+
368441
/**
369442
* Makes sure the string ends with "/"
370443
*

0 commit comments

Comments
 (0)