如何在 Eclipse 中根据变量设置 Ant 属性?
我有一个常见问题,可能有无数种方法可以解决它。我正在为这种典型场景寻找一种优雅、简单的解决方案:
我在 Eclipse 中有一个带有 Ant 构建文件 (build.xml) 的项目,该构建文件使用属性文件 (build.properties)。在该属性文件中,我想设置一个指向 Eclipse 项目根目录的属性,例如:
project.root = /path/to/eclipse/workspace/projectName
或者最好是:
project.root = ${path.to.eclipse.workspace}/projectName
如何以这样的方式执行此操作: 在
- 具有不同项目根路径的不同计算机上工作(即在团队环境中)
- 允许 ant build.xml 文件在 eclipse 内部执行
- 允许 ant build.xml 文件在 eclipse 外部执行(即从命令行)
- 允许 build.properties 文件存在于除项目根
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请参阅窗口 ->首选项->蚂蚁->运行时->用于定义自定义 ant 属性的属性,这些属性应可供从 Eclipse 调用的任何 ant 脚本使用。从命令行调用脚本时,只需手动设置相同的属性即可。
您的 build.properties 文件可以存在于您喜欢的任何位置。使用普通的 Ant 工具将其导入到您的脚本中。
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.
Your build.properties file can exist wherever you like. Use normal Ant facilities to import it into your script.
我认为我正在寻找的是将以下内容添加到 build.properties 文件中:
或者,每当需要 project.root 时,我都可以使用 basedir 属性。
我碰巧正在查看 ivy.properties 的源代码,我看到正在使用 basedir 属性。我刚刚测试并验证了此属性在 eclipse 内部和命令行以及从不同目录调用 ant 时是否适用于不同的机器,例如:
当我有时间时,我将验证这是否也有效在不同位置导入属性文件时(例如在 src/main/resources/config/ivy/ivysettings.xml 内部)。
I think what I'm looking for is to add the following to the build.properties file:
alternatively, I can just use the basedir property whenever project.root is needed.
I happened to be looking at the source code for ivy.properties and I saw the basedir property 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:
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).
对于我的项目档案。
您的情况只需更改 ant xml 文件,
然后我可以使用变量获取项目根目录,
例如
For my project archieve.
Your case can just simply change the ant xml file, the
<project default="main" basedir="../"/>
Then I can get the project root using variable of
e.g.
<echo message= "Project Root: ${basedir}" />
如果您需要的不仅仅是琐碎的 basedir 内容 =
Ant4Eclipse - 一堆用于从 ant 内访问 eclipse 配置的 ant 任务 -
可能会帮助您。只需按原样使用它或 grep 代码并选择相关部分..
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..
您可以从 eclipse 为您的 ANT 构建设置 eclipse 相关属性
转到您的 ANT Builder 属性,在参数部分您可以使用 -D 设置属性,如下所示
(此处工作空间_loc 和项目_loc 是 eclipse 变量)。这些属性可以像常规属性一样在 ANT 构建脚本中访问,例如:
You can set eclipse relative properties for your ANT Build from eclipse
Go to your ANT Builder properties and in arguments section you can set properties using -D as below
(here workspace_loc and project_loc are eclipse variables). These properties can be accessed in your ANT build script like regular properties, for example: