使用gradle进行springboot的多模块开发,每个模块都单独打包成独立的jar,只有主模块有main方法,在运行时候以lib形式引用子模块,如何配置?
时间: 2024-03-08 19:49:05 浏览: 83
对于使用Gradle进行SpringBoot多模块开发,每个模块都单独打包成独立的jar,只有主模块有main方法,在运行时候以lib形式引用子模块,可以按照以下步骤进行配置:
1.在主模块(也是有main方法的模块)的build.gradle文件中,添加如下配置:
```
plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}
group 'com.example'
version '0.0.1-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation project(':module1')
implementation project(':module2')
}
bootJar {
baseName = 'main-module'
}
jar {
manifest {
attributes 'Main-Class': 'com.example.MainApplication'
}
}
```
其中,implementation project(':module1')和implementation project(':module2')表示引入子模块1和子模块2,baseName = 'main-module'表示打包后的jar文件名为main-module.jar。
2.在子模块的build.gradle文件中,添加如下配置:
```
plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}
group 'com.example'
version '0.0.1-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
// 其他依赖
}
bootJar {
baseName = '子模块名'
}
jar {
enabled = true
}
```
其中,implementation 'org.springframework.boot:spring-boot-starter-web'和其他dependencies表示子模块的依赖,baseName = '子模块名'表示打包后的jar文件名为子模块名.jar。
3.使用Gradle命令进行打包,在主模块的根目录下执行以下命令:
```
./gradlew clean bootJar
```
执行后,会在主模块的build/libs目录下生成main-module.jar文件,同时在子模块的build/libs目录下生成子模块的jar文件。
4.在运行时以lib形式引用子模块,可以在主模块的运行脚本中添加如下配置:
```
java -cp main-module.jar:module1.jar:module2.jar com.example.MainApplication
```
其中,main-module.jar是主模块的jar文件名,module1.jar和module2.jar是子模块的jar文件名,com.example.MainApplication是主模块中的main方法所在的类。这样就可以在运行时以lib形式引用子模块了。
阅读全文
相关推荐

















