如何让 Gradle Spring 依赖插件工作?

How to get Gradle Spring dependency plugin to work?

因为我是这方面的新手,所以问题是上下文。他们的示例与我的构建不匹配。我已经在本地安装了 jar,所以它肯定可以使用。

我的项目看起来像:

    allprojects  {
        apply plugin: 'maven'
        apply plugin: "io.spring.dependency-management"

        group = 'com.wnp'
        version = '6.5.0-SNAPSHOT'
    }

    subprojects {

        apply plugin: 'java'

        sourceCompatibility = System.getProperty("java.version")
        targetCompatibility = System.getProperty("java.version")

        repositories {
            mavenLocal()
            mavenCentral()
            jcenter()         
        }

        dependencyManagement {
            imports {
                mavenBom 'org.springframework.ws:spring-ws:2.1.4.RELEASE'
                mavenBom 'org.jboss.as:jboss-as-jms-client-bom:7.5.0.Final-redhat-21'
            }

            dependencies {
                dependency "antlr:antlr:2.7.7" 
       }

    }

apply plugin: "io.spring.dependency-management"

他们的项目看起来像:

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "io.spring.gradle:dependency-management-plugin:0.5.2.RELEASE"
  }
}

apply plugin: "io.spring.dependency-management"

我从系统得到的是:

Caused by: org.gradle.api.plugins.UnknownPluginException: Plugin with id 'io.spring.dependency-management' not found.

线索? "buildscript"代表什么?那是任务吗?我试过将 "dependencies" 部分放在几乎所有地方。 "apply plugin:" 行也是如此。我的 dependencyManagement 部分在正确的位置吗?我一直看到的一件事是 "dependencies cannot be applied to closure",就错误消息而言,这非常无用。

您需要做的是为 build.gradle 本身定义一个具有您要查找的插件的依赖项。它可能是例如:

buildscript {
   repositories {
      mavenCentral()
   }

   dependencies {
      classpath 'io.spring.gradle:dependency-management-plugin:0.5.3.RELEASE'
   }
}

allprojects  {
   apply plugin: 'maven'
   apply plugin: "io.spring.dependency-management"

   group = 'com.wnp'
   version = '6.5.0-SNAPSHOT'
}

查看名为 "external dependencies" 的 official user guide 部分。根据它:

If your build script needs to use external libraries, you can add them to the script's classpath in the build script itself. You do this using the buildscript() method, passing in a closure which declares the build script classpath.

在你的例子中,一个插件是一个外部依赖,你必须为所有需要应用这个插件的项目提供buildscript部分。