【问题标题】:Running "ng build" in docker container gives "project definition could not be found"在 docker 容器中运行“ng build”会给出“找不到项目定义”
【发布时间】:2021-02-23 09:43:29
【问题描述】:

我正在尝试使用 Maven 构建一个 Angular + Spring-boot 项目并将其部署为 docker 容器。 编译我运行的应用程序

mvn package

在某些时候运行ng build。 当我在本地机器上执行此操作时,它按预期工作。但是当我将文件复制到 docker 映像中并尝试运行 mvn package 时,出现错误

[INFO] --- frontend-maven-plugin:1.11.2:npm (npm-build) @ tradingSandbox ---
[INFO] Running 'npm run-script build' in /
[INFO] 
[INFO] > trading-sandbox@0.0.0 build /
[INFO] > ng build
[INFO] 
[INFO] The build command requires to be run in an Angular project, but a project definition could not be found.
[INFO] npm ERR! code ELIFECYCLE
[INFO] npm ERR! errno 1
[INFO] npm ERR! trading-sandbox@0.0.0 build: `ng build`
[INFO] npm ERR! Exit status 1
[INFO] npm ERR! 
[INFO] npm ERR! Failed at the trading-sandbox@0.0.0 build script.
[INFO] npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
[INFO] 
[INFO] npm ERR! A complete log of this run can be found in:
[INFO] npm ERR!     /root/.npm/_logs/2021-02-23T09_15_24_641Z-debug.log
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

根目录包含正确的angular.json 文件,Maven 会下载正确版本的 nodejs。因为它位于 docker 映像中,所以我无法访问 debug.log。不用说我没有使用 Docker 的经验。

Dockerfile

FROM maven:3.6.3-jdk-11

COPY . /

RUN cd /
RUN ls -a

ENV PORT=8080

RUN mvn package

CMD mvn spring-boot:run -Dspring-boot.run.profiles=$PROFILE -Dserver.port=$PORT

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>tradingSandbox</artifactId>
    <version>0.0.1</version>
    <name>tradingSandbox</name>
    <description>Trading sandbox</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!--    Database controller for deployment  -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

<!--    Database controller for testing -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>0.10.7</version>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>0.10.7</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>0.10.7</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>

        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
        <groupId>com.github.eirslett</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <version>1.11.2</version>
                <configuration>
                    <nodeVersion>v12.18.4</nodeVersion>
                </configuration>
                <executions>

                    <execution>
                        <id>install-npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                    </execution>

                    <execution>
                        <id>npm-install</id>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                    </execution>

          <execution>
            <id>npm-build</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <configuration>
              <arguments>run-script build</arguments>
            </configuration>
          </execution>

                </executions>
            </plugin>

        </plugins>
    </build>

</project>

【问题讨论】:

    标签: angular docker maven


    【解决方案1】:

    您在错误的目录中。找不到angular.json 文件。

    删除

    RUN cd /
    

    来自您的 dockerfile。

    确保RUN ls -a返回包含angular.json的目录,如果没有,请正确配置RUN cd blabla

    【讨论】:

    • 感谢您的建议。我删除了RUN cd /,但不幸的是错误仍然存​​在。
    • 运行 ls -a 时打印什么?
    • Step 3/6 : RUN ls -a ---&gt; Running in 657efcabf8c0 . .. .browserslistrc .dockerenv .editorconfig .git .gitignore .idea .mvn README.md angular.json bin boot dev dockerfile e2e etc heroku.yml home karma.conf.js lib lib64 media mnt ng node node_modules npm opt package-lock.json package.json pom.xml proc root run sbin src srv sys target tmp tradingSandbox.iml tsconfig.app.json tsconfig.json tsconfig.spec.json tslint.json usr var
    【解决方案2】:

    今天遇到了同样的问题。即使RUN ls -la 给了我正确的目录,设置一个明确的WORKDIR 为我修复了它

    WORKDIR /var/www
    

    我把它放在我的 Dockerfile 中 FROM ... 行之后的第二行。该目录应该无关紧要,因为WORKDIR 会在它不存在时创建它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-17
      • 2020-08-25
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      相关资源
      最近更新 更多