Vaadin + Spring Boot生产模式构建出现运行时错误:无法确定“ npm”工具

解决方案更新 该错误似乎是导致此问题的原因:https://github.com/vaadin/flow/issues/6657

推出此修复程序时,标记的答案可以用作解决方法

背景

我正在为portal创建基于dockerized的,基于Spring Boot + Vaadin流的IoT project

问题

在Vaadin的开发模式下,该门户在我的计算机上运行良好,并且安装了npm,但是对于创建已经包含所有生成的前端组件的轻量级部署jar来说,我似乎没有正确的设置。

当我在Docker容器中运行服务时,出现错误,它找不到npm,但我认为生产部署中不需要它

at com.vaadin.flow.server.startup.DevModeInitializer.initDevModeHandler(DevModeInitializer.java:327)
    at com.vaadin.flow.spring.VaadinServletContextInitializer$DevModeServletContextListener.contextInitialized(VaadinServletContextInitializer.java:323)
    ... 46 common frames omitted
Caused by: com.vaadin.flow.server.ExecutionFailedException: 

Failed to determine 'npm' tool.
Please install it either:
  - by following the https://nodejs.org/en/download/ guide to install it globally
  - or by running the frontend-maven-plugin goal to install it in this project:
  $ mvn com.github.eirslett:frontend-maven-plugin:1.7.6:install-node-and-npm -DnodeVersion="v12.13.0" 

没有npm的Docker文件

FROM openjdk:11-jre-slim

ARG PROJECT
ARG SERVICE_PORT
ARG JAR_FILE

EXPOSE ${SERVICE_PORT}

RUN mkdir /${PROJECT}
WORKDIR /${PROJECT}

ADD target/${JAR_FILE} ./app.jar

CMD ["java","-jar","app.jar"]

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.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tlvlp</groupId>
    <artifactId>iot-portal</artifactId>
    <version>0.0.2</version>
    <name>iot-portal</name>
    <description>tlvlp IoT server portal</description>

    <properties>
        <java.version>11</java.version>
        <vaadin.version>14.0.12</vaadin.version>
        <dockerfile.maven.version>1.4.13</dockerfile.maven.version>
        <flow.server.prod.version>2.0.17</flow.server.prod.version>
        <!--    DOCKER IMAGE ARGS   -->
        <docker.project.repository>tlvlp/iot-portal</docker.project.repository>
        <service.port>8600</service.port>
    </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>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>${vaadin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

        </plugins>
    </build>

    <profiles>
        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
                <vaadin.productionmode>true</vaadin.productionmode>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>com.vaadin</groupId>
                    <artifactId>flow-server-production-mode</artifactId>
                    <version>${flow.server.prod.version}</version>
                </dependency>
            </dependencies>

            <build>
                <plugins>
                    <plugin>
                        <groupId>com.vaadin</groupId>
                        <artifactId>flow-maven-plugin</artifactId>
                        <version>${flow.server.prod.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>build-frontend</goal>
                                    <goal>copy-production-files</goal>
                                    <goal>package-for-production</goal>
                                </goals>
                                <phase>compile</phase>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.spotify</groupId>
                        <artifactId>dockerfile-maven-plugin</artifactId>
                        <version>${dockerfile.maven.version}</version>
                        <executions>
                            <execution>
                                <id>prod</id>
                                <goals>
                                    <goal>build</goal>
                                    <goal>push</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <repository>${docker.project.repository}</repository>
                            <tag>${project.version}</tag>
                            <tag>latest</tag>
                            <buildArgs>
                                <PROJECT>${project.groupId}.${project.artifactId}</PROJECT>
                                <SERVICE_PORT>${service.port}</SERVICE_PORT>
                                <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                            </buildArgs>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>dev</id>
            <properties>
                <activatedProperties>dev</activatedProperties>
                <vaadin.productionmode>false</vaadin.productionmode>
            </properties>
        </profile>
    </profiles>

</project>

application.properties

spring.profiles.active=@activatedProperties@

application-prod.properties

vaadin.compatibilityMode=false
vaadin.servlet.productionmode=true

我感谢任何指导:)

所有代码都可以在项目公共存储库的主分支上找到:https://github.com/tlvlp/iot-portal

xiaoxue1006 回答:Vaadin + Spring Boot生产模式构建出现运行时错误:无法确定“ npm”工具

由于我最近也遇到了同样的困难,所以我有以下解决方案。我发现拥有Java和Nodejs / NPM的最简单方法是将其安装到Java基本映像中。这将不是一个小的docker映像。 mvn clean package -Pproduction vaadin根目录。然后编辑Dockerfile以包括如下安装:https://gitlab.com/snippets/1913782

然后构建并运行映像。构建时间和图像大小将增加很多。我希望看到一些建议或在此过程中取得的其他进展。从Vaadin8到14,在不得不了解为什么非Java事物被破坏的过程中,造成了许多麻烦。

,

我终于设法找到了解决问题的办法。 正确的假设是运行生产应用程序不需要npm。 尚不清楚from even the official guide:尽管将以下属性添加到prod构建配置文件中确实会填充到已构建的jar文件中,但是

本身不会触发Vaadin在生产模式下运行(至少不与SpringBoot一起运行):

<vaadin.productionMode>true</vaadin.productionMode>

解决方案:

最终解决问题的方法是在SpringBoot属性文件中也分配相同参数的值。 然后,它开始在构建时使用通过npm生成的资产。

这是 application.properties 文件(由dev和prod属性文件共享)。

spring.profiles.active=@spring.activatedProperties@
vaadin.productionMode=@vaadin.productionMode@
vaadin.compatibilityMode=false

这是完整且已更新且可以使用的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.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tlvlp</groupId>
    <artifactId>iot-portal</artifactId>
    <version>0.0.2</version>
    <name>iot-portal</name>
    <description>tlvlp IoT server portal</description>

    <properties>
        <java.version>11</java.version>
        <vaadin.version>14.0.12</vaadin.version>
        <dockerfile.maven.version>1.4.13</dockerfile.maven.version>
        <flow.server.prod.version>2.0.17</flow.server.prod.version>
        <!--    DOCKER IMAGE ARGS   -->
        <docker.project.repository>tlvlp/iot-portal</docker.project.repository>
        <service.port>8600</service.port>
    </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>com.vaadin</groupId>
            <artifactId>vaadin-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>${vaadin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

        </plugins>
    </build>

    <profiles>
        <profile>
            <id>prod</id>
            <properties>
                <spring.activatedProperties>prod</spring.activatedProperties>
                <vaadin.productionMode>true</vaadin.productionMode>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>com.vaadin</groupId>
                    <artifactId>flow-server-production-mode</artifactId>
                    <version>${flow.server.prod.version}</version>
                </dependency>
            </dependencies>

            <build>
                <plugins>
                    <plugin>
                        <groupId>com.vaadin</groupId>
                        <artifactId>flow-maven-plugin</artifactId>
                        <version>${flow.server.prod.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>prepare-frontend</goal>
                                    <goal>build-frontend</goal>

                                </goals>
                                <phase>compile</phase>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.spotify</groupId>
                        <artifactId>dockerfile-maven-plugin</artifactId>
                        <version>${dockerfile.maven.version}</version>
                        <executions>
                            <execution>
                                <id>prod</id>
                                <goals>
                                    <goal>build</goal>
                                    <goal>push</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <repository>${docker.project.repository}</repository>
                            <tag>${project.version}</tag>
                            <tag>latest</tag>
                            <buildArgs>
                                <PROJECT>${project.groupId}.${project.artifactId}</PROJECT>
                                <SERVICE_PORT>${service.port}</SERVICE_PORT>
                                <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                            </buildArgs>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>dev</id>
            <properties>
                <spring.activatedProperties>dev</spring.activatedProperties>
                <vaadin.productionMode>false</vaadin.productionMode>
            </properties>
        </profile>
    </profiles>

</project>
本文链接:https://www.f2er.com/3128262.html

大家都在问