用于Android Manifest Versioning的Ant构建文件

时间:2012-12-03 11:16:14

标签: android eclipse ant

我正在尝试使用Ant构建文件自动更新Android Manifest。代码本身似乎有用(当手动运行时),但是当我从Eclipse运行项目时,我似乎无法完全使用它。预构建部分工作,问题是清理(后期构建)。我正在尝试撤消构建后的所有内容,以便在repo中没有任何更改的文件:

<project name="manifest-versioning" default="pre-build" >

<target name="pre-build" depends="get-repository-info,add-manifest-version">

    <echo>Android Manifest was updated</echo>

</target>

<target name="get-repository-info">

    <echo>Get the total number of git commits</echo>
    <exec executable="sh" outputproperty="git.commits">
        <arg value="-c" />
        <arg value="git log --pretty=format:&apos;&apos; | wc -l" />
    </exec>
    <echo>git.commits: ${git.commits}</echo>

    <echo>Get the version from the git release branch</echo>
    <exec executable="sh" outputproperty="git.branchversion">
        <arg value="-c" />
        <arg value="git rev-parse --abbrev-ref HEAD  | cut -d &apos;-&apos; -f2" />
    </exec>
    <echo>git.branchversion: ${git.branchversion}</echo>

    <echo>Get the number of git release branch commits</echo>
    <exec executable="sh" outputproperty="git.branchcommits">
        <arg value="-c" />
        <arg value="git log master..release-${git.branchversion} --oneline | wc -l" />
    </exec>
    <echo>git.branchcommits: ${git.branchcommits}</echo>

    <echo>Get the last known subversion revision number from git svn</echo>
    <exec executable="sh" outputproperty="git.svnrevision">
        <arg value="-c" />
        <arg value="git svn log --oneline -1 | cut -d &apos; &apos; -f1 | cut -d &apos;r&apos; -f2" />
    </exec>
    <echo>git.svn.revision: ${git.svnrevision}</echo>

    <!-- create a temporary property file for later use -->
    <propertyfile file="git.properties" comment="Git properties">
        <entry key="commits" type="int" value="${git.commits}" />
        <entry key="branchversion" value="${git.branchversion}" />
        <entry key="branchcommits" type="int" value="${git.branchcommits}" />
        <entry key="svnrevision" type="int" value="${git.svnrevision}" />
    </propertyfile>

</target>

<target name="add-manifest-version" depends="get-repository-info">

    <echo>Creating backup of AndroidManifest.xml</echo>
    <copy file="AndroidManifest.xml"
        tofile="AndroidManifest.xml.antbak"
        preservelastmodified="false" /> <!-- true copies postcopy changes as well -->

    <echo>Adding version numbers to AndroidManifest.xml</echo>
    <replaceregexp
        file="AndroidManifest.xml"
        match="android:versionCode=&quot;(\d+)&quot;"
        replace="android:versionCode=&quot;${git.svnrevision}&quot;" />
    <replaceregexp
        file="AndroidManifest.xml"
        match="android:versionName=&quot;(\d+\.\d+)\.\d+&quot;"
        replace="android:versionName=&quot;${git.branchversion}.${git.branchcommits}&quot;" />

</target>

<target name="post-build" depends="restore-manifest,create-versioned-apk">

    <echo>Android Manifest was restored</echo>

</target>

<target name="restore-manifest">

    <echo>Restoring backup of AndroidManifest.xml</echo>
    <move file="AndroidManifest.xml.antbak"
      tofile="AndroidManifest.xml"
      preservelastmodified="false"
      overwrite="true" />

</target>

<target name="create-versioned-apk" depends="restore-manifest">

    <property name="out.final.file" value="bin/Zorgdashboard.apk" />
    <property prefix="git" file="git.properties"/>
    <property name="suffix" value="${git.branchversion}.${git.branchcommits}.apk" />

    <exec executable="sed" inputstring="${out.final.file}" outputproperty="out.final.renamedfile">
        <arg value="s/\.apk/-${suffix}/" />
    </exec>

    <copy file="${out.final.file}" tofile="${out.final.renamedfile}" />
    <echo>Final file copied to: ${out.final.renamedfile}</echo>

    <delete file="git.properties" />

</target>

</project>

我猜我在设置目标方面做错了。我也尝试将恢复部分放在一个单独的文件中,但无济于事。这甚至可能吗?

0 个答案:

没有答案
相关问题