【问题标题】:new gradle format confusion and migrating causes Could not find method testCompile()新的 gradle 格式混淆和迁移原因找不到方法 testCompile()
【发布时间】:2020-12-06 02:23:33
【问题描述】:

我有一个 build.gradle

plugins {
   id {some plugin for all projects}
   id "com.diffplug.spotless" version "5.1.1"
}

然后我有一个 allprojects {} 部分,它定义了一个 apply plugin: 'jacoco' 和一个 subprojects {} 部分,它声明了 apply plugin: '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 和一尘不染现在一起玩得很好

编辑(忘记附上完整的不工作的修剪文件):

plugins {
   id "java"
   id "com.diffplug.spotless" version "5.1.1"
}

ext {
    //dependency versions every project usees so we version in one location all jars(less jar hell this way)
    deps = [
       'junit':           'junit:junit:4.11'
    ]
}

allprojects {
   repositories {
       jcenter()
       mavenCentral()
       maven {
           //webpieces VERSIONED snapshots so you can lock on a snapshot
          url "https://dl.bintray.com/deanhiller/maven"
       }

       //For testing locally
       maven {
         url uri('/tmp/myRepo/')
       }
   }
}

subprojects {
   dependencies {
       testCompile deps['junit']
   }
}

谢谢, 院长

【问题讨论】:

    标签: gradle build.gradle gradle-plugin spotless


    【解决方案1】:

    您只是将插件应用到根项目 - 而不是子项目。但是,如果您想通过subprojects 配置来配置插件,则必须使用apply plugin 语法。但是,如果您将两者结合使用,则不必使用旧的 buildscript 块来配置类路径和存储库。

    这是一个例子。我假设根项目不是 Java 项目。我还移除了你的 cmets 并插入了我的,唯一的原因是让它们更容易被发现。

    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
       }
    }
    

    【讨论】:

    • ohhhhhh @BjornVester 我有 id 'java' apply false 所以我认为这种方式甚至行不通。我没有意识到插件可能会如此不一致,因为你不能用 java 做到这一点。我从来没有在插件部分尝试过没有 id 'java' 。我以为一切都在朝着新的插件 {...} 部分发展,所以我真的很困惑,但是哦......今晚会尝试一下。
    • 另外,顺便说一句,我真的不想要根项目中的任何插件,但我认为这个新的插件部分是未来加载速度应该采用的方式。我很困惑现在正确的方向与旧的应用插件方式。
    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2020-04-13
    相关资源
    最近更新 更多