ant:设置依赖于系统的属性的最佳方法?

Jas*_*n S 5 ant

我有许多文件/可执行文件位置可能会有所不同,具体取决于我运行它们的计算机,我想通过某种方式通过ant属性抽象出来.最好的方法是什么?是否有一个系统范围的ant设置脚本被调用?或者我可以制作这样的剧本吗?

And*_*aft 10

除了Vladimir的解决方案之外,您可能还有每个操作系统或其他可能部署构建系统的默认属性文件.使用$ {os.name}(和其他Java系统属性)来设置路径.例如

<property file="build-${os.name}.properties">
Run Code Online (Sandbox Code Playgroud)

这些文件也可以维护并签入版本控制系统.


Vla*_*mir 8

我使用或多或少的标准build.propertiesbuild-local.properties文件.

第一个包含所有环境通用的默认值,第二个包含例外.第一个是检查颠覆,而另一个不是.

编辑:复制/粘贴Akr的好主意

此外,您可能拥有每个操作系统或您可能部署构建系统的其他操作系统的默认属性文件.这些文件也可以签入您的版本控制系统.

然后Ant脚本将包含以下所有文件(记住:在Ant中,第一个定义获胜):

<property file="build-local.properties"/>
<property file="build.properties"/>
<property file="build-${os.name}.properties">
Run Code Online (Sandbox Code Playgroud)


Ale*_*yak 1

设置一个名为properties.xml 的ant 构建文件,您应该在其中定义要自定义的属性。

这是我在项目中使用的properties.xml样板(我从一本关于Ant的书中改编了它):

<?xml version="1.0" encoding="UTF-8"?>
<project
  name="workspace-properties"
>
  <dirname
    property="workspace-properties.basedir"
    file="${ant.file.workspace-properties}"
  />

  <!--
    ==========================================================
    Load Environment Variables
    ==========================================================
  -->
  <!-- #Load environment variables -->
  <property environment="env" />

  <!-- this is here to deal with the fact that an IntelliJ IDEA build
    has no ant home
  -->
  <property
    name="ant.home"
    value="${env.ANT_HOME}"
  />

  <!-- get Unix hostname, and set to Windows comparable name -->
  <!-- #Trick to get host name x-platform -->
  <property
    name="env.COMPUTERNAME"
    value="${env.HOSTNAME}"
  />

  <!--
    ==========================================================
    Load property files
    Note: the ordering is VERY important.
    ==========================================================
  -->
  <!-- #Allow even users property file to relocate -->
  <property
    name="user.properties.file"
    location="${user.home}/.build.properties"
  />

  <!-- Load the application specific settings -->
  <!-- #Project specific props -->
  <property file="build.properties" />

  <!--
    ==========================================================
    Define your custom properties here.
    You can overwrite them through build.properties and
    ${user.home}/.build.properties
    ==========================================================
  -->

  <property name="myexec1" location="/usr/bin/myexec1"/>
  <property name="myexec2" location="/usr/bin/myexec2"/>

</project>
Run Code Online (Sandbox Code Playgroud)

这里重要的是提供尽可能多的有用的默认属性值,那么您甚至可能永远不会提供自定义build.properties文件。

然后你只需<import>将此文件添加到项目的 build.xml 中即可。

<project
  name="my-project"
>
  <!-- this is done, so you may import my-project somewhere else -->
  <dirname
    property="my-project.basedir"
    file="${ant.file.my-project}"
  />

  <import file="${my-project.basedir}/relative/path/to/properties.xml"/>

  <target name="using.myexec1">
    <echo message="myexec1=${myexec1}"/>
  </target>
</project>
Run Code Online (Sandbox Code Playgroud)

如果您想要 myexec1 中的自定义值my-project,只需将自定义平面 build.properties 文件放入 build.xml 所在的同一目录中即可。

build.properties 文件可能如下所示:

myexec1=c:/custom/path/to/myexec1.exe
Run Code Online (Sandbox Code Playgroud)