我在ant task(build.xml)中为git clone创建了一个小宏定义,如下所示:
<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:\Users\Haritha\Desktop\H2H\conf\build.xml:14: The following error occurred while executing this line:
C:\Users\Haritha\Desktop\H2H\conf\build.xml:25: Execute failed: java.io.IOException: Cannot run program "git": CreateProcess error=2, The system cannot find the file specified
如何解决这个问题,成功执行我的git ant任务没有任何错误?
答案 0 :(得分:2)
将git.exe的bin路径(如C:\ Users \ Haritha \ Desktop \ git \ bin之类)添加到PATH变量,如下所示。要使其永久更改,如果您使用的是Windows,则可以将其添加为环境变量。
答案 1 :(得分:1)
您有两种选择,具体取决于您的偏好。
选项1:向build.properties文件添加属性值,并在build.xml文件中将可执行文件“git”替换为“$ {gitEXE}”。
注意:我们在c:/ Program Files / Git / cmd / git.exe的cmd目录中使用git.exe
<强> build.properties 强>
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>