這篇文章給大家介紹Gradle中怎么構建一個SpringBoot多模塊項目,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
1. settings.gradle
/** * rootProject.name 項目名稱 * include 模塊名稱 */ rootProject.name = 'micro-service-framework' include 'framework-base' include 'framework-web' include 'framework-redis'
2. build.gradle
這個相當于maven中的父maven配置
buildscript { ext { //spring boot 版本 bootVersion = '2.0.6.RELEASE' } //私服地址,這個地址適用于gradle自身,比如刪除,下面的springboot插件就會找不到 repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } } //springboot gradle插件配置 dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${bootVersion}") } } allprojects { //導入使用的插件 apply plugin: 'java' apply plugin: 'maven' //如果導入該插件,你需要指定main class,否則不能打包 //apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' //這個插件用于發布jar包到私服 apply plugin: 'maven-publish' //jdk編譯版本 sourceCompatibility = 1.8 //jar包的group ,version配置 group 'net.178le.micro' version '0.0.1-SNAPSHOT' //私服地址, repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } } /** * 導入了springboot,spring cloud的pom文件,能夠免去自己管理版本 * PS: 在Spring官網指導上面有另外一種配置,那種配置需要配置main class,一會說明 */ dependencyManagement { imports { mavenBom "org.springframework.boot:spring-boot-starter-parent:${bootVersion}" mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.SR2" } } //私服發布配置 publishing { publications { maven(MavenPublication) { //指定group/artifact/version信息,可以不填。默認使用項目group/name/version作為groupId/artifactId/version groupId = project.group artifactId = project.name version = project.version //如果是war包填寫components.web,如果是jar包填寫components.java from components.java //配置上傳源碼 artifact sourceJar { classifier "src" } } } repositories { maven { def releasesUrl = "http://你的私服ip:8081/repository/maven-releases/" def snapshotsUrl = "http://你的私服ip:8081/repository/maven-snapshots/" url = version.endsWith('SNAPSHOT') ? snapshotsUrl : releasesUrl credentials { username = 'admin' password = 'admin123' } } } } } //這里的配置對子項目生效 subprojects { dependencies { testCompile("org.springframework.boot:spring-boot-starter-test") compile("com.google.guava:guava:28.0-jre") } } //打包源碼 task sourceJar(type: Jar) { from sourceSets.main.allJava }
maven publish使用
在task -> publishing 中有如下幾個命令
我認為使用這兩個命令就足夠了
publishMavenPublicationToMavenLocal 發布項目到本地倉庫
publishMavenPublicationToMavenRepository 發布項目到私服
PS:使用apply plugin: 'org.springframework.boot' build必須要指定main class
23:26:17: Executing task 'build'... > Task :framework-base:compileJava NO-SOURCE > Task :framework-base:processResources NO-SOURCE > Task :framework-base:classes UP-TO-DATE > Task :framework-base:jar SKIPPED > Task :framework-redis:compileJava UP-TO-DATE > Task :framework-redis:processResources NO-SOURCE > Task :framework-redis:classes UP-TO-DATE > Task :framework-redis:jar SKIPPED > Task :framework-web:compileJava UP-TO-DATE > Task :framework-web:processResources NO-SOURCE > Task :framework-web:classes UP-TO-DATE > Task :framework-web:bootJar FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':framework-web:bootJar'. > Main class name has not been configured and it could not be resolved * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/4.10.3/userguide/command_line_interface.html#sec:command_line_warnings BUILD FAILED in 1s 3 actionable tasks: 1 executed, 2 up-to-date Main class name has not been configured and it could not be resolved 23:26:18: Task execution finished 'build'.
framework-web 項目 3. build.gradle
dependencies { //依賴framework-redis項目 compile project(':framework-redis') //不需要寫版本 compile('org.springframework.boot:spring-boot-starter-web') //不需要寫版本 compile('org.springframework.cloud:spring-cloud-starter-openfeign') }
framework-redis 項目 4. build.gradle
dependencies { //依賴framework-base compile project(':framework-base') compile('org.springframework.boot:spring-boot-starter-data-redis') }
framework-base 5. build.gradle
//做為演示沒有引入任何jar包 dependencies { }
關于Gradle中怎么構建一個SpringBoot多模塊項目就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。