我在ant task(build.xml)中为git克隆创建了小的宏定义,如下所示:
<project name="MyProject" default="fileCopy">
<target name="fileCopy" depends="file-checks, local-file, git-file"/>
<target name="file-checks">
<available file="rdbms1.properties" property="file.found"/>
</target>
<target name="local-file" if="file.found">
<echo message="File is available in Local" />
</target>
<target name="git-file" unless="file.found">
<git command="clone" options="https://git-wip-us.apache.org/repos/asf/ant.git"/>
</target>
<macrodef name="git">
<attribute name="command" />
<attribute name="options" default="" />
<attribute name="dir" default="" />
<attribute name="failerror" default="false" />
<element name="args" optional="true" />
<sequential>
<echo message="git dir @{dir}" />
<echo message="git @{command}" />
<exec executable="git" dir="@{dir}" failonerror="@{failerror}">
<arg line="@{command} @{options}" />
<args />
</exec>
</sequential>
</macrodef>
</project>
但出现如下异常:
C:UsersHarithaDesktopH2Hconfbuild.xml:14: The following error occurred while executing this line:
C:UsersHarithaDesktopH2Hconfbuild.xml:25: Execute failed: java.io.IOException: Cannot run program "git": CreateProcess error=2, The system cannot find the file specified
如何解决此问题以成功执行我的git ant任务而不会出现任何错误?
将 git 的 bin 路径.exe(类似于 C:\Users\Haritha\Desktop\git\bin)添加到 PATH 变量中,如下所示。若要使其永久更改,如果使用的是 Windows,则可以将其添加为环境变量。
您有 2 个选项,具体取决于您的偏好。
- 将 Git.exe 路径添加到 build.properties 文件
- 将可执行文件硬编码到 ANT 脚本中
选项 1:将属性值添加到 build.properties 文件,并将 build.xml 文件中的可执行文件"git"替换为"${gitEXE}"。
注意:我们在 c:/Program Files/Git/cmd/git 的 cmd 目录中使用 git.exe.exe
构建属性
gitEXE=c:/Program Files/Git/cmd/git.exe
<macrodef name="git">
<attribute name="command" />
<attribute name="options" default="" />
<attribute name="dir" default="" />
<attribute name="failerror" default="false" />
<element name="args" optional="true" />
<sequential>
<echo message="git dir @{dir}" />
<echo message="git @{command}" />
<exec executable="${gitEXE}" dir="@{dir}" failonerror="@{failerror}">
<arg line="@{command} @{options}" />
<args />
</exec>
</sequential>
</macrodef>
选项 2:硬代码可执行文件。首选选项 1。
<macrodef name="git">
<attribute name="command" />
<attribute name="options" default="" />
<attribute name="dir" default="" />
<attribute name="failerror" default="false" />
<element name="args" optional="true" />
<sequential>
<echo message="git dir @{dir}" />
<echo message="git @{command}" />
<exec executable="c:/Program Files/Git/cmd/git.exe" dir="@{dir}" failonerror="@{failerror}">
<arg line="@{command} @{options}" />
<args />
</exec>
</sequential>
</macrodef>