新的gradle格式混乱和迁移原因找不到方法testCompile()

新的gradle格式混乱和迁移原因找不到方法testCompile(),gradle,build.gradle,gradle-plugin,spotless,Gradle,Build.gradle,Gradle Plugin,Spotless,我有一个身材,格雷德尔和 plugins { id {some plugin for all projects} id "com.diffplug.spotless" version "5.1.1" } 然后我有一个allprojects{}部分,它定义了一个apply插件:“jacoco”,还有一个subprojects{}部分,它声明apply插件:“java”和其他几个插件 立即添加一尘不染的乱七八糟的东西和错误,因为它找不到java插

我有一个身材,格雷德尔和

plugins {
   id {some plugin for all projects}
   id "com.diffplug.spotless" version "5.1.1"
}
然后我有一个allprojects{}部分,它定义了一个apply插件:“jacoco”,还有一个subprojects{}部分,它声明apply插件:“java”和其他几个插件

立即添加一尘不染的乱七八糟的东西和错误,因为它找不到java插件,所以我修改了所有插件,使其位于插件部分,如下所示

plugins {
   id "java"
   id "checkstyle"
   id "eclipse"
   id "idea"
   id "jacoco"
   id "com.diffplug.spotless" version "5.1.1"
   id "com.dorongold.task-tree" version "1.5" //This prints out a task tree (pretty print)
}
这将导致此错误

Could not find method testCompile() for arguments [junit:junit:4.11] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
因此,由于某种原因,java插件丢失了。我无法找到正确的组合来将所有内容迁移到这个新的插件部分格式

我该怎么做?我随机尝试在所有项目和子项目中放置一个插件部分,但这导致了这个新错误

Could not find method plugins() for arguments [build_d8c2jgy4ua1m0vkv9kmvgefmc$_run_closure2$_closure5@62337fda] on root project 'providersvc-all' of type org.gradle.api.Project
这个新的插件部分是如何工作的?我似乎无法迁移而不破坏一切。我现在只希望java插件、testCompile和spotless能够很好地配合使用

编辑(忘记附加不起作用的完整修剪文件):

谢谢,
Dean

您只将插件应用于根项目,而不是子项目。但是,如果您希望通过
子项目
配置来配置插件,则必须使用
应用插件
语法。但是,如果同时使用了类路径和存储库,则不必使用旧的
buildscript
块来配置类路径和存储库

这里有一个例子。我假设根项目不是Java项目。我也删除了你的评论并插入了我的评论,唯一的原因是为了让它们更容易被发现

plugins {
   id "com.diffplug.spotless" version "5.1.1" apply false // <-- Set "apply false" here
   // This makes it configure which version to use on the classpath for the entire build, but without applying it.
   // Notice that the Java plugin is not specified here anymore.
   // This is because it is a core plugin so you can't set the version (and I am assuming you don't want it on in the root project).
}

ext {
    deps = [
       'junit':           'junit:junit:4.11'
    ]
}

allprojects {
   repositories {
       jcenter()
       mavenCentral() // <-- You can remove this if you want as it is already present as a proxy in jcenter().
       maven {
          url "https://dl.bintray.com/deanhiller/maven"
       }

       maven {
         url uri('/tmp/myRepo/')
       }
   }
}

subprojects {
   // Here are the two plugins
   apply plugin: "java"
   apply plugin: "com.diffplug.spotless"

   dependencies {
       testImplementation deps['junit'] // <-- testCompile renamed to testImplementation as the former has been deprecated for a long time
   }
}
插件{

id“com.diffplug.spotless”版本“5.1.1”apply false//Ohhhhh@BjornVester我的id为“java”apply false,所以我认为这种方式根本不起作用。我没有意识到插件可能会如此不一致,以至于你无法用java实现这一点。我从未尝试过在插件部分没有id“java”。我认为一切都在向新插件发展{…}所以我真的很困惑,但是哦,好吧……今晚我将尝试一下。另外,我真的不想在根项目中使用任何插件,但是我认为这个新的插件部分是未来加载速度应该采用的方式。我很困惑现在与旧的应用插件方式的正确方向。
plugins {
   id "com.diffplug.spotless" version "5.1.1" apply false // <-- Set "apply false" here
   // This makes it configure which version to use on the classpath for the entire build, but without applying it.
   // Notice that the Java plugin is not specified here anymore.
   // This is because it is a core plugin so you can't set the version (and I am assuming you don't want it on in the root project).
}

ext {
    deps = [
       'junit':           'junit:junit:4.11'
    ]
}

allprojects {
   repositories {
       jcenter()
       mavenCentral() // <-- You can remove this if you want as it is already present as a proxy in jcenter().
       maven {
          url "https://dl.bintray.com/deanhiller/maven"
       }

       maven {
         url uri('/tmp/myRepo/')
       }
   }
}

subprojects {
   // Here are the two plugins
   apply plugin: "java"
   apply plugin: "com.diffplug.spotless"

   dependencies {
       testImplementation deps['junit'] // <-- testCompile renamed to testImplementation as the former has been deprecated for a long time
   }
}