我从映像mvn clean install
在docker容器内执行maven:3-alpine
以构建应用程序。在pom.xml中,我使用frontend-maven-plugin
,因为我需要安装node
和npm
,然后运行npm install
来构建前端(角度)。
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.9.1</version>
<configuration>
<nodeVersion>v12.16.1</nodeVersion>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<phase>generate-resources</phase>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>install</arguments>
<installDirectory>./</installDirectory>
</configuration>
</execution>
<execution>
<id>ng build</id>
<goals>
<goal>npm</goal>
</goals>
<phase>compile</phase>
<configuration>
<arguments>run-script build</arguments>
</configuration>
</execution>
</executions>
</plugin>
节点和npm将被正确安装,但是当它运行npm install
时会返回错误:Failed to run task: 'npm install' failed. java.io.IOException: Cannot run program "/var/lib/jenkins/workspace/myProject/node/node" (in directory "/var/lib/jenkins/workspace/myProject"): error=2, No such file or directory -> [Help 1]
如果我进入容器(docker exec)并尝试手动运行npm install,它将再次给我同样的错误。当我检查节点是否正确安装时,我会看到文件/var/lib/jenkins/workspace/myProject/node/node
在其中,但是当我尝试手动运行时,我们说node -v
它再次对我说No such file or directory
。我不明白为什么会这样,因为节点在那里!
我对此问题进行了一些搜索,有人说以这种方式在ubuntu上安装节点不是正确的方法,但这仅发生在我的容器中。如果我在本地计算机(也是ubuntu)上尝试相同的操作,则节点有效。
单独运行node -v
时,它试图在PATH上查找节点。以这种方式通过maven安装不会为您完成]
Check out one of these answers,有关在Linux上的操作方法。
如果您的用户没有权限,如果您使用詹金斯身份运行,我怀疑是这种情况,请使用指向节点和npm二进制文件的完整或相对路径来运行所需的命令。
例如如果目录test.js
中有文件/var/lib/jenkins/workspace/myProject
,则该目录中的文件可以运行:
./node/node test.js
或
/var/lib/jenkins/workspace/myProject/node/node
此外:在您的CI环境中使用Maven安装NPM和Node的更好解决方案是使用nvm,Docker映像或Jenkins use the Node.js plugin。