使用 Maven 插件 frontend-maven-plugin 合并打包前后端分离项目

前后端分离下,前端打包后需要再手动将文件复制到后端项目的 src/main/resources/static 目录下,最后打包后端项目。这里使用 Maven 插件 frontend-maven-plugin 进行前后端合并打包。

下面以 Spring Boot + Vue 为例:

GitHub shpunishment/spring-boot-vue-demo

项目结构
项目结构

最外层pom.xml,注意modules中将前端项目放前面

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.shpun</groupId>
    <artifactId>spring-boot-vue-test</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <modules>
        <module>user-frontend</module>
        <module>user-api</module>
    </modules>
</project>

以下均在前端项目下修改:

在 src/utils 下创建文件cleanFile.js,fileCopy.js,fileUtils.js

cleanFile.js:用于打包前删除包

const fs = require('fs')
const p = require('path')

const nodeModulesPath = p.join(__dirname, '../../node_modules')
const lockJsonPath = p.join(__dirname, '../../package-lock.json')

if (fs.existsSync(nodeModulesPath)) {
  const fileUtil = require('./fileUtil')

  fileUtil.deleteFolderByRimraf(nodeModulesPath)
  console.log('删除 node_modules 成功!')
  
  fileUtil.deleteFile(lockJsonPath)
  console.log('删除 package-lock.json 成功!')
}

fileCopy.js:将前端打包后的文件复制到后端项目 src/main/resources/static 下

const fileUtil = require('./fileUtil')

// 后端项目的文件夹名称,这里是 user-api
const backendProjectName = 'user-api'
// 目标文件夹
const staticDirectory = '../' + backendProjectName + '/src/main/resources/static/'
// 删除
fileUtil.deleteFolder(staticDirectory)
// 拷贝
fileUtil.copyFolder('./dist', staticDirectory)
console.log('文件拷贝成功!')

fileUtils.js:文件操作工具

const fs = require('fs')
const rimraf = require('rimraf');

/**
 * 删除文件夹
 * @param path
 */
function deleteFolder (path) {
  let files = [];
  if (fs.existsSync(path)) {
    if (fs.statSync(path).isDirectory()) {
      files = fs.readdirSync(path)
      files.forEach((file) => {
        const curPath = path + '/' + file;
        if (fs.statSync(curPath).isDirectory()) {
          deleteFolder(curPath)
        } else {
          fs.unlinkSync(curPath)
        }
      })
      fs.rmdirSync(path)
    } else {
      fs.unlinkSync(path)
    }
  }
}

/**
 * 使用 rimraf 删除文件夹
 * @param path
 */
function deleteFolderByRimraf (path) {
  rimraf(path, (err) => {
    if (err) {
      console.log(err)
    }
  })
}

/**
 * 删除文件
 * @param path
 */
function deleteFile (path) {
  if (fs.existsSync(path)) {
    if (fs.statSync(path).isDirectory()) {
      deleteFolder(path)
    } else {
      fs.unlinkSync(path)
    }
  }
}

/**
 * 复制文件夹到指定目录
 * @param from
 * @param to
 */
function copyFolder (from, to) {
  let files = []
  // 文件是否存在 如果不存在则创建
  if (fs.existsSync(to)) {
    files = fs.readdirSync(from)
    files.forEach((file) => {
      const targetPath = from + '/' + file;
      const toPath = to + '/' + file;

      // 复制文件夹
      if (fs.statSync(targetPath).isDirectory()) {
        copyFolder(targetPath, toPath)
      } else {
        // 拷贝文件
        fs.copyFileSync(targetPath, toPath)
      }
    })
  } else {
    fs.mkdirSync(to)
    copyFolder(from, to)
  }
}

module.exports = {
  deleteFolder,
  deleteFolderByRimraf,
  deleteFile,
  copyFolder
}

修改package.json,添加脚本

{
  ...
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "clean": "node src/utils/cleanFiles.js",
    "build-copy": "vue-cli-service build && node src/utils/fileCopy.js"
  },
  ...
}

修改前端项目 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.shpun</groupId>
  <artifactId>user-frontend</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>com.github.eirslett</groupId>
        <artifactId>frontend-maven-plugin</artifactId>
        <version>1.8.0</version>
        <executions>
          <!-- 检查是否安装node npm -->
          <execution>
            <id>install node and npm</id>
            <goals>
              <goal>install-node-and-npm</goal>
            </goals>
            <phase>generate-resources</phase>
          </execution>
          <!-- 安装rimraf -->
          <execution>
            <id>npm install rimraf</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
              <arguments>install rimraf --registry=https://registry.npm.taobao.org</arguments>
            </configuration>
          </execution>
          <!-- 执行脚本,删除node_modules和package-lock.json -->
          <execution>
            <id>npm run clean</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
              <arguments>run clean</arguments>
            </configuration>
          </execution>
          <!-- npm install -->
          <execution>
            <id>npm install</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
              <arguments>install --registry=https://registry.npm.taobao.org</arguments>
            </configuration>
          </execution>
          <!-- build 之后复制文件到 src/main/resource/static 下 -->
          <execution>
            <id>npm run build</id>
            <goals>
              <goal>npm</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
              <arguments>run build-copy</arguments>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <nodeVersion>v10.16.3</nodeVersion>
          <npmVersion>6.11.3</npmVersion>
          <!-- node安装路径 -->
          <installDirectory>${settings.localRepository}</installDirectory>
          <!-- 前端代码路径 -->
          <workingDirectory>${basedir}</workingDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

测试
在最外层执行mvn clean install,按序执行命令

检查node和npm是否安装
检查是否安装node npm
安装rimraf
安装rimraf
执行脚本,删除node_modules和package-lock.json
执行脚本,删除node_modules和package-lock.json
npm install
npm install
build 之后复制文件到 src/main/resource/static 下
build 之后复制文件到 src/main/resource/static 下

参考:
GitHub eirslett/frontend-maven-plugin
Spring Boot + Vue前后端分离项目,Maven自动打包整合
使用插件 frontend-maven-plugin,通过maven一键打包前端后端

如果您使用frontend-maven-plugin打包的jar文件中没有static文件夹,那可能是因为frontend-maven-plugin默认不会将静态资源文件包含在打包的jar文件中。为了解决这个问题,您可以尝试以下步骤: 1. 在您的Vue项目的根目录下创建一个名为`static`的文件夹,并将您的静态资源文件放置在其中。 2. 在您的Vue项目的根目录下创建一个名为`.npmrc`的文件,并添加以下内容: ``` prefix=${project.basedir}/target/${project.build.finalName}/static ``` 这将告诉frontend-maven-plugin将静态资源文件复制到打包的jar文件中的正确位置。 3. 更新您的pom.xml文件中frontend-maven-plugin插件的配置,确保它包含`copy`目标,并将`copy`目标的`outputDirectory`设置为`${project.build.directory}/${project.build.finalName}/static`。示例如下: ```xml <plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> <version>1.12.0</version> <executions> <!-- 其他执行目标 --> <!-- ... --> <!-- 添加 copy 目标 --> <execution> <id>copy files</id> <goals> <goal>copy</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/${project.build.finalName}/static</outputDirectory> <resources> <resource> <directory>static</directory> <includes> <include>**/*</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> ``` 4. 重新运行`mvn clean install`命令来重新打包您的应用。这将确保静态资源文件被正确地复制到打包的jar文件中的static文件夹中。 现在,您应该能够在打包后的jar文件中找到static文件夹并包含您的静态资源文件。请确保在使用这些静态资源文件时,使用正确的路径引用它们。 希望这能解决您的问题!如有其他疑问,请随时追问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值