advertisement

Using Maven to build Java & Android program

50 %
50 %
advertisement
Information about Using Maven to build Java & Android program
Technology

Published on February 26, 2014

Author: kewang

Source: slideshare.net

advertisement

Using Maven to build Java & Android program Kewang

Install Maven ● Download from [HERE] (http://maven.apache.org/download.cgi) – ● Or apt-get / yum / brew ...... Add env. – – ● M2_HOME=/opt/maven M2=$M2_HOME/bin Run mvn --version – verify mvn is installed correctly. 2

About me ● 王慕羣 (Kewang) ● Skills – – AWS (EC2, S3, DynamoDB) – Hadoop (HBase, MapReduce) – ● Android Node.js, Rails Company – ● Mitake Inc. Others – Google Play, GitHub, slideshare, Scribd 3

Maven

Maven's objectives ● Making the build process easy ● Providing a uniform build system ● Providing quality project information ● Providing guidelines for best practices development ● Allowing transparent migration to new features 5

Maven can manage ... ● Builds ● Documentation ● Reporting ● Dependencies ● SCMs ● Releases ● Distribution 6

Maven's concepts ● Project Object Model ● Convention over Configuration (CoC) ● Dependency management ● Plugin-based architecture 7

Project Object Model 8

Project Object Model <project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>my-app</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> 9

Convention over Configuration 10

Convention over Configuration <sourceDirectory>/tmp/tts/src</sourceDirectory> <scriptSourceDirectory>/tmp/tts/src/main/scripts</scriptSourceDirectory> <testSourceDirectory>/tmp/tts/src/test/java</testSourceDirectory> <outputDirectory>/tmp/tts/target/classes</outputDirectory> <testOutputDirectory>/tmp/tts/target/test-classes</testOutputDirectory> <resources> <resource> <directory>/tmp/tts/src/main/resources</directory> </resource> </resources> <testResources> <testResource> <directory>/tmp/tts/src/test/resources</directory> </testResource> </testResources> mvn help:effective-pom 11

Convention over Configuration <sourceDirectory>/tmp/tts/src</sourceDirectory> <scriptSourceDirectory>/tmp/tts/src/main/scripts</scriptSourceDirectory> <testSourceDirectory>/tmp/tts/src/test/java</testSourceDirectory> <outputDirectory>/tmp/tts/target/classes</outputDirectory> <testOutputDirectory>/tmp/tts/target/test-classes</testOutputDirectory> <resources> <resource> <directory>/tmp/tts/src/main/resources</directory> </resource> </resources> <testResources> <testResource> <directory>/tmp/tts/src/test/resources</directory> </testResource> </testResources> mvn help:effective-pom 12

Dependency management 13

Dependency management <dependencies> <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>2.3.3_r2</version> <scope>provided</scope> </dependency> </dependencies> 14

Dependency management <dependencies> <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>2.3.3_r2</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.squareup.picasso</groupId> <artifactId>picasso</artifactId> <version>2.2.0</version> </dependency> </dependencies> 15

Plugin-based architecture 16

Plugin-based architecture <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <mainClass>com.mycompany.example.App</mainClass> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> 17

Maven lifecycle & commands

Maven lifecycle 19

Maven commands ● mvn clean – ● mvn compile – ● Package compiled code to distributable format. mvn install – ● Run tests using a suitable unit testing framework. mvn package – ● Compile the source code of the project. mvn test – ● Remove all previous build generated files. Install the package into the local repository. mvn deploy – Copy the final package to the remote repository. 20

Build a simple Java project

Step by step ● Normalize directory structure ● Write pom.xml ● Run it 22

Normalize directory structure 23

Or maven-archetype-quickstart mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 24

Write pom.xml <project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-app</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>my-app</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> 25

mvn clean compile 26

mvn clean compile only compiled class 27

mvn clean package 28

mvn clean package include test file, jar file... 29

but it can't run ...

mvn clean compile exec:java <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <mainClass>com.mycompany.example.App</mainClass> </configuration> </plugin> </plugins> </build> 31

mvn clean compile exec:java <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <mainClass>com.mycompany.example.App</mainClass> </configuration> </plugin> </plugins> remember assign <mainClass/> </build> 32

Live DEMO

Apply Maven to Android

Project directory structure 35

Step by step ● Install Android SDK ● Run Maven Android SDK Deployer ● Write pom.xml ● Run it 36

Install Android SDK, at least ... ● All versions SDK Platform ● All versions Google APIs ● All Extra items (including obsoleted) ● Glass Development Kit Sneak Peek 37

Run Maven Android SDK Deployer ● Checkout mosabua/maven-android-sdk-deployer ● mvn install 38

Run Maven Android SDK Deployer ● Checkout mosabua/maven-android-sdk-deployer ● mvn install copy all jar files to maven repository 39

Use Android libraries like ... <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>4.4_r1</version> <scope>provided</scope> </dependency> 40

Use Android libraries like ... <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>4.4_r1</version> <scope>provided</scope> </dependency> "provided" stands for only compile, not package itself 41

Use Android libraries like ... <dependency> <groupId>com.google.android.maps</groupId> <artifactId>maps</artifactId> <version>19_r1</version> <scope>provided</scope> </dependency> 42

Use Android libraries like ... <dependency> <groupId>android.support</groupId> <artifactId>compatibility-v13</artifactId> <version>19.0.1</version> </dependency> 43

Write pom.xml

<project> tags <packaging>apk</packaging> 45

<dependencies> tags <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>4.0_r3</version> <scope>provided</scope> </dependency> <dependency> <groupId>android.support</groupId> <artifactId>compatibility-v4</artifactId> <version>19.0.1</version> </dependency> 46

<plugins> tags <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <version>3.8.1</version> <extensions>true</extensions> <configuration> <sdk> <platform>14</platform> </sdk> </configuration> </plugin> 47

mvn clean package 48

mvn clean package generates R.java 49

mvn android:deploy android:run ● mvn android:deploy – ● Install app to device mvn android:run – Run app at device 50

Live DEMO

android-maven-plugin FAQ 52

android-maven-plugin FAQ ● No Android SDK path could be found. 53

android-maven-plugin FAQ ● No Android SDK path could be found. – no ANDROID_HOME env., or value is incorrect ● set ANDROID_HOME env. to Android SDK path 54

android-maven-plugin FAQ ● No Android SDK path could be found. – no ANDROID_HOME env., or value is incorrect ● ● set ANDROID_HOME env. to Android SDK path Unknown packaging: apk 55

android-maven-plugin FAQ ● No Android SDK path could be found. – no ANDROID_HOME env., or value is incorrect ● ● set ANDROID_HOME env. to Android SDK path Unknown packaging: apk – no extensions tag, or value is not "true" ● <extensions>true</extensions> 56

android-maven-plugin FAQ ● No Android SDK path could be found. – no ANDROID_HOME env., or value is incorrect ● ● Unknown packaging: apk – no extensions tag, or value is not "true" ● ● set ANDROID_HOME env. to Android SDK path <extensions>true</extensions> package R does not exist 57

android-maven-plugin FAQ ● No Android SDK path could be found. – no ANDROID_HOME env., or value is incorrect ● ● Unknown packaging: apk – no extensions tag, or value is not "true" ● ● set ANDROID_HOME env. to Android SDK path <extensions>true</extensions> package R does not exist – <packaging>jar</packaging> ● <packaging>apk</packaging> 58

android-maven-plugin FAQ ● No Android SDK path could be found. – no ANDROID_HOME env., or value is incorrect ● ● Unknown packaging: apk – no extensions tag, or value is not "true" ● ● <extensions>true</extensions> package R does not exist – <packaging>jar</packaging> ● ● set ANDROID_HOME env. to Android SDK path <packaging>apk</packaging> Ill-advised or mistaken usage of a core class 59

android-maven-plugin FAQ ● No Android SDK path could be found. – no ANDROID_HOME env., or value is incorrect ● ● Unknown packaging: apk – no extensions tag, or value is not "true" ● ● <extensions>true</extensions> package R does not exist – <packaging>jar</packaging> ● ● set ANDROID_HOME env. to Android SDK path <packaging>apk</packaging> Ill-advised or mistaken usage of a core class – no scope tag, or value is not "provided" ● <scope>provided</scope> 60

Eclipse tips IMPORTANT !!!

Tips - How to use src folder ? 62

Tips - How to use src folder ? <build> <sourceDirectory>src</sourceDirectory> </build> 63

Tips - How to use libs folder ? 64

Tips - How to use libs folder ? ● mvn install:install-file – -Dfile=<path-to-file> – -DgroupId=<group-id> – -DartifactId=<artifact-id> – -Dversion=<version> – -Dpackaging=jar 65

Tips - How to use libs folder ? ● mvn install:install-file – – -DgroupId=<group-id> – -DartifactId=<artifact-id> – -Dversion=<version> – ● -Dfile=<path-to-file> -Dpackaging=jar mvn install:install-file -Dfile=flappybird.jar -DgroupId=tw.kewang -DartifactId=flappybird -Dversion=1.0 -Dpackaging=jar 66

Tips - How to use libs folder ? ● mvn install:install-file – – -DgroupId=<group-id> – -DartifactId=<artifact-id> – -Dversion=<version> – ● -Dfile=<path-to-file> -Dpackaging=jar copy jar file to maven repository mvn install:install-file -Dfile=flappybird.jar -DgroupId=tw.kewang -DartifactId=flappybird -Dversion=1.0 -Dpackaging=jar 67

Tips - How to use gen folder ? 68

Tips - How to use gen folder ? <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <configuration> <genDirectory>gen</genDirectory> </configuration> </plugin> 69

Tips - How to use gen folder ? <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <configuration> <genDirectory>gen</genDirectory> </configuration> </plugin> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <configuration> <filesets> <fileset> <directory>gen</directory> </fileset> </filesets> </configuration> </plugin> 70

Tips - How to use gen folder ? <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <configuration> <genDirectory>gen</genDirectory> </configuration> </plugin> remember clean gen folder <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <configuration> <filesets> <fileset> <directory>gen</directory> </fileset> </filesets> </configuration> </plugin> 71

Tips - How to clean bin folder ? <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <configuration> <filesets> <fileset> <directory>bin</directory> </fileset> </filesets> </configuration> </plugin> 72

Tips - How to use library project ? 73

Tips - How to use library project ? ● Write pom.xml at library project as general Android project 74

Tips - How to use library project ? ● ● Write pom.xml at library project as general Android project mvn clean android:apklib – Apklib file includes src/main/java, libs & res directory 75

Tips - How to use library project ? ● ● Write pom.xml at library project as general Android project mvn clean android:apklib – ● Apklib file includes src/main/java, libs & res directory Use install goal to copy apklib to maven repository 76

Tips - How to use library project ? ● ● Write pom.xml at library project as general Android project mvn clean android:apklib – Apklib file includes src/main/java, libs & res directory ● Use install goal to copy apklib to maven repository ● Add dependency at main project <dependency> <groupId>tw.kewang.ui.captchaview</groupId> <artifactId>CaptchaView</artifactId> <version>1.0.0</version> <type>apklib</type> </dependency> 77

Tips - How to use non-maven library ? ● Compress non-maven library to zip file 78

Tips - How to use non-maven library ? ● Compress non-maven library to zip file ● Use install goal to copy zip to maven repository – packaging=apklib 79

Tips - How to use non-maven library ? ● Compress non-maven library to zip file ● Use install goal to copy zip to maven repository – ● packaging=apklib Add dependency at main project <dependency> <groupId>tw.kewang.ui.captchaview</groupId> <artifactId>CaptchaView</artifactId> <version>1.0.0</version> <type>apklib</type> </dependency> 80

Show Time github://kewangtw

Show Time - google-tts tts.say(sentence, true, new OnFinishListener() { @Override public void onFinish() { Toast.makeText(this, sentence, Toast.LENGTH_SHORT).show(); } }); 82

Show Time - map-controller mc.startTrackMyLocation(new ChangeMyLocation() { @Override public void changed(GoogleMap map, Location location) { Log.d("TRACK", location.toString()); } }); mc.whenMapClick(new ClickCallback() { @Override public void clicked(GoogleMap map, LatLng latLng) { Log.d("CLICK", latLng.toString()); } }); 83

Show Time - CaptchaView captcha.setPattern("1234567890"); captcha.setLength(6); captcha.refresh(); if (captcha.getCaptcha().equals(check)) { Log.d("CAPTCHA", "Success"); } else { Log.d("CAPTCHA", "Fail"); } 84

Show Time - tile <tw.kewang.tile.Tile> <tw.kewang.tile.TileGroup tile:maxCells="3" tile:x="450dp" tile:y="50dp" > <tw.kewang.tile.TileCell tile:x="50dp" tile:y="50dp" /> <tw.kewang.tile.TileCell tile:x="100dp" tile:y="150dp" /> </tw.kewang.tile.TileGroup> </tw.kewang.tile.Tile> tile2.setOnCellLongClickListener(new OnLongClickListener() { @Override public void onLongClick(TileGroup group, TileCell cell) { tile2.startDrag(); } }); 85

References ● Apache Maven Project ● Android Application Development with Maven ● Android Maven Plugin ● Maven Android SDK Deployer 86

87

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Using Maven to build Java & Android program - YouTube

簡介 Maven 是一套強大的 Java 自動編譯工具,我們可以 ... Using Maven to build Java & Android program Taipei GTUG. ...
Read more

Maven – Maven in 5 Minutes

Maven / Maven in 5 Minutes; Last ... Inc. Java home: C:Program FilesJavajdk1.6.0_25jre ... to Configuring Maven if necessary. If you are using ...
Read more

Getting Started · Building Java Projects with Maven

Building Java Projects with Maven. This guide walks you through using Maven to build a simple Java ... need to setup a Java project for Maven to build.
Read more

Maven for building Java applications – Tutorial

This tutorial describes the usage of Maven for building Java ... Running com.vogella.build.maven.java ... Android, Git, Java, ...
Read more

java - How to build a runnable JavaFX application using ...

How to build a runnable JavaFX application using maven? ... I prefer using Maven, but I have no idea how to build it. ... Program FilesJavajre1.8.0. 0.
Read more

Using Maven to build Java & Android program - Technology

1. Using Maven to build Java & Android program Kewang ; 2. Install ...
Read more

Packaging of a Standalone Java program built using Maven ...

I have a standalone java program which is built using Maven. ... Packaging of a Standalone Java program built using Maven ... Another goal called "build ...
Read more

Java Build Automation with Maven - lynda.com

This course introduces Maven for build automation in Java, ... you to the process of using Apache Maven as a build ... sample program 8m 1s ...
Read more

Maven – Welcome to Apache Maven

Welcome to Apache Maven. ... Maven can manage a project's build, ... Information for those who may or may not be using Maven, ...
Read more