当前位置:   article > 正文

Gradle构建SpringBoot多模块项目

> main class name has not been configured and it could not be resolved

Gradle官方介绍是Build Anything, Automate Everything,Deliver Faster,即构建任何项目,自动化,构建快。

​Gradle VS Maven

这是官方跟Maven构建的比较视频

640?wx_fmt=gif

可以明显看到Gradle速度快,显示内容也比较清爽

项目实例

IDEA创建初始项目

so easy..

640?wx_fmt=pngcopy 两个项目,目录结构是这样,这里用1-5标识Gradle配置文件

640?wx_fmt=png

配置文件详解

1. settings.gradle

  1. /**
  2. * rootProject.name 项目名称
  3. * include 模块名称
  4. */
  5. rootProject.name = 'micro-service-framework'
  6. include 'framework-base'
  7. include 'framework-web'
  8. include 'framework-redis'

2. build.gradle
这个相当于maven中的父maven配置

  1. buildscript {
  2. ext {
  3. //spring boot 版本
  4. bootVersion = '2.0.6.RELEASE'
  5. }
  6. //私服地址,这个地址适用于gradle自身,比如删除,下面的springboot插件就会找不到
  7. repositories {
  8. maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
  9. }
  10. //springboot gradle插件配置
  11. dependencies {
  12. classpath("org.springframework.boot:spring-boot-gradle-plugin:${bootVersion}")
  13. }
  14. }
  15. allprojects {
  16. //导入使用的插件
  17. apply plugin: 'java'
  18. apply plugin: 'maven'
  19. //如果导入该插件,你需要指定main class,否则不能打包
  20. //apply plugin: 'org.springframework.boot'
  21. apply plugin: 'io.spring.dependency-management'
  22. //这个插件用于发布jar包到私服
  23. apply plugin: 'maven-publish'
  24. //jdk编译版本
  25. sourceCompatibility = 1.8
  26. //jar包的group ,version配置
  27. group 'net.178le.micro'
  28. version '0.0.1-SNAPSHOT'
  29. //私服地址,
  30. repositories {
  31. maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
  32. }
  33. /**
  34. * 导入了springboot,spring cloud的pom文件,能够免去自己管理版本
  35. * PS: 在Spring官网指导上面有另外一种配置,那种配置需要配置main class,一会说明
  36. */
  37. dependencyManagement {
  38. imports {
  39. mavenBom "org.springframework.boot:spring-boot-starter-parent:${bootVersion}"
  40. mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.SR2"
  41. }
  42. }
  43. //私服发布配置
  44. publishing {
  45. publications {
  46. maven(MavenPublication) {
  47. //指定group/artifact/version信息,可以不填。默认使用项目group/name/version作为groupId/artifactId/version
  48. groupId = project.group
  49. artifactId = project.name
  50. version = project.version
  51. //如果是war包填写components.web,如果是jar包填写components.java
  52. from components.java
  53. //配置上传源码
  54. artifact sourceJar {
  55. classifier "src"
  56. }
  57. }
  58. }
  59. repositories {
  60. maven {
  61. def releasesUrl = "http://你的私服ip:8081/repository/maven-releases/"
  62. def snapshotsUrl = "http://你的私服ip:8081/repository/maven-snapshots/"
  63. url = version.endsWith('SNAPSHOT') ? snapshotsUrl : releasesUrl
  64. credentials {
  65. username = 'admin'
  66. password = 'admin123'
  67. }
  68. }
  69. }
  70. }
  71. }
  72. //这里的配置对子项目生效
  73. subprojects {
  74. dependencies {
  75. testCompile("org.springframework.boot:spring-boot-starter-test")
  76. compile("com.google.guava:guava:28.0-jre")
  77. }
  78. }
  79. //打包源码
  80. task sourceJar(type: Jar) {
  81. from sourceSets.main.allJava
  82. }

maven publish使用
在task -> publishing 中有如下几个命令

640?wx_fmt=jpeg我认为使用这两个命令就足够了
publishMavenPublicationToMavenLocal 发布项目到本地仓库
publishMavenPublicationToMavenRepository 发布项目到私服

PS:使用apply plugin: 'org.springframework.boot' build必须要指定main class

  1. 23:26:17: Executing task 'build'...
  2. > Task :framework-base:compileJava NO-SOURCE
  3. > Task :framework-base:processResources NO-SOURCE
  4. > Task :framework-base:classes UP-TO-DATE
  5. > Task :framework-base:jar SKIPPED
  6. > Task :framework-redis:compileJava UP-TO-DATE
  7. > Task :framework-redis:processResources NO-SOURCE
  8. > Task :framework-redis:classes UP-TO-DATE
  9. > Task :framework-redis:jar SKIPPED
  10. > Task :framework-web:compileJava UP-TO-DATE
  11. > Task :framework-web:processResources NO-SOURCE
  12. > Task :framework-web:classes UP-TO-DATE
  13. > Task :framework-web:bootJar FAILED
  14. FAILURE: Build failed with an exception.
  15. * What went wrong:
  16. Execution failed for task ':framework-web:bootJar'.
  17. > Main class name has not been configured and it could not be resolved
  18. * Try:
  19. 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.
  20. * Get more help at https://help.gradle.org
  21. Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
  22. Use '--warning-mode all' to show the individual deprecation warnings.
  23. See https://docs.gradle.org/4.10.3/userguide/command_line_interface.html#sec:command_line_warnings
  24. BUILD FAILED in 1s
  25. 3 actionable tasks: 1 executed, 2 up-to-date
  26. Main class name has not been configured and it could not be resolved
  27. 23:26:18: Task execution finished 'build'.

framework-web 项目 3. build.gradle

  1. dependencies {
  2. //依赖framework-redis项目
  3. compile project(':framework-redis')
  4. //不需要写版本
  5. compile('org.springframework.boot:spring-boot-starter-web')
  6. //不需要写版本
  7. compile('org.springframework.cloud:spring-cloud-starter-openfeign')
  8. }

framework-redis 项目 4. build.gradle

  1. dependencies {
  2. //依赖framework-base
  3. compile project(':framework-base')
  4. compile('org.springframework.boot:spring-boot-starter-data-redis')
  5. }

framework-base 5. build.gradle

  1. //做为演示没有引入任何jar包
  2. dependencies {
  3. }

640?wx_fmt=png

转载于:https://my.oschina.net/itsaysay/blog/3095846

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/305693
推荐阅读
相关标签
  

闽ICP备14008679号