Integrate ant multi-channel packaging with Maven for Android

Source: Internet
Author: User

I found this on the Internet. It seems that I am not familiar with Maven, So I thought of a stupid way to execute pom using ant to execute the MVN command. XML file,

Maven is only responsible for packaging and signing, and other multi-channel operations are handled in ant.

The ant. xml part performs cyclic operations:

1. file replacement

2. Modify the <appname> label in POM. xml

3. Execute the MVN command, compress the APK package and sign the package.

Ant core code:

<? XML version = "1.0" encoding = "UTF-8"?> <Project name = "test" basedir = "." default = "run" xmlns: artifact = "urn: Maven-artifact-ant"> <! -- Reference of ant build --> <property file = "project. properties "/> <property file =" ant. properties "/> <property file =" build. properties "/> <! -- SDK directory (get the operating system environment variable Env. android_sdk_home value) --> <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} "/> <! -- Channel name: channel number --> <property name = "assetpathkey" value = "$ {nowpath}"/> <property name = "classpath" value = ". b. controlant "/> <property name =" javacpath "value =" a/B/controlant. java "/> <property name =" mavenpath "value =" $ {m2_home}/bin "/> <! -- Use a third-party ant package to enable ant to support the for loop --> <taskdef resource = "net/SF/antcontrib. properties "> <classpath> <pathelement location =" $ {antcontrib_jar_path} "/> </classpath> </taskdef> <! -- SDK specifies the platform directory --> <property name = "SDK-platform-folder" value = "$ {SDK-Folder}/platforms/Android-4"/> <! -- Tools Directory In SDK --> <property name = "SDK-Tools" value = "$ {SDK-Folder}/tools"/> <! -- SDK specifies the tools directory on the platform --> <property name = "SDK-platform-Tools" value = "$ {SDK-platform-Folder}/tools"/> <! -- Compile the required jar. If the project uses the map service, maps is required. jar --> <property name = "Android-jar" value = "$ {SDK-platform-Folder}/android. jar "/> <! -- Directory for storing compiled files --> <property name = "outdir-target" value = "target"/> <! -- Configuration file --> <property name = "manifest-xml" value = "androidmanifest. xml"/> <! -- Source file directory --> <property name = "resource-Dir" value = "res"/> <property name = "asset-Dir" value = "assets"/> <! -- Java source file directory --> <property name = "srcdir" value = "src"/> <property name = "srcdir-ospath" value = "$ {basedir}/$ {srcdir} "/> <! -- Directory of the external class library --> <property name = "external-lib" value = "Libs"/> <property name = "external-Lib-ospath" value = "$ {basedir }/$ {external-lib} "/> <! -- Generate the class directory --> <property name = "outdir-classes-ospath" value = "$ {basedir}/$ {outdir-target}"/> <! -- Replace the file key: Value --> <property name = "assetchannelname" value = ""/> <property name = "assetchannelvalue" value = ""/> <! -- Cyclically modify the path worker name --> <target name = "run"> <foreach target = "update_replace_path" list = "$ {assetkey}" Param = "assetchannel" delimiter = ", "> </foreach> </Target> <! -- Modify nowpath, replace and package the file according to 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-C Lasses-ospath}/classes "/> <! -- This Java class does one thing, through the cyclic value assetchannel (path name) read the corresponding value and assign it to 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> <! -- Replace the file cyclically according to the path value --> <target name = "deploy"> <echo level = "info"> modify the asset files... </echo> <foreach target = "modify_content_by_path" list = "$ {assetpathkey}" Param = "assetpathchannel" delimiter = ","> </foreach> <! -- Clear --> <Delete multiple deemptydirs = "true" verbose = "true"> <fileset dir = "$ {outdir-classes-ospath}" excludes = "*. APK, classes "> </fileset> </delete> </Target> <! -- Replace the file to be modified and modify the appname --> <target name = "modify_content_by_path"> <! -- Get channel name --> <propertyregex override = "true" property = "assetchannelname" input = "$ {assetpathchannel}" Regexp = "(. *): "select =" \ 1 "/> <! -- Get channel number --> <propertyregex override = "true" property = "assetchannelvalue" input = "$ {assetpathchannel}" Regexp = "::(. *) "select =" \ 1 "/> <echo level =" info "> replace ----- $ {assetchannelname }...... $ {assetchannelvalue }......... </echo> <if> <equals arg1 = "$ {appname}" arg2 = "$ {assetchannelname}"/> <then> <! -- Modify the appname label of the POM file, determine the name of the packaged 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> <! -- Start to replace the file --> <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}" file does not exist </echo> </else> </If> </Target> <Ta Rget name = "Maven-APK" depends = "deploy"> <! -- Run the maven command to package the APK, provided that Maven is first configured to the environment variable --> <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>

Configuration information:

Ant. properies: ----> property configuration information. This configuration can be omitted because the keystore information is configured in setting. XML in Maven.

Build. properies: ----> file configuration information to be replaced. Ant. properties can be integrated.

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 modifies the value of nowpath Based on path/path1/path2. If this is not required, skip this step. (The path for loop ant. contrib. Asset. Key in ant is $ {assetchanel },

Use $ {assetchanel} as the key to retrieve "E :\...... "When the value is always the key value, path is the value, there should be a way, someone will tell me, So I defined a nowpath

In addition, it is more troublesome to modify it in 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); // path string STR = (string) properties. get (ARGs [0]); string property = properties. getproperty (STR, STR); // get the PATH value through the path name, and then assign it to nowpathproperties. setproperty ("nowpath", property); fileoutputstream Fos = new fileoutputstream ("build. properties "); properties. store (FOS, "Update" + "nowpath" + "value");} catch (ioexception e) {e. printstacktrace ();}}}

The configuration of the maven module is as follows:

<? 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>. B </groupid> <version> 2.1.2 </version> <packaging> APK </packaging> <Name> mavenant </Name> <Properties> <! -- Modify the label in ant to determine the APK name after packaging, multi-channel packaging --> <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_h Ome }</path> <platform> 8 </platform> </SDK> <deleteconflictingfiles> true </deleteconflictingfiles> <strong> true </undeploybeforedeploy> </configuration> <extensions> true </extensions> </plugin> <! -- Reduced packaging --> <! -- 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/invalid appnameapps.apk </argument> <argument> T Arget/${appnameapps-sign.apk </argument> </arguments> <tasks> </configuration> </execution> </executions> </plugin --> <! -- Signature --> <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> <shortdes> <argument> Target/shortlisted appnameapps.apk </argument> </shortdes> <strong> true </removeexistingsignatures> <keystore >$ {keyfilepath} </keystore> <storepass >$ {storepassword} </storepass> <keypass >$ {keypassword} </keypass> <alias >$ {keyalias} </alias> <deleteconflictingfiles> true </deleteconflictingfiles> </configuration> </execution> </executions> </plugin> </plugins> </build> </Project>

Add:

<! -- New package --> <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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.