maven+springboot+npm+vue 创建项目

本文是一个项目下多个模块,创建父模块,然后创建子模块。eg:项目名为demo,创建模块supervisor为父模块,其下包括两个子模块supervisor-api(springboot项目)、supervisor-web(npm项目)

  • 选中项目,选择File->New Module->Maven,next后填入group和artifactId后finish
    在这里插入图片描述
    在这里插入图片描述
  • 父模块的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>
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <groupId>com.zhanglu.demo</groupId>
    <artifactId>supervisor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <modules>
        <module>supervisor-api</module>
        <module>supervisor-web</module>
    </modules>
   
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

注:1.由于子模块需要用到springboot,所以父模块中prarent需要引入springboot,子模块的parent指向父模块
2.子模块如果需要继承父模块中的dependency,父模块中的依赖需要括中dependencyManagement中
3.父子模块的关系是在pom.xml中体现的,父pom中需要在modules中引入module(子模块的artifactId名),子POM需要在parent中指定父模块的group及artifactId

1 创建springboot模块

  • 创建子模块supervisor-api(springboot)有两种方法,第一可以直接通过idea中的File->New Module->Spring Initializr创建,next->输入group和artifact,其中Type中选择Maven POM,即通过Maven pom Generate,然后选中需要用到的spring其它模块finsh即可,也可以通过https://start.spring.io直接gernerator。
    在这里插入图片描述
    在这里插入图片描述
  • 子模块的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>

    <parent>
        <groupId>com.zhanglu.demo</groupId>
        <artifactId>supervisor</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.zhanglu.demo</groupId>
    <artifactId>supervisor-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>supervisor-api</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</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-mongodb</artifactId>
        </dependency>
    </dependencies>

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

</project>

注:如果pom中不配置web的依赖,springboot正常启动后会退出,报出Process finished with exit code 0。填 加web的依赖后,会默认其用web容器启动,就不会再退出了。

2.npm 创建web模块

  • 首先需要安装node环境,从nodejs.org中下载nodejs(自行百度),安装成功后可用node -v 和 npm -v 查看版本信息
    node会自带npm,也可以通过命令npm install -g npm对npm的版本升级。(在接下来的使用中,如果npm慢的话,可百度使用taobao镜像npm install -g cnpm --registry=https://registry.npm.taobao.org)

  • 全局安装vue-cli :npm install vue-cli -g (可用vue list查看vue-cli是否安装成功)

  • 进入项目路径supervisor,通过命令vue init webpack 项目名。eg:vue init webpack supervisor-web,据说红框中如果不选no的话,后面编写项目时会有很多警告,请酌情选择。
    在这里插入图片描述

  • 然后执行npm install来安装依赖
    在这里插入图片描述

  • 运行项目npm run dev
    在这里插入图片描述

  • 如果可以访问到了vue的页面即成功了
    在这里插入图片描述

  • npm run build:然后此时你会发现项目下多了一个 dist 文件夹,dist下文件便是项目打包之后生成的文件

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Maven 搭建 Spring Boot + Vue3 项目的步骤: 1. 创建一个 Maven 项目 在你的 IDE 中创建一个 Maven 项目,可以选择使用 Spring Initializr 快速生成一个 Spring Boot 项目骨架。 2. 添加 Spring Boot 依赖 在 pom.xml 文件中添加 Spring Boot 的依赖,例如: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 3. 添加 Vue3 依赖 在 pom.xml 中添加 Vue3 的依赖,例如: ``` <dependency> <groupId>org.webjars.npm</groupId> <artifactId>vue@3.0.0</artifactId> <version>3.0.0</version> </dependency> ``` 注意:这里使用了 WebJars,需要在项目中添加 WebJars 的依赖。 4. 配置 Maven 插件 在 pom.xml 文件中配置 Maven 插件,用于编译 Vue3 代码。例如: ``` <build> <plugins> <plugin> <groupId>com.github.eirslett</groupId> <artifactId>frontend-maven-plugin</artifactId> <version>1.10.0</version> <executions> <execution> <id>install node and npm</id> <goals> <goal>install-node-and-npm</goal> </goals> <configuration> <nodeVersion>v12.16.2</nodeVersion> <npmVersion>6.14.4</npmVersion> </configuration> </execution> <execution> <id>npm install</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>install</arguments> </configuration> </execution> <execution> <id>npm build</id> <goals> <goal>npm</goal> </goals> <configuration> <arguments>run build</arguments> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 这里使用了 frontend-maven-plugin 插件来编译 Vue3 代码。 5. 创建 Vue3 项目项目根目录下创建一个名为 vue 的文件夹,用于存放 Vue3 项目代码。 在 vue 文件夹下执行以下命令,创建一个 Vue3 项目: ``` vue create . ``` 这里使用了点号(.)表示当前目录。 6. 配置 Vue3 项目vue 文件夹下创建一个 vue.config.js 文件,用于配置 Vue3 项目。例如: ``` module.exports = { outputDir: "../src/main/resources/static", indexPath: "../static/index.html", devServer: { proxy: { "/api": { target: "http://localhost:8080" } } } } ``` 这里将 Vue3 项目的输出目录设置为 src/main/resources/static,将 Vue3 项目的入口文件设置为 static/index.html,并配置了代理,将所有以 /api 开头的请求转发到 http://localhost:8080。 7. 编译项目项目根目录下执行以下命令,编译项目: ``` mvn clean install ``` 这里会自动编译 Vue3 项目,并将编译后的代码打包到 jar 包中。 8. 运行项目 在 target 目录下找到生成的 jar 包,执行以下命令,运行项目: ``` java -jar your-project.jar ``` 这里需要将 your-project.jar 替换为实际的 jar 包名称。 9. 访问应用 在浏览器中访问 http://localhost:8080,即可看到运行的应用。 以上就是使用 Maven 搭建 Spring Boot + Vue3 项目的步骤,希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值