Maven & Android: complete pom.xml files contents

17 11 2012

The complete pom.xml file I use for Android projects is as follows.

1. Main Android application’s pom.xml

<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>pl.looksok</groupId>
	<artifactId>ColCalc</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>apk</packaging>

	<build>
		<sourceDirectory>${project.basedir}/src</sourceDirectory>
		<plugins>
			<plugin>
				<groupId>com.jayway.maven.plugins.android.generation2</groupId>
				<artifactId>android-maven-plugin</artifactId>
				<version>3.3.2</version>
				<configuration>
					<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
					<assetsDirectory>${project.basedir}/assets</assetsDirectory>
					<resourceDirectory>${project.basedir}/res</resourceDirectory>
					<genDirectory>${project.basedir}/gen</genDirectory>
					<deleteConflictingFiles>true</deleteConflictingFiles>
					<undeployBeforeDeploy>true</undeployBeforeDeploy>
					<sdk>
						<platform>7</platform>
					</sdk>
				</configuration>
				<extensions>true</extensions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.5.1</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<!-- java project dependency -->
		<dependency>
			<groupId>pl.looksok.logic</groupId>
			<artifactId>ColCalcLogic</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<!-- android apklib project dependency -->
		<dependency>
			<groupId>pl.looksok.currencyedittext</groupId>
			<artifactId>CurrencyEditText</artifactId>
			<version>0.0.1-SNAPSHOT</version>
			<type>apklib</type>
		</dependency>
	</dependencies>
</project>

Please remember to set target Android SDK and java version in file above. In my case Java version is 1.6, and SDK is 7. Keep the same config in AndroidManifest.xml.

2. apklibAndroid library project pom.xml file

<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>pl.looksok.currencyedittext</groupId>
  <artifactId>CurrencyEditText</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- set up packaging -->
  <packaging>apklib</packaging>
  <build>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
            <plugins>
                <plugin>
			<groupId>com.jayway.maven.plugins.android.generation2</groupId>
			<artifactId>android-maven-plugin</artifactId>
			<version>3.1.1</version>
			<configuration>
				<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
				<assetsDirectory>${project.basedir}/assets</assetsDirectory>
				<resourceDirectory>${project.basedir}/res</resourceDirectory>
				<genDirectory>${project.basedir}/gen</genDirectory>
				<deleteConflictingFiles>true</deleteConflictingFiles>
				<undeployBeforeDeploy>true</undeployBeforeDeploy>
				<sdk>
					<platform>7</platform>
				</sdk>
			</configuration>
			<extensions>true</extensions>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.5.1</version>
			<configuration>
				<source>1.6</source>
				<target>1.6</target>
			</configuration>
		</plugin>

        </plugins>

        <pluginManagement>
		<plugins>
		<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
		<plugin>
			<groupId>org.eclipse.m2e</groupId>
			<artifactId>lifecycle-mapping</artifactId>
			<version>1.0.0</version>
			<configuration>
				<lifecycleMappingMetadata>
				<pluginExecutions>
				<pluginExecution>
				<pluginExecutionFilter>
					<groupId>com.jayway.maven.plugins.android.generation2</groupId>
		  			<artifactId>android-maven-plugin</artifactId>
		  			<versionRange>[3.3.0,)</versionRange>
		  			<goals>
		  				<goal>proguard</goal>
		  			</goals>
		  		</pluginExecutionFilter>
		  		</pluginExecution>
		  		</pluginExecutions>
		  		</lifecycleMappingMetadata>
		  	</configuration>
		  </plugin>
		</plugins>
	</pluginManagement>
  </build>
</project>

The most important change to notice is the packaging tag at the beginning – set it to apklib.

Note that eclipse does not read maven apklib dependencies from pom.xml automatically. If you want your android project to use apklib project in eclipse, you have to manually add library in your main project’s settings:

Please note also, that your apklib project has to be marked as Android Library in its properties (check it in the same project properties window as shown above). Maven plugin however should automatically mark this project as library, based on its pom.xml file.

3. jar – standard java library’s pom.xml

<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>pl.looksok.logic</groupId>
  <artifactId>ColCalcLogic</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Just as a remainder – this is standard pom.xml for jar library

Summary

These files are operational, after performing all maven, m2eclipse and maven for android plugin configurations

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Use Maven with Android project

12 05 2012

In order to mavenize (existing or not) Android project in Eclipse few steps are required:

1. install m2eclipse plugin

All instructions how to do it, You will find here.

2. install ‘Android Configurator for M2E

in Eclipse go to menu:

Help -> Eclipse Marketplace

search for ‘Android Configurator for M2E’ and install it:

Here is plugin’s website. Any additional info will be provided there. Moreover here is very detailed description about Android development with Maven.

3. Install Maven 3.0.3

Android Configurator for m2e requires Maven in version 3.0.3 or higher, and m2eclipse comes with lower version. There is need to update it. Use download source site for Maven 3.0.4 and unzip it. Then in eclipse go to:

Window -> Preferences -> Maven -> Installations

and set path to downloaded files (click ‘Add‘):

4. Create pom.xml

Here is sample pom.xml from plugin’s website. The most important differences between this, and standard pom.xml are marked with green.

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.simpligility.android</groupId>
    <artifactId>helloflashlight</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>HelloFlashlight</name>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>1.6_r2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <sourceDirectory>src</sourceDirectory>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <version>3.2.0</version>
                    <extensions>true</extensions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <configuration>
                    <sdk>
                        <!-- platform or api level (api level 4 = platform 1.6)-->
                        <platform>4</platform>
                    </sdk>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

If you need some more info on basic configuration of pom.xml for m2eclipse, look  here.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Basic Maven (m2eclipse / pom.xml) configuration

5 05 2012

Introducing Maven to Eclipse projects from scratch was described in my previous post. Now the basic usage and configuration will be described.

1a. Addind external jars (Maven dependencies)
To add some external jar file or reference to another workspace project you will need to:

Right Click on project -> Maven -> Add dependency

You can add it by filling in groupId, version etc. manually or search Maven repository by keyowrds. For example searching for JSON lib looks like in the picture below. After confirm, you will see changes reflected in pom.xml file.


1b. Another workspace project dependency
It is also possible to refer to another project, however it also has to be the Maven one (has its own pom.xml). Some instructions can be found here. Sample snippet of dependency in pom.xml is:

<dependency>
  <groupId>com.maventest</groupId>
  <artifactId>mytest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>compile</scope>
</dependency>

2. Setting the src folder
The issue after adding Maven nature to project in my case was Eclipse missing the source folder in project. src was present in workspace, however it was not recognized as source folder. The solution is to add following lines in pom.xml file (if more info is needed look at this source):

 <build>
   <sourceDirectory>${basedir}/src</sourceDirectory>
 </build>

Note: Setting source folder by eclipse standard way (Build path -> Configure Build Path) will have no effect, because it will be reverted after Updating Maven Configuration.

3. Set jre for project
If you want to use specific Java version in Maven project, you can use Maven plugin: compiler-plugin, as described here. It’s easy and requires only adding this entry to pom.xml file:

<plugins>
  ..
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>
</plugins>

4. Problem with selecting targets for Maven builds
If You have problem with selecting targets (empty goals list), see solution here and corresponding bug description here.

It did not help me :D However solution makes sense and worth to try it anyway :)

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Incorporate Maven into Eclipse project

28 04 2012

Here is short instruction how to incorporate Maven into existing Eclipse projects from scratch, step by step. The goal of Maven introduction presented here is quite generic, however I installed Maven to manage dependencies (jars). Maven build, installs or clean functions are not used by me. If You need to know more about maven itself - this screencast is good start.

1. Install eclipse plugin for maven
I recommend m2eclipse as it is most popular and works quite well. It is supported by Eclipse: 3.6 Galileo and 3.7 Indigo. Install it by ‘Install new software‘ Eclipse option. Update site link you will find on this site.

This will install its own Maven instance, so you don’t need to install Maven yourself – m2eclipse will handle it. However manual Maven installation is necessary if You need another version that is provided in m2eclipse by default.

Some very good introduction about m2eclipse is here. If m2eclipse doesn’t suit you, then you can find another plugins comparison here. The best one decided on StackOverflow.com is however m2eclipse.

2. Update existing project with maven functionality
(If You are creating new project use Eclipse -> New Project and choose type: Maven. Params to enter are the same as described below)

To create pom.xml file and tell to m2eclipse to use this file:

Right click on project -> configure -> Convert to maven project

On the project configuration dialog You need to fill in these fields:

  • groupId – in fact this is main package name that is used in eclipse project. Param states that project belongs to some group, pointed by package name.
  • artifactId – project name that is used in Eclipse. This will be the name of the file that will be produced by Maven after compilation (e.g. name of external jar)
  • archetype – project type. This is similar to creating new project in Eclipse, where You choose on of the project types at the beginning (Android project / Java project / Dynamic Web Project etc.). Like Eclipse project types, Maven provides its archetypes. Choosing proper one, eases project configuration. Maven preconfigures its structure and pom.xml file.
  • dependencies – list of all jars or another workspace projects required for Mavenized project to run. Referenced workspace projects should also be maven ones.

3. Updating Maven project configuration
After making changes in Maven config (pom.xml file) you will have to update project configuration to reflect these changes in project.
Do it by:

Right Click on th project -> Maven -> Update project configuration

4. That’s all

On another computer, where this project is used it is enough to install m2eclipse (point 1.) and trigger ‘Update Project Configuration’ (point 3.).

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!








%d bloggers like this: