如何在 Eclipse 中根据变量设置 Ant 属性?

发布于 2024-10-11 14:53:51 字数 558 浏览 7 评论 0原文

我有一个常见问题,可能有无数种方法可以解决它。我正在为这种典型场景寻找一种优雅、简单的解决方案:

我在 Eclipse 中有一个带有 Ant 构建文件 (build.xml) 的项目,该构建文件使用属性文件 (build.properties)。在该属性文件中,我想设置一个指向 Eclipse 项目根目录的属性,例如:

project.root = /path/to/eclipse/workspace/projectName

或者最好是:

project.root = ${path.to.eclipse.workspace}/projectName

如何以这样的方式执行此操作: 在

  1. 具有不同项目根路径的不同计算机上工作(即在团队环境中)
  2. 允许 ant build.xml 文件在 eclipse 内部执行
  3. 允许 ant build.xml 文件在 eclipse 外部执行(即从命令行)
  4. 允许 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:

  1. Works on different machines with different paths to the project root (i.e. in a team environment)
  2. Allows the ant build.xml file to be executed inside eclipse
  3. Allows the ant build.xml file to be executed outside of eclipse (i.e. from command line)
  4. Allows the build.properties file to exist in a directory other than the project root

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拒绝两难 2024-10-18 14:53:51

请参阅窗口 ->首选项->蚂蚁->运行时->用于定义自定义 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.

野生奥特曼 2024-10-18 14:53:51

我认为我正在寻找的是将以下内容添加到 build.properties 文件中:

project.root = ${basedir}

或者,每当需要 project.root 时,我都可以使用 basedir 属性。

我碰巧正在查看 ivy.properties 的源代码,我看到正在使用 basedir 属性。我刚刚测试并验证了此属性在 eclipse 内部和命令行以及从不同目录调用 ant 时是否适用于不同的机器,例如:

ant -f /path/to/eclipse/workspace/projectName/build.xml

当我有时间时,我将验证这是否也有效在不同位置导入属性文件时(例如在 src/main/resources/config/ivy/ivysettings.xml 内部)。

I think what I'm looking for is to add the following to the build.properties file:

project.root = ${basedir}

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:

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).

溇涏 2024-10-18 14:53:51

对于我的项目档案。

ProjectName <dir>
 |_ ant <dir>
     |_ ant.xml

您的情况只需更改 ant xml 文件,

然后我可以使用变量获取项目根目录,

例如

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="../"/>

Then I can get the project root using variable of

e.g. <echo message= "Project Root: ${basedir}" />

梦中楼上月下 2024-10-18 14:53:51

如果您需要的不仅仅是琐碎的 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..

心奴独伤 2024-10-18 14:53:51

您可以从 eclipse 为您的 ANT 构建设置 eclipse 相关属性

转到您的 ANT Builder 属性,在参数部分您可以使用 -D 设置属性,如下所示

-Dworkspace="${workspace_loc}" -Dproject_dir="${project_loc}"

(此处工作空间_loc 和项目_loc 是 eclipse 变量)。这些属性可以像常规属性一样在 ANT 构建脚本中访问,例如:

<echo message="${workspace}" />
<echo message="${project_dir}" />

参考屏幕截图

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

-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:

<echo message="${workspace}" />
<echo message="${project_dir}" />

reference screenshot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文