java Ant 中的目标覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6979583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:07:44  来源:igfitidea点击:

Target overriding in Ant

javaandroidantbuild.xml

提问by shaman.sir

Is there a possibility to literally override a target or emulate this somehow?

是否有可能从字面上覆盖目标或以某种方式模拟它?

So, when I call

所以,当我打电话

<target perform-after="release">
     <do-something />
</target>

It will act like this:

它会像这样:

<target name="release">
     <antcall target="release" /> <!-- call previous version, not recursion -->
     <do-something /> 
</target>

I think it has a meaning, I'll describe on Android example:

我认为它有一个含义,我将在Android示例中描述:

We have an .xmltemplates for every build.xmlin SDK folder ({$SDK}/tools/ant/*.xml), these files are included in every generated build.xmlfor each project. There are only -pre-compile, -pre-buildand -post-compiletargets that empty and easy to override. But there is no empty -post-releasetarget, for example. Google recommends in generated build.xmlcomments just to copy-paste a target to my own build.xmland then tune it. But I think it is not ok, because if Google will change something in this target inside a template, I will never know about I am using outdated version.

我们.xml为每个build.xmlSDK 文件夹 ( {$SDK}/tools/ant/*.xml) 中的每个都有一个模板,这些文件包含在build.xml为每个项目生成的每个文件中。只有-pre-compile,-pre-build-post-compile目标为空且易于覆盖。但是没有空的-post-release目标,例如。Google 建议在生成的build.xml评论中只是将目标复制粘贴到我自己的目标build.xml,然后对其进行调整。但我认为这是不行的,因为如果 Google 会在模板中更改此目标中的某些内容,我将永远不会知道我使用的是过时的版本。

回答by Brett Kail

See the "Target overriding" section of the importtask or the "Target rewriting" section of the includetask. In short, give the common build.xml a project name like "common", and then use "common.release" in the antcall.

请参阅导入任务的“目标覆盖”部分或包含任务的“目标重写”部分。总之,给common build.xml一个像“common”这样的项目名,然后在antcall中使用“common.release”。

I'll note that antcall isn't quite the same since it starts a new project at runtime, which means variables set by the target won't be visible later. I don't have Ant available on this machine to test, but you might try something like this to avoid the antcall:

我会注意到 antcall 并不完全相同,因为它在运行时启动一个新项目,这意味着目标设置的变量稍后将不可见。我在这台机器上没有可用的 Ant 来测试,但你可以尝试这样的事情来避免 antcall:

<target name="release" depends="common.release, -post-release"/>