Using Maven to build Java & Android program

1,805 views

Published on

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

No Downloads
Views
Total views
1,805
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
53
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Using Maven to build Java & Android program

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

×