vue通过maven自动打包成jar

7 篇文章 0 订阅

在vue项目下新建pom.xml

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <configuration>
          <skip>${skipDeploy}</skip>
        </configuration>
        <executions>
          <!--1. 执行npm install-->
          <!--如果node_modules安装过了,将第一步执行npm install注销(建议自己用命令安装好node_modules)-->
<!--          <execution>-->
<!--            <id>exec-npm-install</id>-->
<!--            <phase>generate-resources</phase>-->
<!--            <goals>-->
<!--              <goal>exec</goal>-->
<!--            </goals>-->
<!--            <configuration>-->
<!--              <executable>npm</executable>-->
<!--              <arguments>-->
<!--                <argument>install</argument>-->
<!--              </arguments>-->
<!--              <workingDirectory>${basedir}</workingDirectory>--> <!--你的项目目录-->
<!--            </configuration>-->
<!--          </execution>-->

          <!--2. 执行npm build-->
          <execution>
            <id>exec-npm-run-build</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <executable>npm</executable>
              <arguments>
                <argument>run</argument>
                <argument>build</argument>
              </arguments>
              <workingDirectory>${basedir}</workingDirectory><!--你的项目目录-->
            </configuration>
          </execution>

     <!--3. dist自动复制到spring boot的resources目录-->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <configuration>
          <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
        <executions>
          <execution>
            <id>copy-spring-boot-webapp</id>
            <phase>initialize</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <encoding>utf-8</encoding>
              <!--将dist复制到springboot resources/static目录下-->
            <outputDirectory>${project.parent.basedir}/component/iprobemgr/src/main/resources/static</outputDirectory>
              <resources>
                <resource>
                  <directory>${basedir}/dist</directory>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
        </executions>
      </plugin>
    </plugins>
  </build>



新建好后,选中pom右击add到maven,然后直接package或者install即可。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Vue应用打包jar,并在Spring Boot项目中引用,您可以使用frontend-maven-plugin插件来执行这个任务。下面是一些步骤可以帮助您实现这个目标: 1. 在您的Vue项目根目录下创建一个`pom.xml`文件,并添加以下配置: ```xml <build> <plugins> <!-- frontend-maven-plugin配置 --> <plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> <version>1.12.0</version> <executions> <!-- 安装 Node.js 和 NPM --> <execution> <id>install node and npm</id> <goals> <goal>install-node-and-npm</goal> </goals> <configuration> <nodeVersion>v14.17.3</nodeVersion> <npmVersion>6.14.13</npmVersion> </configuration> </execution> <!-- 安装 Vue 依赖 --> <execution> <id>npm install</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>install</arguments> </configuration> </execution> <!-- 执行 Vue 打包 --> <execution> <id>npm run build</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>run build</arguments> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 请注意,您需要根据您的项目需求调整`nodeVersion`和`npmVersion`的版本号。 2. 打开终端并导航到您的Vue项目根目录,运行以下命令将Vue应用打包: ```shell mvn clean install ``` 这个命令将触发frontend-maven-plugin插件的执行,它将安装Node.js和NPM,安装Vue依赖,并执行Vue打包。 3. 打包功后,您将在`target`目录下找到生jar文件。将该jar文件复制到您的Spring Boot项目中,并将其作为依赖添加到Spring Boot项目的`pom.xml`文件中。 ```xml <dependency> <groupId>your.groupId</groupId> <artifactId>your-artifactId</artifactId> <version>your-version</version> <scope>runtime</scope> </dependency> ``` 请记得将`your.groupId`、`your-artifactId`和`your-version`替换为您实际的值。 4. 在Spring Boot项目中,您可以使用`org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration`类中的`addResourceHandlers()`方法来配置静态资源的处理。添加以下代码: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/") .setCachePeriod(0); } } ``` 这将使Spring Boot能够正确地处理静态资源路径。 现在,您可以重新构建并运行Spring Boot项目Vue应用将作为静态资源打包jar中,并能够在Spring Boot应用中访问。 请注意,这只是一种将Vue应用打包jar并在Spring Boot项目中引用的方法。根据您的具体需求和项目结构,可能需要进行一些调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值