如何在Ant中查找可执行文件的路径

时间:2015-03-05 13:04:59

标签: mysql ant build ant-contrib

在我的Ant构建文件中,我想测试是否在环境路径上找到了mysql命令。这应该是系统无关的。

到目前为止我所做的是以下内容:

        <trycatch property="mysql.error">
        <try>
            <echo message="Testing mysql..." />
            <exec executable="mysql" outputproperty="null" append="true" />
            <echo message="MySQL executable found in path." />
            <property name="mysql.command" value="mysql"/>
        </try>
        <catch>
            <echo message="MySQL executable not found in path, trying to locate default folder." />
            <if>
                <istrue value="${isWindows}"/>
                <then>
                    <antcallback target="search-file-windows" return="search.result"> 
                        <param name="search.target" value="mysql.exe"/>
                    </antcallback>
                    <property name="mysql.command" value="${search.result}"/>
                </then>
                <else>
                    <property name="mysql.command" value="/usr/local/mysql/bin/mysql"/>
                </else>
            </if>
            <echo message="MySQL executable found at location: ${mysql.command}." />
            <trycatch property="mysql.error">
                <try>
                    <echo message="Possible path found, testing again..." />
                    <exec executable="${mysql.command}" outputproperty="null" append="true" />
                    <echo message="MySQL executable found at location: ${mysql.command}." />
                </try>
                <catch>
                    <fail message="Unable to locate MySQL executable. Please add your local MySQL installation to the PATH environment variable."/>
                </catch>
            </trycatch>
        </catch>
    </trycatch>

所以我只执行mysql命令,如果失败了,我将运行一个批处理文件,它可以有效地搜索mysql。但是,如果在调用mysql时出现任何错误,则检查失败,即使在路径中找到它也是如此。在我的Windows机器上会发生这种情况,因为刚启动mysql会出现以下错误:ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost'

这个错误当然是可以解决的,但我真的在寻找一个通用的解决方案。现在PATH变量包含"C:\Program Files\MySQL\MySQL Server 5.6\bin",因此此问题中的解决方案不起作用:Check if executable command exists using ant

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

available可以为你做到这一点,你只有&#34;只有&#34;需要处理Windows和Unix之间的差异。

像这样的东西

<!-- load environment variables into properties -->
<property environment="env"/>
<!-- On Windows the Environment-Variable is not all uppercase -->
<path id="combined-PATH">
  <pathelement path="${env.PATH}"/>
  <pathelement path="${env.Path}"/>
</path>
<!-- toString() -->
<property name="PATH" refid="combined-PATH"/>
<condition property="mysql.found">
  <or>
    <available file="mysql.exe" filepath="${PATH}"/>
    <available file="mysql" filepath="${PATH}"/>
  </or>
</condition>
当且仅当mysql.found上有mysql时,

才会设置属性PATH

答案 1 :(得分:-1)

以下Ant脚本使用第三方Ant-Contrib库的<for>任务:

<project name="ant-first-match-on-path" default="run">
  <taskdef resource="net/sf/antcontrib/antlib.xml"/>

  <target name="run">
    <property name="executable-name" value="mysql"/>
    <condition property="executable-filename" 
        value="${executable-name}.exe" 
        else="${executable-name}"
    >
        <os family="windows"/>
    </condition>
    <property environment="env" />

    <for param="dir">
        <path>
          <pathelement path="${env.PATH}"/>
          <pathelement path="${env.Path}"/>
        </path>
      <sequential>
        <if>
          <not>
            <isset property="first-match"/>
          </not>
          <then>
            <local name="executable-absolute-path"/>
            <property 
                name="executable-absolute-path" 
                location="@{dir}/${executable-filename}"
            />
            <available 
                file="${executable-absolute-path}" 
                property="first-match" 
                value="${executable-absolute-path}"
            />
          </then>
        </if>
      </sequential>
    </for>

    <condition property="echo-message" 
        value="First [${executable-filename}] found at [${first-match}]." 
        else="[${executable-filename}] not found on PATH."
    >
      <isset property="first-match"/>
    </condition>
    <echo message="${echo-message}"/>
  </target>
</project>