Android之Maven整合Ant多渠道打包

來源:互聯網
上載者:User

      在網上找了這方面的東西,貌似比較少,對maven也不是太瞭解,所以就想了個比較笨的辦法,即用ant執行mvn命令來執行pom.xml檔案,

maven只負責做打包和簽名,其它的多渠道操作都是ant中處理。

ant.xml部分做迴圈的操作:

             1. 檔案替換

             2. pom.xml中<appName>標籤的修改

             3. 執行mvn命令,打apk包及簽名

Ant核心代碼:

<?xml version="1.0" encoding="utf-8"?><project name="test" basedir="." default="run" xmlns:artifact="urn:maven-artifact-ant"><!-- ant build的引用 --><property file="project.properties" /><property file="ant.properties" /><property file="build.properties" /><!-- SDK目錄(擷取作業系統環境變數env.ANDROID_SDK_HOME的值) --><property name="sdk-folder" value="D:/Program Files/android-sdk-windows" /><property name="antcontrib_jar_path" value="C:/Users/Win7/Desktop/MavenAPK/ant-contrib-1.0b3.jar" /><property name="appName" value="AppName" /><property name="assetKey" value="${ant.contrib.asset.key}" /><property name="nowpath" value="${assetKey}" /><!-- 渠道名:渠道號 --><property name="assetPathKey" value="${nowpath}" /><property name="classPath" value="a.b.ControlAnt" /><property name="javacPath" value="a/b/ControlAnt.java" /><property name="mavenPath" value="${M2_HOME}/bin" /><!-- 使用第三方的ant包,使ant支援for迴圈--><taskdef resource="net/sf/antcontrib/antcontrib.properties"><classpath><pathelement location="${antcontrib_jar_path}" /></classpath></taskdef><!-- SDK指定平台目錄 --><property name="sdk-platform-folder" value="${sdk-folder}/platforms/android-4" /><!-- SDK中tools目錄 --><property name="sdk-tools" value="${sdk-folder}/tools" /><!-- SDK指定平台中tools目錄 --><property name="sdk-platform-tools" value="${sdk-platform-folder}/tools" /><!-- 編譯需要的jar; 如果項目使用到地圖服務則需要maps.jar --><property name="android-jar" value="${sdk-platform-folder}/android.jar" /><!-- 編譯後的檔案放置目錄 --><property name="outdir-target" value="target" /><!-- 資訊清單檔 --><property name="manifest-xml" value="AndroidManifest.xml" /><!-- 源檔案目錄 --><property name="resource-dir" value="res" /><property name="asset-dir" value="assets" /><!-- java源檔案目錄 --><property name="srcdir" value="src" /><property name="srcdir-ospath" value="${basedir}/${srcdir}" /><!-- 外部類庫所在目錄 --><property name="external-lib" value="libs" /><property name="external-lib-ospath" value="${basedir}/${external-lib}" /><!-- 產生class目錄 --><property name="outdir-classes-ospath" value="${basedir}/${outdir-target}" /><!-- 替換檔案key : value --><property name="assetchannelname" value="" /><property name="assetchannelvalue" value="" /><!-- 迴圈修改路徑名 --><target name="run"><foreach target="update_replace_path" list="${assetKey}" param="assetchannel" delimiter=","></foreach></target><!-- 修改nowpath,並根據nowpath替換檔案、打包 --><target name="update_replace_path"><echo>  master.xml I passed PARAMETER passed-info with value ---------${assetchannel}</echo><replaceregexp flags="nowpath" byline="false" encoding="UTF-8"><regexp pattern='nowpath=(.*)' /><substitution expression='nowpath=${assetchannel}' /><fileset dir="${basedir}" includes="ant.properties" /></replaceregexp><mkdir dir="${outdir-classes-ospath}/classes" /><!-- 此java類就做了一件事,通過迴圈的值assetchannel(path名稱)讀取到對應的值賦給nowpath --><javac srcdir="src" includes="${javacPath}" destdir="${outdir-classes-ospath}" classpath="${outdir-classes-ospath}"></javac><java classname="${classPath}"><arg value="${assetchannel}" /><classpath><pathelement path="./bin/classes" /></classpath></java><antcall target="maven-apk"></antcall></target><!--根據path值迴圈替換檔案 --><target name="deploy"><echo level="info">Modify the asset files...</echo><foreach target="modify_content_by_path" list="${assetPathKey}" param="assetpathchannel" delimiter=","></foreach><!-- 清理 --><delete includeemptydirs="true" verbose="true"><fileset dir="${outdir-classes-ospath}" excludes="*.apk,classes"></fileset></delete></target><!-- 替換需修改的檔案並修改AppName --><target name="modify_content_by_path"><!-- 擷取渠道名字 --><propertyregex override="true" property="assetchannelname" input="${assetpathchannel}" regexp="(.*)::" select="\1" /><!-- 擷取渠道號碼 --><propertyregex override="true" property="assetchannelvalue" input="${assetpathchannel}" regexp="::(.*)" select="\1" /><echo level="info">Replace ----- ${assetchannelname}......${assetchannelvalue}.........</echo><if><equals arg1="${appName}" arg2="${assetchannelname}" /><then><!-- 修改pom檔案的appName標籤,確定打包後的apk名稱 --><replaceregexp flags="g" byline="false" encoding="UTF-8"><regexp pattern='\x3CappName\x3E(.*)\x3C/appName\x3E' /><substitution expression="<appName>${assetchannelvalue}</appName>" /><fileset dir="${basedir}" includes="pom.xml" /></replaceregexp></then><else><!-- 開始替換檔案 --><available property="file.exist" file="${assetchannelname}" /><if><isset property="file.exist" /><then><filter filtersfile="${assetchannelname}" /><copy file="${assetchannelname}" overwrite="true" preservelastmodified="true" todir="./${assetchannelvalue}" verbose="true"></copy></then><else><echo level="error">Error: "${assetchannelname}" 檔案不存在</echo></else></if></else></if></target><target name="maven-apk" depends="deploy"><!-- 通過 執行maven命令來打apk包,前提是先配置maven到環境變數  --><exec command="mvn.bat"><arg value="-f" /><arg value="${basedir}/pom.xml" /><arg value="compile" /><arg value="package" /></exec><echo level="verbose">--------BUILD SUCCESSFULL---------</echo></target></project>

配置資訊:

     ant.properies: ---->  屬性配置資訊,由於在maven中的setting.xml配置了keystore資訊,所以此配置可略掉

     build.properies: ---->  需替換的檔案配置資訊,可把ant.properties整合

ant.properies:

key.store=LS.keystorekey.alias=LSkey.store.password=xxxxxxxkey.alias.password=xxxxxxxjdk.version=1.6

build.properties:

#Update nowpath value#Mon Apr 08 13:29:02 CST 2013ant.contrib.asset.key=path,path1,path2path2=E\:/a.png\:\:res/drawable-hdpi,E\:/b.png\:\:res/drawable-hdpi,E\:/c.png\:\:res/drawable-hdpi,E\:/ic_launcher.png\:\:res/drawable-hdpi,AppName\:\:MavenAnt2path=E\:/a.png\:\:res/drawable-hdpi,E\:/b.png\:\:res/drawable-hdpi,E\:/c.png\:\:res/drawable-hdpi,E\:/ic_launcher.png\:\:res/drawable-hdpi,AppName\:\:MavenAntpath1=E\:/a.png\:\:res/drawable-hdpi,E\:/b.png\:\:res/drawable-hdpi,E\:/c.png\:\:res/drawable-hdpi,E\:/ic_launcher.png\:\:res/drawable-hdpi,AppName\:\:MavenAnt1nowpath=E\:/a.png\:\:res/drawable-hdpi,E\:/b.png\:\:res/drawable-hdpi,E\:/c.png\:\:res/drawable-hdpi,E\:/ic_launcher.png\:\:res/drawable-hdpi,AppName\:\:MavenAnt2

AntControl.java根據path/path1/path2修改nowpath的值,如果沒有此需求可以省略這一步。(在ant中迴圈ant.contrib.asset.key的值path為${assetchanel},

以${assetchanel}為key取出"E:\......"的時候取的值總為key的值,path本來就是value,應該有辦法的,有人知道的話告訴我一下,所以又定義 了一個nowpath

而且還是在java中去修改,這樣比較麻煩)。

AntControl.java

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;//This class is a util class , in order to pack apk file. public class ControlAnt {public static void main(String[] args) {try {InputStream in = new FileInputStream("build.properties");Properties properties = new Properties();properties.load(in);//路徑名String str = (String) properties.get(args[0]);String property = properties.getProperty(str, str);//通過路徑名拿到路徑value,然後賦給nowpathproperties.setProperty("nowpath", property);FileOutputStream fos = new FileOutputStream("build.properties");properties.store(fos, "Update" + " nowpath " + "value");} catch (IOException e) {e.printStackTrace();}}}

以下是maven模組的配置:

<?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><artifactId>MavenAnt</artifactId><groupId>a.b</groupId><version>2.1.2</version><packaging>apk</packaging><name>MavenAnt</name><properties>    <!-- ant中修改此標籤,決定打包後apk名稱,達到多渠道打包目的 --><appName>MavenAnt</appName></properties><dependencies><dependency><groupId>com.google.android</groupId><artifactId>android</artifactId><version>2.1.2</version><scope>provided</scope></dependency></dependencies><build><finalName>${appName}</finalName><plugins><plugin><groupId>com.jayway.maven.plugins.android.generation2</groupId><artifactId>android-maven-plugin</artifactId><version>3.0.0-alpha-14</version><configuration><encoding>UTF-8</encoding><androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile><assetsDirectory>${project.basedir}/assets</assetsDirectory><resourceDirectory>${project.basedir}/res</resourceDirectory><nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory><sdk><path>${env.ANDROID_HOME}</path><platform>8</platform></sdk><deleteConflictingFiles>true</deleteConflictingFiles><undeployBeforeDeploy>true</undeployBeforeDeploy></configuration><extensions>true</extensions></plugin><!-- 精減打包 --><!--plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <id>zipalign</id> <goals> <goal>exec</goal> </goals> <phase>install</phase> <configuration> <executable>${ANDROID_HOME}/tools/zipalign</executable> <arguments> <argument>-f</argument> <argument>4</argument> <argument>target/${appName}.apk</argument> <argument>target/${appName}-sign.apk</argument> </arguments> <tasks> </tasks> </configuration> </execution> <execution> </execution> </executions> </plugin --><!-- 簽名 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jarsigner-plugin</artifactId><version>1.2</version><executions><execution><id>signing</id><goals><goal>sign</goal></goals><phase>package</phase><inherited>true</inherited><configuration><archiveDirectory></archiveDirectory><encoding>UTF-8</encoding><includes><argument>target/${appName}.apk</argument></includes><removeExistingSignatures>true</removeExistingSignatures><keystore>${keyFilePath}</keystore><storepass>${storePassword}</storepass><keypass>${keyPassword}</keypass><alias>${keyAlias}</alias><deleteConflictingFiles>true</deleteConflictingFiles></configuration></execution></executions></plugin></plugins></build></project>

在setting.xml中的profiles中添加:

<!-- 打包新增 --><profile><id>sign</id><activation><activeByDefault>true</activeByDefault></activation><properties><keyFilePath>C:\Users\Win7\Desktop\Task\LS.keystore</keyFilePath><storePassword>xxxxxxx</storePassword><keyPassword>xxxxxxx</keyPassword><keyAlias>LS</keyAlias></properties></profile>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.