Your SlideShare is downloading. ×
Using Maven to build Java & Android program
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Using Maven to build Java & Android program

1,146

Published on

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,146
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
41
Comments
0
Likes
7
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Using Maven to build Java & Android program Kewang
  • 2. 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
  • 3. 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
  • 4. Maven
  • 5. 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
  • 6. Maven can manage ... ● Builds ● Documentation ● Reporting ● Dependencies ● SCMs ● Releases ● Distribution 6
  • 7. Maven's concepts ● Project Object Model ● Convention over Configuration (CoC) ● Dependency management ● Plugin-based architecture 7
  • 8. Project Object Model 8
  • 9. 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
  • 10. Convention over Configuration 10
  • 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 11
  • 12. 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
  • 13. Dependency management 13
  • 14. Dependency management <dependencies> <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>2.3.3_r2</version> <scope>provided</scope> </dependency> </dependencies> 14
  • 15. 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
  • 16. Plugin-based architecture 16
  • 17. 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
  • 18. Maven lifecycle & commands
  • 19. Maven lifecycle 19
  • 20. 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
  • 21. Build a simple Java project
  • 22. Step by step ● Normalize directory structure ● Write pom.xml ● Run it 22
  • 23. Normalize directory structure 23
  • 24. Or maven-archetype-quickstart mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 24
  • 25. 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
  • 26. mvn clean compile 26
  • 27. mvn clean compile only compiled class 27
  • 28. mvn clean package 28
  • 29. mvn clean package include test file, jar file... 29
  • 30. but it can't run ...
  • 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> </build> 31
  • 32. 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
  • 33. Live DEMO
  • 34. Apply Maven to Android
  • 35. Project directory structure 35
  • 36. Step by step ● Install Android SDK ● Run Maven Android SDK Deployer ● Write pom.xml ● Run it 36
  • 37. Install Android SDK, at least ... ● All versions SDK Platform ● All versions Google APIs ● All Extra items (including obsoleted) ● Glass Development Kit Sneak Peek 37
  • 38. Run Maven Android SDK Deployer ● Checkout mosabua/maven-android-sdk-deployer ● mvn install 38
  • 39. Run Maven Android SDK Deployer ● Checkout mosabua/maven-android-sdk-deployer ● mvn install copy all jar files to maven repository 39
  • 40. Use Android libraries like ... <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>4.4_r1</version> <scope>provided</scope> </dependency> 40
  • 41. 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
  • 42. Use Android libraries like ... <dependency> <groupId>com.google.android.maps</groupId> <artifactId>maps</artifactId> <version>19_r1</version> <scope>provided</scope> </dependency> 42
  • 43. Use Android libraries like ... <dependency> <groupId>android.support</groupId> <artifactId>compatibility-v13</artifactId> <version>19.0.1</version> </dependency> 43
  • 44. Write pom.xml
  • 45. <project> tags <packaging>apk</packaging> 45
  • 46. <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
  • 47. <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
  • 48. mvn clean package 48
  • 49. mvn clean package generates R.java 49
  • 50. mvn android:deploy android:run ● mvn android:deploy – ● Install app to device mvn android:run – Run app at device 50
  • 51. Live DEMO
  • 52. android-maven-plugin FAQ 52
  • 53. android-maven-plugin FAQ ● No Android SDK path could be found. 53
  • 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 54
  • 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 55
  • 56. 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
  • 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 57
  • 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" ● ● set ANDROID_HOME env. to Android SDK path <extensions>true</extensions> package R does not exist – <packaging>jar</packaging> ● <packaging>apk</packaging> 58
  • 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 59
  • 60. 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
  • 61. Eclipse tips IMPORTANT !!!
  • 62. Tips - How to use src folder ? 62
  • 63. Tips - How to use src folder ? <build> <sourceDirectory>src</sourceDirectory> </build> 63
  • 64. Tips - How to use libs folder ? 64
  • 65. 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
  • 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 mvn install:install-file -Dfile=flappybird.jar -DgroupId=tw.kewang -DartifactId=flappybird -Dversion=1.0 -Dpackaging=jar 66
  • 67. 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
  • 68. Tips - How to use gen folder ? 68
  • 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> 69
  • 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> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <configuration> <filesets> <fileset> <directory>gen</directory> </fileset> </filesets> </configuration> </plugin> 70
  • 71. 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
  • 72. 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
  • 73. Tips - How to use library project ? 73
  • 74. Tips - How to use library project ? ● Write pom.xml at library project as general Android project 74
  • 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 75
  • 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 76
  • 77. 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
  • 78. Tips - How to use non-maven library ? ● Compress non-maven library to zip file 78
  • 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 79
  • 80. 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
  • 81. Show Time github://kewangtw
  • 82. Show Time - google-tts tts.say(sentence, true, new OnFinishListener() { @Override public void onFinish() { Toast.makeText(this, sentence, Toast.LENGTH_SHORT).show(); } }); 82
  • 83. 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
  • 84. 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
  • 85. 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
  • 86. References ● Apache Maven Project ● Android Application Development with Maven ● Android Maven Plugin ● Maven Android SDK Deployer 86
  • 87. 87

×