【问题标题】:Ant build in EclipseEclipse 中的 Ant 构建
【发布时间】:2014-07-23 21:51:21
【问题描述】:

我正在尝试使用自定义构建文件来在我的 Android 应用程序中显示当前的 git 版本。我以前没有使用过 ant,我也不知道如何使用它。我在 SO 中阅读了很多主题,并在 Google 中进行了很多搜索,但我无法弄清楚。我真的没有时间学习关于 ant 的一切,但我需要运行这个东西。在底部,您可以找到代码。

当前状态

文件custom_rules.xml被导入到Eclipse创建的build.xml中。 macrodef 部分被调用,但目标不被调用。我尝试更改External Tools Configurations,标签Targets,但每当我检查目标(无论在哪个ant文件中)时,都会收到一条消息:

Unknown argument: -pre-build

例如(当我在-pre-build 上打勾时)。我尝试添加这一行:

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

并定义 sdk.dir 但这并没有改变任何东西。我错过了什么?正如我所说,我对蚂蚁​​一无所知,唯一对我有帮助的教程是this one

当前代码 (custom_rules.xml)

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse.ant.import ?>
<project name="Custom build">

    <macrodef name="git" taskname="@{taskname}">
        <attribute name="command" />
        <attribute name="dir" default="" />
        <attribute name="property" default="" />
        <attribute name="taskname" default="" />
        <attribute name="failonerror" default="on" />
        <element name="args" optional="true" />
        <sequential>
            <exec executable="git" dir="@{dir}" outputproperty="@{property}" 
                failifexecutionfails="@{failonerror}" failonerror="@{failonerror}">
                <arg value="@{command}" />
                <args/>
            </exec>
        </sequential>
    </macrodef>

    <target name="-pre-build">
        <git command="rev-list" property="versioning.code" taskname="versioning">
            <args>
                <arg value="master" />
                <arg value="--first-parent" />
                <arg value="--count" />
            </args>
        </git>
        <git command="describe" property="versioning.name" taskname="versioning">
            <args>
                <arg value="--always" />
            </args>
        </git>
        <echo level="info" taskname="versioning">${versioning.code}, ${versioning.name}</echo>
        <replaceregexp file="AndroidManifest.xml" match='android:versionCode=".*"' replace='android:versionCode="${versioning.code}"' />
        <replaceregexp file="AndroidManifest.xml" match='android:versionName=".*"' replace='android:versionName="${versioning.name}"' />
    </target>

    <target name="-post-build" >
        <replaceregexp file="AndroidManifest.xml" match='android:versionCode=".*"' replace='android:versionCode="0"' />
        <replaceregexp file="AndroidManifest.xml" match='android:versionName=".*"' replace='android:versionName="0"' />
    </target>

</project>

【问题讨论】:

    标签: android eclipse git ant build


    【解决方案1】:

    我刚刚想通了。

    • 我将目标重命名为pre-buildpost-build,而没有-
    • 我去了External Tools Configurations并选择了左边的build.xml。在右侧,我转到标签Targets,检查了我的两个目标(除了已经有复选标记的build 目标)并将顺序设置为pre-build, build, post-build
    • 我进入项目属性,在左侧选择了Builders。我使用build.xml 文件创建了一个新构建器,并以相同的顺序具有上一个要点中的三个目标。我将此构建器放在 Java 构建器之前。
    • 我删除了 post-build 目标,因为它将版本恢复为 0 并且似乎比我想要的要早。

    我仍然不确定这是否是最佳解决方案。此外,当没有 git 版本控制时,解决方案会失败。我尝试使用this code 解决问题,但没有奏效。尽管如此,这是我能做的最好的事情,它可以帮助我在应用程序中获得所需的信息。

    【讨论】:

    • 您好,我在从 ant 构建 apk 时收到“未知参数:-release”。你能猜到我哪里出错了吗?
    【解决方案2】:

    使用带有参数--version 的 Git 可执行文件,并在属性中捕获输出以供进一步使用,f.e. :

    <project> 
    
    <exec  executable="git" outputproperty="gitversion">
     <arg value="--version"/>
    </exec>
    
    <echo>$${gitversion} => ${gitversion}</echo>
    </project>
    

    输出:

    [echo] ${gitversion} => git version 1.8.3.msysgit.0
    

    【讨论】:

    • 我想要我的项目的 git 版本并且我的代码已经包含正确的 git 命令。问题是让 ant 运行而不是 git 命令。
    猜你喜欢
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2011-07-28
    • 2016-11-26
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多