Gradle 文件构建脚本块

Gradle file buildscript block

我正在尝试使用 gradle 构建我的项目。 我有如下 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 {} 块必须出现在脚本中任何插件 {} 块之前

我已经尝试将第一个插件块放入构建脚本中,但没有任何效果。有人可以帮帮我吗?

谢谢

错误信息:

all buildscript {} blocks must appear before any plugins {} blocks in the script

说明您需要做什么:将构建脚本块移到插件块之前。例如

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

plugins {
  ...
}

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

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

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

其次,plugins 块可以看作是一种更新的更简单的编写方式 apply plugin,您通常不必配置构建脚本类路径。您应该尽可能使用它,而不是使用更陈旧和更冗长的方式。如果您对如何以较新的方式编写第三方插件有疑问,请在 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 is no longer maintained, but has an (at this time relative) active fork

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