5. Maven Dependency Management

5.1 Introduction

An alternative to downloading the individual library jars yourself is to use Maven for dependency management. The Android Maven Plugin allows developers to utilize Maven's dependency management capabilities within an Android application. Additionally, the Android Configurator for M2E bridges the Maven Android Plugin and the Android Development Tools (ADT) to allow the use of dependency management within Eclipse.

5.2 SpringSource Repository

The following repositories are available for all SpringSource projects. Much more information is available at the SpringSource Repository FAQ.

Release versions, such as 1.0.0.RELEASE, are available through Maven Central or via the SpringSource Repository:

<repository>
    <id>springsource-repo</id>
    <name>SpringSource Repository</name>
    <url>http://repo.springsource.org/release</url>
</repository>
		

If you are developing against a milestone version, such as 1.0.0.RC2, you will need to add the following repository in order to resolve the artifact:

<repository>
    <id>springsource-milestone</id>
    <name>SpringSource Milestone Repository</name>
    <url>http://repo.springsource.org/milestone</url>
</repository>
		

If you are testing out the latest nightly build version (e.g. 1.0.1.BUILD-SNAPSHOT), you will need to add the following repository:

<repository>
    <id>springsource-snapshot</id>
    <name>SpringSource Snapshot Repository</name>
    <url>http://repo.springsource.org/snapshot</url>
</repository>
		

5.3 Example POM

The following Maven POM file from the Spring for Android Showcase sample application, illustrates how to configure the Maven Android Plugin and associated dependencies for use with Spring for Android and Spring Social.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework.android</groupId>
    <artifactId>spring-android-showcase-client</artifactId>
    <version>1.0.1.BUILD-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>spring-android-showcase-client</name>
    <url>http://www.springsource.org/spring-android</url>
    <inceptionYear>2010</inceptionYear>
    <organization>
        <name>SpringSource</name>
        <url>http://www.springsource.org</url>
    </organization>

    <properties>
        <android-platform>16</android-platform>
        <android-maven-plugin-version>3.4.0</android-maven-plugin-version>
        <maven-compiler-plugin-version>2.5.1</maven-compiler-plugin-version>
        <java-version>1.6</java-version>
        <maven-eclipse-plugin-version>2.8</maven-eclipse-plugin-version>
        <com.google.android-version>4.1.1.4</com.google.android-version>
        <!-- Available Android versions: 1.5_r3, 1.5_r4, 1.6_r2, 2.1.2, 2.1_r1, 
            2.2.1, 2.3.1, 2.3.3, 4.0.1.2, 4.1.1.4 -->
        <org.springframework.android-version>1.0.1.BUILD-SNAPSHOT</org.springframework.android-version>
        <com.fasterxml.jackson-version>2.1.2</com.fasterxml.jackson-version>
        <com.google.code.gson-version>2.2.2</com.google.code.gson-version>
        <org.simpleframework-version>2.6.9</org.simpleframework-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>${com.google.android-version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.android</groupId>
            <artifactId>spring-android-rest-template</artifactId>
            <version>${org.springframework.android-version}</version>
        </dependency>
        
        <!-- Jackson JSON Processor -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${com.fasterxml.jackson-version}</version>
        </dependency>
        
        <!-- Gson JSON Processor -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${com.google.code.gson-version}</version>
        </dependency>
        
        <!-- Simple XML Processor -->
        <dependency>
            <groupId>org.simpleframework</groupId>
            <artifactId>simple-xml</artifactId>
            <version>${org.simpleframework-version}</version>
            <exclusions>
                <!-- StAX is not available on Android -->
                <exclusion>
                    <artifactId>stax</artifactId>
                    <groupId>stax</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>stax-api</artifactId>
                    <groupId>stax</groupId>
                </exclusion>
                <!-- Provided by Android -->
                <exclusion>
                    <artifactId>xpp3</artifactId>
                    <groupId>xpp3</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-snapshot</id>
            <name>SpringSource Snapshot Repository</name>
            <url>http://repo.springsource.org/libs-snapshot</url>
        </repository>
    </repositories>

    <build>
        <finalName>${project.artifactId}</finalName>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>${android-maven-plugin-version}</version>
                <configuration>
                    <sdk>
                        <platform>${android-platform}</platform>
                    </sdk>
                    <deleteConflictingFiles>true</deleteConflictingFiles>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                </configuration>
                <extensions>true</extensions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin-version}</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>${maven-eclipse-plugin-version}</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
	

5.4 Maven Commands

Once you have configured a Maven POM in your Android project you can use the following Maven command to clean and assemble your Android APK file.

$ mvn clean install
	

The Android Maven Plugin provides several goals for use in building and deploying your application. You can configure a specific emulator in the plugin configuration, or if you omit the emulator name, the plugin will attempt to execute the specified goal on all available emulators and devices.

The following command starts the emulator specified in the Maven Android Plugin section of the POM file. If no emulator name is configured, then the plugin attempts to start an AVD with the name of Default.

$ mvn android:emulator-start
	

Deploys the built apk file to the emulator or attached device.

$ mvn android:deploy
	

Starts the app on the emulator or attached device.

$ mvn android:run
	

Displays a list of help topics for the Android Maven Plugin.

$ mvn android:help