【问题标题】:Gradle file buildscript blockGradle 文件构建脚本块
【发布时间】:2021-01-11 17:43:26
【问题描述】:

我正在尝试使用 gradle 构建我的项目。 我有 de build.gradle 文件如下:

plugins {
    id "fr.putnami.gwt" version "0.4.0"
    id "info.solidsoft.pitest" version "1.3.0"
    }

buildscript {

  repositories {
        mavenCentral() 
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
  }
  dependencies {
  
    testCompile 'junit:junit:4.12'
    testCompile 'org.easymock:easymock:2.5.2'
    testCompile 'com.google.gwt:gwt-dev:2.8.1'
    compile 'net.sourceforge.plantuml:plantuml:8001'
    classpath 'gradle.plugin.de.fntsoftware.gradle:markdown-to-pdf:1.1.0'
  }
  

}
    
    apply plugin: 'war'
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'jacoco'
    apply plugin: 'de.fntsoftware.gradle.markdown-to-pdf'

当我尝试执行构建任务时,我总是得到:

所有 buildscript {} 块必须出现在脚本中的任何 plugins {} 块之前

我已经尝试将第一块插件放在 buildscript 中,但没有任何效果。有人可以帮帮我吗?

谢谢

【问题讨论】:

    标签: gradle build.gradle


    【解决方案1】:

    错误信息:

    所有 buildscript {} 块必须出现在脚本中的任何 plugins {} 块之前

    说明您需要做什么:将 buildscript 块移到 plugins 块之前。例如

    buildscript {
      repositories { ... }
      dependencies { ... }
      ...
    }
    
    plugins {
      ...
    }
    

    不要把一个放在另一个里面。

    但是,不管顺序如何,我认为您一开始就没有正确使用这两个块。

    首先,buildscript 块用于配置实际脚本中使用的依赖项,而不是用于构建项目的依赖项。所以像 junit 或 easymock 这样的东西在这里没有意义。您很可能希望将依赖项块放在外面,因为它们是用于项目的。

    其次,plugins 块可以看作是编写apply plugin 的一种更新的更简单的方法,您通常不必配置 buildscript 类路径。您应该尽可能地使用它,而不是使用旧的和更冗长的方式。如果您对如何以较新的方式编写第三方插件有疑问,请在Gradle Plugins Portal 中找到它并阅读那里的说明。

    改用这样的方法:

    plugins {
      id 'java'
      id 'war'
      id 'jacoco'
      id 'eclipse'
      id "fr.putnami.gwt" version "0.4.0"
      id "info.solidsoft.pitest" version "1.3.0"
      id "de.fntsoftware.gradle.markdown-to-pdf" version "1.1.0"
    }
    
    repositories {
      mavenCentral() 
    }
     
    dependencies {
      testCompile 'junit:junit:4.12'
      testCompile 'org.easymock:easymock:2.5.2'
      testCompile 'com.google.gwt:gwt-dev:2.8.1'
      compile 'net.sourceforge.plantuml:plantuml:8001'
    }
    

    另外请注意,Putnami GWT plugin 不再维护,但有一个(此时是相对的)active fork

    此外,compile 已被弃用,如果您的 Gradle 版本和插件支持它,您应该使用 implementation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      相关资源
      最近更新 更多