针对特定目标运行海关规则

时间:2013-08-29 15:02:09

标签: android ant

我是一个ANT菜鸟,所以很难在网上找到我可以为我的android项目解决这个小谜语。

我想要做的是根据我运行的ant命令运行一些规则。 基本上当我运行ANT DEBUG时,我不希望任何自定义规则运行。 当我运行ANT RELEASE时,我希望我的自定义规则能够运行。

目前我的自定义规则无论在预构建阶段是什么时都会运行。

这是我的build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="MainActivity" default="help">
<property file="local.properties" />
<property file="ant.properties" />
<property environment="env" />

<condition property="sdk.dir" value="${env.ANDROID_HOME}">
    <isset property="env.ANDROID_HOME" />
</condition>

<loadproperties srcFile="project.properties" />
<fail
        message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
        unless="sdk.dir"
/>

<import file="custom_rules.xml" optional="true" />

<import file="${sdk.dir}/tools/ant/build.xml" />

这是我的custom_rules.xml

<project>
<macrodef name="git" description="run a git command">
    <attribute name="command" />
    <attribute name="dir" default="" />
    <element name="args" optional="true" />
    <element name="gitOutputRedirector" optional="true"/>
    <sequential>
        <echo message="git @{command}" />
        <exec executable="git" dir="@{dir}">
            <arg value="@{command}" />
            <args/>
            <gitOutputRedirector/>
        </exec>
    </sequential>
</macrodef>

<target name="-pre-build" depends="set-version-using-file,git-last-commit-hash-rev-parse" >

</target>

<target name="set-version-using-file">
    <!-- Load properties from "version.properties" file -->     
    <property file="version.properties" />
    <replaceregexp file="AndroidManifest.xml" match="android:versionCode(.*)"
                           replace='android:versionCode="${Version.Code}"'/>
    <replaceregexp file="AndroidManifest.xml" match='android:versionName="\d+\.+\d+\.+\d+\.+\d"'
                           replace='android:versionName="${Version.Name}"'/>        
   <echo message="Set android:versionCode as ${Version.Code}" />
   <echo message="Set android:versionName as ${Version.Name}" />
</target>


<!-- Get the last commit -->
<target name="git-last-commit-hash-rev-parse" description="Commits all changes to version git" >
    <property file="version.properties" />
    <git command="rev-parse" >
        <args>
            <arg value="HEAD" />
        </args>
        <gitOutputRedirector>
            <redirector outputproperty="git.last.commit"/>
         </gitOutputRedirector>
    </git>
    <echo message="Last commit found was ${git.last.commit}" />
    <echo message="Will now tag ${git.last.commit} with ${Version.Name}" />

    <git command="tag">
        <args>
            <!-- This tags the last commit with the full version name -->
            <arg value="${Version.Name}" />
            <!-- For some reason why it doesn't like this command through ant.-->
            <!-- <arg value="${Version.Name} ${git.last.commit} -m 'Tagged for build'" /> -->
        </args>
    </git>

</target>
</project>

1 个答案:

答案 0 :(得分:1)

好的,我已经解决了!

我将我的宏def和两个目标移动到主build.xml

在我的main build.xml中添加了以下内容

<target name="releaseCan" depends="set-version-using-file, release, git-last-commit-hash-rev-parse"/>

将版本目标更改为以下内容,以便运行预构建

<target name="set-version-using-file" depends="-pre-build">

将git目标更改为以下内容,因此它会在构建后运行。

<target name="git-last-commit-hash-rev-parse" description="Commits all changes to version git" depends="-post-build">

所以我运行ant releaseCan它执行以下操作

修改AndroidManifest.xml(-pre-build) 运行正常发布目标(编译和签名等) 运行git命令(-post-build)

所以它相当基本,我不确定如果发布目标失败,后期构建是否仍会运行。