如何根据 Eclipse 中的变量设置 Ant 属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4660366/
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
How to set Ant properties based on variables in Eclipse?
提问by gMale
I have a common problem and there are probably countless ways to solve it. I'm looking for an elegant, simple solution to this typical scenario:
我有一个共同的问题,可能有无数种方法可以解决它。我正在为这种典型情况寻找一种优雅、简单的解决方案:
I have a project in Eclipse with an Ant build file (build.xml) the build file uses a property file (build.properties). In that property file, I want to set a property that points to the root directory of the eclipse project such as:
我在 Eclipse 中有一个带有 Ant 构建文件 (build.xml) 的项目,该构建文件使用一个属性文件 (build.properties)。在该属性文件中,我想设置一个指向 eclipse 项目根目录的属性,例如:
project.root = /path/to/eclipse/workspace/projectName
or preferably:
或最好:
project.root = ${path.to.eclipse.workspace}/projectName
How do I do this in such a way that:
我该如何做到这一点:
- Works on different machines with different paths to the project root (i.e. in a team environment)
- Allows the ant build.xml file to be executed inside eclipse
- Allows the ant build.xml file to be executed outside of eclipse (i.e. from command line)
- Allows the build.properties file to exist in a directory other than the project root
- 在具有不同路径到项目根目录的不同机器上工作(即在团队环境中)
- 允许在eclipse内部执行ant build.xml文件
- 允许 ant build.xml 文件在 eclipse 之外执行(即从命令行)
- 允许 build.properties 文件存在于项目根目录以外的目录中
采纳答案by gMale
I think what I'm looking for is to add the following to the build.properties file:
我想我正在寻找的是将以下内容添加到 build.properties 文件中:
project.root = ${basedir}
alternatively, I can just use the basedirproperty whenever project.rootis needed.
或者,我可以在需要project.root时使用basedir属性。
I happened to be looking at the source code for ivy.propertiesand I saw the basedirproperty being used. I just tested and verified that this property works on different machines both from inside eclipse and from the command line as well as when making a call to ant from a different directory such as:
我碰巧正在查看 ivy.properties的源代码,我看到正在使用basedir属性。我刚刚测试并验证了这个属性在 eclipse 内部和命令行以及从不同目录调用 ant 时在不同的机器上工作,例如:
ant -f /path/to/eclipse/workspace/projectName/build.xml
When I get a minute, I will verify that this also works when importing the property file in different locations (such as inside src/main/resources/config/ivy/ivysettings.xml).
当我有时间时,我将验证在不同位置(例如在 src/main/resources/config/ivy/ivysettings.xml 中)导入属性文件时这是否也有效。
回答by Konstantin Komissarchik
See Window -> Preferences -> Ant -> Runtime -> Properties to define custom ant properties that should be available to any ant script invoked from Eclipse. The simply set the same property manually when invoking script from command-line.
请参阅 Window -> Preferences -> Ant -> Runtime -> Properties 以定义自定义 ant 属性,这些属性应该可用于从 Eclipse 调用的任何 ant 脚本。从命令行调用脚本时,只需手动设置相同的属性。
Your build.properties file can exist wherever you like. Use normal Ant facilities to import it into your script.
您的 build.properties 文件可以存在于您喜欢的任何位置。使用普通的 Ant 工具将其导入到您的脚本中。
回答by Man Pak Hong
For my project archieve.
对于我的项目档案。
ProjectName <dir>
|_ ant <dir>
|_ ant.xml
Your case can just simply change the ant xml file, the <project default="main" basedir="../"/>
您的情况只需更改 ant xml 文件, <project default="main" basedir="../"/>
Then I can get the project root using variable of
然后我可以使用变量获取项目根目录
e.g. <echo message= "Project Root: ${basedir}" />
例如 <echo message= "Project Root: ${basedir}" />
回答by Rebse
if you need more than the trivial basedir stuff =
Ant4Eclipse- a bunch of ant tasks for access to eclipse configurations from within ant -
may help you. Just use it as is or grep the code and pick the relevant parts..
如果您需要的不仅仅是基于琐碎的东西 =
Ant4Eclipse- 一堆用于从 ant 内部访问 eclipse 配置的 ant 任务 -
可能会对您有所帮助。只需按原样使用它或 grep 代码并选择相关部分..
回答by deepak
You can set eclipse relative properties for your ANT Build from eclipse
您可以从 eclipse 为您的 ANT Build 设置 eclipse 相关属性
Go to your ANT Builder properties and in arguments section you can set properties using -D as below
转到您的 ANT Builder 属性,在参数部分您可以使用 -D 设置属性,如下所示
-Dworkspace="${workspace_loc}" -Dproject_dir="${project_loc}"
(here workspace_loc and project_loc are eclipse variables). These properties can be accessed in your ANT build script like regular properties, for example:
(这里workspace_loc 和project_loc 是eclipse 变量)。这些属性可以像常规属性一样在您的 ANT 构建脚本中访问,例如:
<echo message="${workspace}" />
<echo message="${project_dir}" />