关于使用Gradle结合SpringBoot进行多模块开发。
本来是打算使用buildSrc之类的,但是感觉好像好麻烦,使用这种方法就可以实现,没必要采用其他的。
我不怎么会表述,可能写的跟粑粑一样,哈哈哈哈
这是我的项目地址。
存在gitee,会一直更新
TinaSprout
首先这是模块的目录结构
C:\USERS\MI\CODESPACE\IDEAPROJECTS\TINASPROUT
│ .gitignore
│ build.gradle
│ gradlew
│ gradlew.bat
│ LICENSE
│ settings.gradle
│
├─.gradle
│ ├─7.6.1
│ │ │ gc.properties
│ │ │
│ │ ├─checksums
│ │ │ checksums.lock
│ │ │ md5-checksums.bin
│ │ │ sha1-checksums.bin
│ │ │
│ │ ├─dependencies-accessors
│ │ │ dependencies-accessors.lock
│ │ │ gc.properties
│ │ │
│ │ ├─executionHistory
│ │ │ executionHistory.bin
│ │ │ executionHistory.lock
│ │ │
│ │ ├─fileChanges
│ │ │ last-build.bin
│ │ │
│ │ ├─fileHashes
│ │ │ fileHashes.bin
│ │ │ fileHashes.lock
│ │ │ resourceHashesCache.bin
│ │ │
│ │ └─vcsMetadata
│ ├─buildOutputCleanup
│ │ buildOutputCleanup.lock
│ │ cache.properties
│ │ outputFiles.bin
│ │
│ └─vcs-1
│ gc.properties
│
├─.idea
│ │ compiler.xml
│ │ git_toolbox_prj.xml
│ │ gradle.xml
│ │ jarRepositories.xml
│ │ misc.xml
│ │ modules.xml
│ │ vcs.xml
│ │ workspace.xml
│ │
│ └─modules
│ ├─tinasprout-core
│ │ TinaSprout.tinasprout-core.main.iml
│ │
│ └─tinasprout-robot
│ TinaSprout.tinasprout-robot.main.iml
│
├─gradle
│ └─wrapper
│ gradle-wrapper.jar
│ gradle-wrapper.properties
│
├─tinasprout-core
│ │ .gitignore
│ │ build.gradle
│ │ gradlew
│ │ gradlew.bat
│ │
│ ├─gradle
│ │ └─wrapper
│ │ gradle-wrapper.jar
│ │ gradle-wrapper.properties
│ │
│ └─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─wuxianggujun
│ │ │ └─tinasproutcore
│ │ │ │ TinaSproutCoreApplication.java
│ │ │ │
│ │ │ ├─config
│ │ │ │ BotConfig.java
│ │ │ │
│ │ │ └─network
│ │ │ WsHandler.java
│ │ │
│ │ └─resources
│ │ │ application.properties
│ │ │
│ │ ├─static
│ │ └─templates
│ └─test
│ └─java
│ └─com
│ └─wuxianggujun
│ └─tinasproutcore
│ TinaSproutCoreApplicationTests.java
│
└─tinasprout-robot│ .gitignore│ build.gradle│ gradlew│ gradlew.bat│├─gradle│ └─wrapper│ gradle-wrapper.jar│ gradle-wrapper.properties│└─src├─main│ ├─java│ │ └─com│ │ └─wuxianggujun│ │ └─tinasproutrobot│ │ TinasproutRobotApplication.java│ ││ └─resources│ │ application.properties│ ││ ├─static│ └─templates└─test└─java└─com└─wuxianggujun└─tinasproutrobotTinasproutRobotApplicationTests.java
步骤就是,现在创建一个Gradle项目,取名为TinaSprout(当然,这是我项目的名字,你们随便取)然后删除src的目录,用不到。
然后新建两个子模块tinasprout-core、tinasprout-robot。
tinasprout-core ----- 是用来写核心框架的。
tinasprout-robot -----是SpringBoot启动项目。
将两个子模块的settings.gradle删除,不需要。
完成之后开始编写我们根目录的settings.gradle文件,将两个子模块引入。
rootProject.name = 'TinaSprout'
include 'tinasprout-core'
include 'tinasprout-robot'
现在我们开始编写根目录的build.gradle,用于管理子模块
buildscript {repositories {mavenLocal()maven {url 'https://repo.huaweicloud.com/repository/maven/'}}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:3.0.4")}
}plugins {id 'java'id 'org.springframework.boot' version '3.0.4' apply false//apply false的作用是告诉Gradle不要执行插件的逻辑,只是声明依赖id 'io.spring.dependency-management' version '1.1.0'
}allprojects {apply plugin: 'java'group 'com.wuxianggujun'version '1.0-SNAPSHOT'sourceCompatibility = '17'configurations {compileOnly {extendsFrom annotationProcessor}}repositories {mavenCentral()maven {url 'https://repo.huaweicloud.com/repository/maven/'}}dependencies {testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'}tasks.withType(JavaCompile) {options.encoding = "UTF-8"}}subprojects {apply plugin: 'org.springframework.boot'apply plugin: 'io.spring.dependency-management'dependencyManagement {imports {mavenBom "org.springframework.boot:spring-boot-dependencies:3.0.4"}}dependencies {implementation 'org.springframework.boot:spring-boot-starter'implementation 'org.springframework.boot:spring-boot-starter-web'compileOnly 'org.projectlombok:lombok'developmentOnly 'org.springframework.boot:spring-boot-devtools'annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'annotationProcessor 'org.projectlombok:lombok'testImplementation 'org.springframework.boot:spring-boot-starter-test'}tasks.named('test') {useJUnitPlatform()}}
通过上面的build.gradle配置文件,设置了allprojects和subprojects用于定义一些共用的内容。反正不难,应该看得懂吧!
接下来是tinasprout-core,我是想将这个打包成jar包,供给tinasprout-robot使用。所以指定bootjar的enabled为false,jar 的enabled为true
bootJar{enabled(false)archiveBaseName = 'tinasprout-core'archiveVersion = '0.0.1-SNAPSHOT'
}jar{enabled = true
}dependencies {implementation "com.alibaba:fastjson:1.2.83"implementation 'io.netty:netty-all:4.1.86.Final'
}
接下来,我们看tinasprout-robot的配置文件,这是用于部署在服务器启动SpringBoot的。
springBoot {mainClass = 'com.wuxianggujun.tinasproutrobot.TinasproutRobotApplication'
}bootJar {archiveBaseName = 'TinaSprout'archiveVersion = '0.0.1-SNAPSHOT'// 将子模块一起打包进来includeEmptyDirs = true// 设置输出目录,相对于项目根目录destinationDirectory = file("$rootDir/build")
}dependencies {implementation project(':tinasprout-core')
}
指定springBoot的mainClass,用于在jar启动的时候可以运行springboot。
bootJar的一些属性,是我自己假的,当然你们也可以删除,不影响。
我是要添加这些是要将这个jar包,部署在服务器,所以修改名称输出在根目录的build文件下。
通过运行bootjar将SpringBoot项目打包成jar包,我们就可以在build目录中看见。
现在项目模块化已经完成了,我也不知道应该搞什么。直接来运行看看效果吧。
打开终端,将文件夹拖动到当前目录,我们使用命令来查看一下jar包。
使用这条命令查看,这个命令用于查看一个 JAR 文件中包含的所有文件和目录,可以用来确认 JAR 文件中是否包含了需要的类和资源文件。
jar tf .\TinaSprout-0.0.1-SNAPSHOT.jar //其他命令
jar tf <jar包> | grep 'BOOT-INF/lib'
jar tf <jar包> | grep 'BOOT-INF/classes'
这是输出内容,可以看到tinasprout-core-1.0-SNAPSHOT-plain已经被打包进去了。
META-INF/
META-INF/MANIFEST.MF
org/
org/springframework/
org/springframework/boot/
org/springframework/boot/loader/
org/springframework/boot/loader/ClassPathIndexFile.class
org/springframework/boot/loader/ExecutableArchiveLauncher.class
org/springframework/boot/loader/JarLauncher.class
org/springframework/boot/loader/LaunchedURLClassLoader$DefinePackageCallType.class
org/springframework/boot/loader/LaunchedURLClassLoader$UseFastConnectionExceptionsEnumeration.class
org/springframework/boot/loader/LaunchedURLClassLoader.class
org/springframework/boot/loader/Launcher.class
org/springframework/boot/loader/MainMethodRunner.class
org/springframework/boot/loader/PropertiesLauncher$ArchiveEntryFilter.class
org/springframework/boot/loader/PropertiesLauncher$ClassPathArchives.class
org/springframework/boot/loader/PropertiesLauncher$PrefixMatchingArchiveFilter.class
org/springframework/boot/loader/PropertiesLauncher.class
org/springframework/boot/loader/WarLauncher.class
org/springframework/boot/loader/archive/
org/springframework/boot/loader/archive/Archive$Entry.class
org/springframework/boot/loader/archive/Archive$EntryFilter.class
org/springframework/boot/loader/archive/Archive.class
org/springframework/boot/loader/archive/ExplodedArchive$AbstractIterator.class
org/springframework/boot/loader/archive/ExplodedArchive$ArchiveIterator.class
org/springframework/boot/loader/archive/ExplodedArchive$EntryIterator.class
org/springframework/boot/loader/archive/ExplodedArchive$FileEntry.class
org/springframework/boot/loader/archive/ExplodedArchive$SimpleJarFileArchive.class
org/springframework/boot/loader/archive/ExplodedArchive.class
org/springframework/boot/loader/archive/JarFileArchive$AbstractIterator.class
org/springframework/boot/loader/archive/JarFileArchive$EntryIterator.class
org/springframework/boot/loader/archive/JarFileArchive$JarFileEntry.class
org/springframework/boot/loader/archive/JarFileArchive$NestedArchiveIterator.class
org/springframework/boot/loader/archive/JarFileArchive.class
org/springframework/boot/loader/data/
org/springframework/boot/loader/data/RandomAccessData.class
org/springframework/boot/loader/data/RandomAccessDataFile$DataInputStream.class
org/springframework/boot/loader/data/RandomAccessDataFile$FileAccess.class
org/springframework/boot/loader/data/RandomAccessDataFile.class
org/springframework/boot/loader/jar/
org/springframework/boot/loader/jar/AbstractJarFile$JarFileType.class
org/springframework/boot/loader/jar/AbstractJarFile.class
org/springframework/boot/loader/jar/AsciiBytes.class
org/springframework/boot/loader/jar/Bytes.class
org/springframework/boot/loader/jar/CentralDirectoryEndRecord$Zip64End.class
org/springframework/boot/loader/jar/CentralDirectoryEndRecord$Zip64Locator.class
org/springframework/boot/loader/jar/CentralDirectoryEndRecord.class
org/springframework/boot/loader/jar/CentralDirectoryFileHeader.class
org/springframework/boot/loader/jar/CentralDirectoryParser.class
org/springframework/boot/loader/jar/CentralDirectoryVisitor.class
org/springframework/boot/loader/jar/FileHeader.class
org/springframework/boot/loader/jar/Handler.class
org/springframework/boot/loader/jar/JarEntry.class
org/springframework/boot/loader/jar/JarEntryCertification.class
org/springframework/boot/loader/jar/JarEntryFilter.class
org/springframework/boot/loader/jar/JarFile$1.class
org/springframework/boot/loader/jar/JarFile$JarEntryEnumeration.class
org/springframework/boot/loader/jar/JarFile.class
org/springframework/boot/loader/jar/JarFileEntries$1.class
org/springframework/boot/loader/jar/JarFileEntries$EntryIterator.class
org/springframework/boot/loader/jar/JarFileEntries$Offsets.class
org/springframework/boot/loader/jar/JarFileEntries$Zip64Offsets.class
org/springframework/boot/loader/jar/JarFileEntries$ZipOffsets.class
org/springframework/boot/loader/jar/JarFileEntries.class
org/springframework/boot/loader/jar/JarFileWrapper.class
org/springframework/boot/loader/jar/JarURLConnection$1.class
org/springframework/boot/loader/jar/JarURLConnection$JarEntryName.class
org/springframework/boot/loader/jar/JarURLConnection.class
org/springframework/boot/loader/jar/StringSequence.class
org/springframework/boot/loader/jar/ZipInflaterInputStream.class
org/springframework/boot/loader/jarmode/
org/springframework/boot/loader/jarmode/JarMode.class
org/springframework/boot/loader/jarmode/JarModeLauncher.class
org/springframework/boot/loader/jarmode/TestJarMode.class
org/springframework/boot/loader/util/
org/springframework/boot/loader/util/SystemPropertyUtils.class
BOOT-INF/
BOOT-INF/classes/
BOOT-INF/classes/com/
BOOT-INF/classes/com/wuxianggujun/
BOOT-INF/classes/com/wuxianggujun/tinasproutrobot/
BOOT-INF/classes/com/wuxianggujun/tinasproutrobot/TinasproutRobotApplication.class
BOOT-INF/classes/application.properties
BOOT-INF/lib/
BOOT-INF/lib/tinasprout-core-1.0-SNAPSHOT-plain.jar
BOOT-INF/lib/spring-boot-autoconfigure-3.0.4.jar
BOOT-INF/lib/spring-boot-3.0.4.jar
BOOT-INF/lib/jakarta.annotation-api-2.1.1.jar
BOOT-INF/lib/spring-webmvc-6.0.6.jar
BOOT-INF/lib/spring-web-6.0.6.jar
BOOT-INF/lib/spring-context-6.0.6.jar
BOOT-INF/lib/spring-aop-6.0.6.jar
BOOT-INF/lib/spring-beans-6.0.6.jar
BOOT-INF/lib/spring-expression-6.0.6.jar
BOOT-INF/lib/spring-core-6.0.6.jar
BOOT-INF/lib/snakeyaml-1.33.jar
BOOT-INF/lib/fastjson-1.2.83.jar
BOOT-INF/lib/netty-all-4.1.89.Final.jar
BOOT-INF/lib/logback-classic-1.4.5.jar
BOOT-INF/lib/log4j-to-slf4j-2.19.0.jar
BOOT-INF/lib/jul-to-slf4j-2.0.6.jar
BOOT-INF/lib/spring-jcl-6.0.6.jar
BOOT-INF/lib/jackson-datatype-jsr310-2.14.2.jar
BOOT-INF/lib/jackson-module-parameter-names-2.14.2.jar
BOOT-INF/lib/jackson-annotations-2.14.2.jar
BOOT-INF/lib/jackson-core-2.14.2.jar
BOOT-INF/lib/jackson-datatype-jdk8-2.14.2.jar
BOOT-INF/lib/jackson-databind-2.14.2.jar
BOOT-INF/lib/tomcat-embed-websocket-10.1.5.jar
BOOT-INF/lib/tomcat-embed-core-10.1.5.jar
BOOT-INF/lib/tomcat-embed-el-10.1.5.jar
BOOT-INF/lib/micrometer-observation-1.10.4.jar
BOOT-INF/lib/netty-transport-native-epoll-4.1.89.Final-linux-x86_64.jar
BOOT-INF/lib/netty-transport-native-epoll-4.1.89.Final-linux-aarch_64.jar
BOOT-INF/lib/netty-transport-native-kqueue-4.1.89.Final-osx-x86_64.jar
BOOT-INF/lib/netty-transport-native-kqueue-4.1.89.Final-osx-aarch_64.jar
BOOT-INF/lib/netty-transport-classes-epoll-4.1.89.Final.jar
BOOT-INF/lib/netty-transport-classes-kqueue-4.1.89.Final.jar
BOOT-INF/lib/netty-resolver-dns-native-macos-4.1.89.Final-osx-x86_64.jar
BOOT-INF/lib/netty-resolver-dns-native-macos-4.1.89.Final-osx-aarch_64.jar
BOOT-INF/lib/netty-resolver-dns-classes-macos-4.1.89.Final.jar
BOOT-INF/lib/netty-resolver-dns-4.1.89.Final.jar
BOOT-INF/lib/netty-handler-4.1.89.Final.jar
BOOT-INF/lib/netty-transport-native-unix-common-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-dns-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-4.1.89.Final.jar
BOOT-INF/lib/netty-transport-4.1.89.Final.jar
BOOT-INF/lib/netty-buffer-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-haproxy-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-http-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-http2-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-memcache-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-mqtt-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-redis-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-smtp-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-socks-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-stomp-4.1.89.Final.jar
BOOT-INF/lib/netty-codec-xml-4.1.89.Final.jar
BOOT-INF/lib/netty-resolver-4.1.89.Final.jar
BOOT-INF/lib/netty-common-4.1.89.Final.jar
BOOT-INF/lib/netty-handler-proxy-4.1.89.Final.jar
BOOT-INF/lib/netty-handler-ssl-ocsp-4.1.89.Final.jar
BOOT-INF/lib/netty-transport-rxtx-4.1.89.Final.jar
BOOT-INF/lib/netty-transport-sctp-4.1.89.Final.jar
BOOT-INF/lib/netty-transport-udt-4.1.89.Final.jar
BOOT-INF/lib/logback-core-1.4.5.jar
BOOT-INF/lib/slf4j-api-2.0.6.jar
BOOT-INF/lib/log4j-api-2.19.0.jar
BOOT-INF/lib/micrometer-commons-1.10.4.jar
BOOT-INF/classes/static/
BOOT-INF/classes/templates/
BOOT-INF/lib/spring-boot-jarmode-layertools-3.0.4.jar
BOOT-INF/classpath.idx
BOOT-INF/layers.idx
接下来,运行看看
java -jar .\TinaSprout-0.0.1-SNAPSHOT.jar
输入命令之后可以看到。项目已经成功的被启动了。