maven-exec-plugin调用npm run build Docker环境下提示Angular项目未找到
问题解决方法
报错信息:The build command requires to be run in an Angular project, but a project definition could not be found.
根因说明
报错本质是ng build执行时,进程的工作目录下找不到angular.json配置文件。虽然Docker容器的默认工作目录为/,但exec-maven-plugin执行命令时默认使用Maven项目的基准目录,若Docker镜像内的Maven项目路径配置和本地不一致,就会出现目录不匹配的问题。
解决方案
方案1:显式指定exec插件的执行工作目录(优先推荐)
在pom.xml的exec插件configuration节点下新增workingDirectory配置,指定到angular.json所在的根目录即可:
<execution> <id>npm run build</id> <goals> <goal>exec</goal> </goals> <phase>prepare-package</phase> <configuration> <executable>npm</executable> <workingDirectory>/</workingDirectory> <!-- 新增这行,指定到angular.json所在目录 --> <arguments> <argument>run</argument> <argument>build:hello</argument> </arguments> </configuration> </execution>
方案2:在npm脚本中强制切换工作目录
修改package.json中的build:hello脚本,执行ng build前先切换到angular.json所在的根目录:
"build:hello": "cd / && ng build hello --base-href /hello --deploy-url /hello/static/ --prod && cd ./projects/hello-app && gulp tasks --base-href=/hello --deploy-url=/hello/static/"
验证方法
如果修改后仍有问题,可以临时在exec插件中增加调试命令,确认执行时的目录和文件是否存在:
<configuration> <executable>ls</executable> <arguments> <argument>-la</argument> <argument>/</argument> </arguments> </configuration>
执行后观察输出中是否存在angular.json文件即可定位问题。
内容的提问来源于stack exchange,提问作者pacionet
相关产品推荐
相关产品推荐

