【问题标题】:How to build not executable jar (library) with Spring Boot Gradle Plugin 2.1.*如何使用 Spring Boot Gradle Plugin 2.1.* 构建不可执行的 jar(库)
【发布时间】:2020-01-06 08:06:32
【问题描述】:

我正在尝试建立一个库。我的 build.gradle 文件中有 Spring Boot Gradle 插件:

plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
}

我的库没有主类。为了防止“未配置主类...”错误,我添加到 build.gradle:

bootRepackage {
enabled = false
}

但出现错误:

评估根项目“mysuperlibrary”时出现问题。

在 root 上找不到参数 [build_3886uiniuyuorta9lpa9n4f5c$_run_closure3@1975ec48] 的方法 bootRepackage() org.gradle.api.Project 类型的项目“mysuperlibrary”。

Spring boot Gradle Plugin 1.5.16 一切正常,但不适用于 2.1.3。

更新:我使用这个插件是因为没有它我无法解析 testCompile 'org.springframework.boot:spring-boot-starter-test' 。当我删除插件时,我可以为测试构建项目位依赖项消失。

【问题讨论】:

  • 您可以在 build.gradle 中使用这一行:apply plugin: 'java-library'。 Spring Boot Plugin 自动不会为输出 jar 创建启动类。此外,插件仍将控制该子项目的依赖关系。

标签: java spring-boot gradle build.gradle spring-boot-gradle-plugin


【解决方案1】:

现在我在我的所有库中使用带有 2.1.* 版本的 spring 插件。需要在'dependencies'部分之前添加next:

bootJar {
    enabled = false
}

jar {
    enabled = true
}

【讨论】:

    【解决方案2】:

    更新:

    只需删除插件并直接添加测试依赖项即可。例如。使用 JUnit 5:

    dependencies {
          testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.1'
          ....
          testImplementation(
                "org.junit.jupiter:junit-jupiter-api:$jUnitVersion",
                "org.junit.jupiter:junit-jupiter-params:$jUnitVersion"
          )
          testRuntimeOnly(
                  "org.junit.jupiter:junit-jupiter-engine:$jUnitVersion"
          )
          test {
              useJUnitPlatform()
          }
    }
    

    旧(适合多模块项目): 您可以使用应用标志:

    plugins {
       id 'org.springframework.boot' version '2.1.3.RELEASE' apply: false
    }
    

    写一个函数

    def isAbstract(project) {
        return ['mysuperlibrary'].contains(project.name)
    }
    
    subprojects { project ->
        if (isAbstract(project) {
            return
        }
    
        apply plugin: 'org.springframework.boot'
    
    }
    

    【讨论】:

    • 我认为第二个代码没有意义,因为我在“mysuperlibrary”中使用了它。是的,我构建了项目,但我可以通过删除插件来做同样的事情。我没有说我使用这个插件,因为没有它我无法解析 testCompile 'org.springframework.boot:spring-boot-starter-test' 。当我删除插件时,对测试的依赖消失了。
    • 对不起,我以为你有一个多模块项目。然后直接添加你需要的依赖即可。就像testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.1' 当整个项目不是springboot 应用程序时,为什么要使用spring-boot-starter-test 依赖项。我更新了我的答案。
    • 我在明确指定测试依赖项时解决了我的问题: testCompile 'org.springframework.boot:spring-boot-starter-test:2.1.7.RELEASE' 。我也删除了插件。你的回答不是我想要的,但非常有用。谢谢。
    【解决方案3】:

    可能是这样的:

    tasks {
        bootJar { mainClassName = "NONE" }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 2019-01-09
      • 2017-08-10
      • 2017-02-19
      • 2017-03-19
      • 1970-01-01
      相关资源
      最近更新 更多