首页 > 解决方案 > Spring Boot - 在 docker 容器内构建 maven 项目(JAR 文件)

问题描述

我有一个运行具有以下文件夹结构的 VueJS 应用程序的 Spring Boot 项目:

root
+-- client
|   +-- [vue app]
|   +-- pom.xml
+-- server
|   +-- [spring boot app]
|   +-- pom.xml
+-- pom.xml

在构建应用程序时,client我正在运行以下命令。pom.xml该脚本正在安装npm并运行npm run build以创建dist/要在下一个代码片段中复制的文件夹。

<plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>${frontend-maven-plugin.version}</version>
    <executions>
        <!-- Install our node and npm version to run npm/node scripts-->
        <execution>
            <id>install node and npm</id>
            <goals>
                <goal>install-node-and-npm</goal>
            </goals>
            <configuration>
                <nodeVersion>v11.5.0</nodeVersion>
            </configuration>
        </execution>
        <!-- Install all project dependencies -->
        <execution>
            <id>npm install</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <!-- optional: default phase is "generate-resources" -->
            <phase>generate-resources</phase>
            <!-- Optional configuration which provides for running any npm command -->
            <configuration>
                <arguments>install</arguments>
            </configuration>
        </execution>
        <!-- Build and minify static files -->
        <execution>
            <id>npm run build</id>
            <goals>
                <goal>npm</goal>
            </goals>
            <configuration>
                <arguments>run build</arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

在目录中的pom.xml-file 中,server我有以下配置将资源从类复制client/dist/到类路径的静态资产server

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
    <execution>
        <!-- Copy Vue files to src-folder in server -->
        <id>copy Vue.js frontend content</id>
        <phase>generate-resources</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>src/main/resources/public</outputDirectory>
            <overwrite>true</overwrite>
            <resources>
                <resource>
                    <directory>${project.parent.basedir}/client/dist</directory>
                    <includes>
                        <!-- Make sure assets are added to static/ folder in vue.config.js-->
                        <include>static/</include>
                        <include>index.html</include>
                        <include>favicon/</include>>
                    </includes>
                </resource>
            </resources>
        </configuration>
    </execution>
</executions>
</plugin>

最终目标是让应用程序通过 jenkins 管道构建到 docker 容器中。

但是,我的问题是,由于生产设置,我无法通过 maven 下载或安装 node / npm(由于安全原因,所有外部包都被阻止)。

因此,我需要一种构建 vue 应用程序的方法,并在创建.jar文件时将文件复制到类路径,而无需通过 maven 实际执行此操作。

一个想法是使用node已安装的 docker 映像并通过 docker 映像进行构建。但是,我对此的实际方法一无所知。如果有人有一个想法,将不胜感激。

标签: javamavenspring-bootdockerjenkins

解决方案


推荐阅读