参考
- https://blog.csdn.net/weixin_43726822/article/details/88562314
- https://cloud.tencent.com/developer/article/1870338 (将本地Jar包导入Maven项目的4种方式)
- 本文代码下载
环境
环境 | 版本 | 操作 |
---|---|---|
windows | 10 | |
vs code | 1.84.2 | |
Spring Boot Extension Pack | v0.2.1 | vscode插件 |
Extension Pack for Java | v0.25.15 | vscode插件 |
JDK | 11 | |
Springboot | 2.3.12.RELEASE | |
Apache Maven | 3.8.6 |
注意
- 通过 https://start.aliyun.com/ 应用架构选择
MVC架构
生成的代码,在打包以后运行 jar 报错:“没有主清单属性”
因为父工程和非 web 入口模块都在pom.xml
中设置了spring-boot-maven-plugin
,该配置只需要在 web 模块中设置,其他模块与父工程中的pom.xml
中都需要删除spring-boot-maven-plugin
的配置。(MVC架构
的 web 模块为 xxx-start)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.12.RELEASE</version>
<configuration>
<mainClass>com.example.demo.DemoApplication</mainClass>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
-
项目多模块好处
关于mvn多模块的理解内容和现在搜索到以及理解的内容差不多,可以参考一下。 -
父工程可以有n多个子模块,命名也不需要按照本文设置,本文这样设置是因为
start.aliyun.com
默认的MVC架构
生成的代码是这样。 -
Maven 项目有多种引入 jar 包方式。
- 指定绝对路径
- 将项目
mvn install
到本地,然后需要的项目pom.xml
中引入。 - ...
正文
父工程与子模块
一个父工程可以有多个子模块,目录结构大概如下。
..
pom.xml
demo-web
pom.xml
src
demo-start
pom.xml
src
demo-service
pom.xml
src
- 父工程 pom.xml 需要指定
packaging
为pom
(默认为jar
),以及引入子模块:
<packaging>pom</packaging>
<modules>
<module>demo-start</module>
<module>demo-web</module>
<module>demo-service</module>
</modules>
- 所有子模块都需要指定所属父工程(demo-web、demo-start、demo-service子模块的
pom.xml
中都需要设置):
<!--子模块名称,在引入的时候用到,如果是demo-start的pom.xml,那就设置为<artifactId>demo-start</artifactId>-->
<artifactId>xxx</artifactId>
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
打包
命令位置默认为:父工程目录
- 打包
mvn clean package
- 运行 jar
java -jar ./start/target/demo-start-0.0.1-SNAPSHOT.jar
- Springboot2 Springboot 模块 mavenspringboot2 springboot模块maven springboot2 springboot2 springboot springboot2 springboot swagger3 swagger springboot2 springboot mybatis springboot2 springboot lettuce redis springboot2 springboot spring3 spring springboot2 springboot后台 管理系统 springboot2 springcache springboot springboot2 springboot mybatis3 mybatis