Skip to content

Commit

Permalink
Merge pull request simpligility#803 from danielbobbert/add_dependenci…
Browse files Browse the repository at this point in the history
…es_to_aar

Add options to include dependencies (JARs) into the generated AAR
  • Loading branch information
mosabua authored Jan 18, 2022
2 parents cca60c5 + 12e1da0 commit 53589de
Showing 1 changed file with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.simpligility.maven.plugins.android.phase09package;

import static com.simpligility.maven.plugins.android.InclusionExclusionResolver.filterArtifacts;
import static com.simpligility.maven.plugins.android.common.AndroidExtension.AAR;
import static com.simpligility.maven.plugins.android.common.AndroidExtension.APKLIB;

Expand All @@ -27,6 +28,7 @@
import java.util.ArrayList;
import java.util.List;

import com.simpligility.maven.plugins.android.IncludeExcludeSet;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -70,6 +72,11 @@ public class AarMojo extends AbstractAndroidMojo
*/
public static final String NATIVE_LIBRARIES_FOLDER = "jni";

/**
* The name of the top level folder in the AAR where JAR libraries are found.
*/
public static final String LIBRARIES_FOLDER = "libs";

/**
* <p>Classifier to add to the artifact generated. If given, the artifact will be an attachment instead.</p>
*/
Expand Down Expand Up @@ -124,6 +131,57 @@ public class AarMojo extends AbstractAndroidMojo
)
private String obfuscatedJar;

/**
* Set to {@code false} to automatically include all dependency JARs in the generated AAR. These are placed in
* a directory called |code libs} in the generated aar. The set of JARs to include can be restricted using the
* {@code artifactSet} and {@code artifactTypeSet} parameters. If {@code skipDependencies} is {@code true}
* (default), only those JARs explicitly selected by the {@code artifactSet} and {@code artifactTypeSet} parameters
* will be included.
*/
@Parameter( property = "skipDependencies", defaultValue = "true" )
private boolean skipDependencies;

/**
* Allows to include or exclude artifacts by type. The {@code include} parameter has higher priority than the
* {@code exclude} parameter. These two parameters can be overridden by the {@code artifactSet} parameter. Empty
* strings are ignored. Example:
* <pre>
* &lt;artifactTypeSet&gt;
* &lt;includes&gt;
* &lt;include&gt;aar&lt;/include&gt;
* &lt;includes&gt;
* &lt;excludes&gt;
* &lt;exclude&gt;jar&lt;/exclude&gt;
* &lt;excludes&gt;
* &lt;/artifactTypeSet&gt;
* </pre>
*/
@Parameter( property = "artifactTypeSet" )
private IncludeExcludeSet artifactTypeSet;

/**
* Allows to include or exclude artifacts by {@code groupId}, {@code artifactId}, and {@code versionId}. The
* {@code include} parameter has higher priority than the {@code exclude} parameter. These two parameters can
* override the {@code artifactTypeSet} and {@code skipDependencies} parameters. Artifact {@code groupId},
* {@code artifactId}, and {@code versionId} are specified by a string with the respective values separated using
* a colon character {@code :}. {@code artifactId} and {@code versionId} can be optional covering an artifact
* range. Empty strings are ignored. Example:
* <pre>
* &lt;artifactTypeSet&gt;
* &lt;includes&gt;
* &lt;include&gt;foo-group:foo-artifact:1.0-SNAPSHOT&lt;/include&gt;
* &lt;include&gt;bar-group:bar-artifact&lt;/include&gt;
* &lt;include&gt;baz-group&lt;/include&gt;
* &lt;includes&gt;
* &lt;excludes&gt;
* &lt;exclude&gt;qux-group:qux-artifact&lt;/exclude&gt;
* &lt;excludes&gt;
* &lt;/artifactTypeSet&gt;
* </pre>
*/
@Parameter( property = "artifactSet" )
private IncludeExcludeSet artifactSet;

private List<String> sourceFolders = new ArrayList<String>();

/**
Expand Down Expand Up @@ -295,6 +353,9 @@ protected File createAarLibraryFile( File classesJar ) throws MojoExecutionExcep
// Lastly, add any native libraries
addNativeLibraries( zipArchiver );

// ... and JAR libraries
addLibraries( zipArchiver );

zipArchiver.createArchive();
}
catch ( ArchiverException e )
Expand Down Expand Up @@ -365,6 +426,18 @@ private void addNativeLibraries( final ZipArchiver zipArchiver ) throws MojoExec
// TODO: - But where is that directory configured?
}

private void addLibraries( final ZipArchiver zipArchiver ) throws MojoExecutionException
{
for ( Artifact artifact : filterArtifacts( getRelevantCompileArtifacts(), skipDependencies,
artifactTypeSet.getIncludes(), artifactTypeSet.getExcludes(), artifactSet.getIncludes(),
artifactSet.getExcludes() ) )
{
getLog().debug( "Include library in AAR :" + artifact );

zipArchiver.addFile( artifact.getFile(), LIBRARIES_FOLDER + "/" + artifact.getFile().getName() );
}
}

/**
* Makes sure the string ends with "/"
*
Expand Down

0 comments on commit 53589de

Please sign in to comment.