用 ant 执行 npm

Ohm*_*men 3 ant npm

我想用前端和后端自动构建应用程序。为此,我想将 maven 与 ant 一起用于平台独立的复制和 cli 任务。用一些 clidocker ...这样的作品。但这不适用于由 clinpmnpm它本身提供的 cli 。

<exec executable="docker">
    <arg line="version"/>
</exec>
<!--Works-->
<exec executable="C:\Program Files\nodejs\npm.cmd">
    <arg line="version"/>
</exec>
<!--Doesn't work-->
<exec executable="npm">
    <arg line="version"/>
</exec>
Run Code Online (Sandbox Code Playgroud)

如第二个示例所示,如果我指定了 npm.cmd 的完整路径,脚本就可以工作。但这至少应该在 windows 和 unix 上工作。所以指定完整路径不是一个选项。

有没有办法npm从 ant运行它的模块?


后期编辑:

真正的问题是,windows 节点安装程序还在npmbin 文件夹中放置了一个名为的文件,该文件是一个用于 cygwin 的 bash 脚本。npm bin 文件夹已添加到“全局” PATHenv var 并且 windows cmd 确实选择了正确的二进制文件,因为它使用PATHEXTenv var 来确定什么是可执行的,什么不是。ant exec 插件不使用PATHEXT,只执行命名npm失败的第一个文件。该解决方案是重命名平原npm路径文件。这样蚂蚁npm.cmd首先看到文件,一切运行顺利。

小智 11

我知道这是旧的,但这是我拥有的,为了将来的其他人。适用于我们的 mac、unix 和 windows 机器。

  <macrodef name="exec-node">
    <attribute name="module" description="The name of the NodeJS module to execute" />
    <attribute name="failonerror" default="true" description="Fail if the exit code is not 0" />
    <attribute name="dir" description="Directory to execute task" />
    <element name="args" implicit="yes" description="Argument to pass to the exec task" />
    <sequential>
      <exec executable="cmd.exe" dir="@{dir}" failonerror="@{failonerror}" osfamily="winnt">
        <arg line="/c  @{module}" />
        <args />
      </exec>
      <exec executable="@{module}" dir="@{dir}" failonerror="@{failonerror}" osfamily="unix" logError="true">
        <args />
      </exec>
    </sequential>
  </macrodef>
Run Code Online (Sandbox Code Playgroud)

然后这样的事情可以工作

    <exec-node dir="${node.project.dir}" module="npm" failonerror="true" >
      <arg value="run" />
      <arg value="lint" />
    </exec-node>
Run Code Online (Sandbox Code Playgroud)

你当然可以将模块硬编码为 npm,或者默认它,但我将它与 npm 和 npx 一起使用。