java groovy.lang.MissingPropertyException:无法为 org.gradle.api.Project 类型的项目“:flamingo”设置未知属性“versionKey”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47800977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:49:05  来源:igfitidea点击:

groovy.lang.MissingPropertyException: Could not set unknown property 'versionKey' for project ':flamingo' of type org.gradle.api.Project

javaswinggradleradiance-flamingo

提问by lolveley

I have this error while trying to compile the flamingo graphic tools for java, using intelliJ.

我在尝试使用 intelliJ 为 Java 编译火烈鸟图形工具时遇到此错误。

here is the build.gradle file for the error's project :

这是错误项目的 build.gradle 文件:

import javax.swing.SwingUtilities

dependencies {
  compile project(":trident")
  compile group: 'org.tmatesoft.svnkit', name: 'svnkit', version:'1.2.3.5521'
  compile group:'org.apache.xmlgraphics', name:'batik-swing', version:'1.7'
  compile (group:'org.apache.xmlgraphics', name:'batik-transcoder', version:'1.7') {
    exclude group:'xml-apis'
    exclude group:'xalan'
    exclude group:'commons-io'
    exclude group:'commons-logging'
    exclude group:'org.apache.avalon.framework'
  }
  testCompile group: 'com.jgoodies', name: 'forms', version: '1.2.0'
  testCompile group: 'junit', name: 'junit', version: '4.3.1'
  testCompile group: 'org.easytesting', name: 'fest-assert', version: '1.2'
  testCompile group: 'org.easytesting', name: 'fest-reflect', version: '1.2'
  testCompile group: 'org.easytesting', name: 'fest-swing', version: '1.2.1'
  testCompile group: 'org.easytesting', name: 'fest-swing-junit', version: '1.2.1'
  testCompile group: 'org.easytesting', name: 'fest-swing-junit-4.3.1', version: '1.2.1'
}

sourceSets {
  main
  test
}

test {
  // if we are headless, don't run our tests
  enabled = !Boolean.getBoolean("java.awt.headless")
}

jar {
  manifest {
    attributes(
        "Flamingo-Version": version,
        "Flamingo-VersionName": versionKey,
    )
  }
}

task testJar(type: Jar) {
  classifier = 'tst'

  from sourceSets.test.classes

  manifest {
    attributes(
        "Flamingo-Version": version,
        "Flamingo-VersionName": versionKey,
    )
  }
}

uploadArchives {
  try {
    def x = [deployUsername, deployPassword]
  } catch (Exception e) {
    deployUsername = 'unset'
    deployPassword = ''
  }
  repositories {
    mavenDeployer {
      snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
        authentication userName: deployUsername, password: deployPassword
      }
      repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
        authentication userName: deployUsername, password: deployPassword
      }
      configurePOM(pom)
    }
  }
}

install {
  configurePOM(repositories.mavenInstaller.pom)
}

private def configurePOM(def pom) {
  configureBasePom(pom)
  pom.project {
    name "flamingo"
    description "A fork of @kirilcool's flamingo project"
    url "http://insubstantial.github.com/peacock"
  }
  // deal with a gradle bug where transitive=false is not passed into the generated POM
  pom.whenConfigured {cpom ->
    cpom.dependencies.each {it
      switch (it.artifactId) {
        case 'trident':
          it.classifier = 'swing'
          break
      }
    }
  }
}

I don't know what to add for the version key, nor where.

我不知道要为版本密钥添加什么,也不知道在哪里添加。

The project is on GitHub : it's a fork of the original project flamingo.

该项目位于 GitHub 上:它是原始项目 flamingo 的一个分支

Here is the build.gradle file inside the root directory:

这是根目录下的 build.gradle 文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'de.huxhorn.gradle:de.huxhorn.gradle.pgp-plugin:0.0.3'
    }
}

subprojects {
  apply plugin: 'java'
  apply plugin: 'maven'
  try {
    def test = pgpSecretKeyRingFile // exception will throw if not set
    apply plugin: 'sign'
    apply plugin: de.huxhorn.gradle.pgp.PgpPlugin
  } catch (Exception ignore) {}

  group = 'com.github.insubstantial'
  version = '6.3-SNAPSHOT'
  versionKey = "6.3-defender"
  release = "internal"

  sourceCompatibility = 1.6
  targetCompatibility = 1.6

  configurations {
    maven { extendsFrom archives }
  }

  repositories {
    mavenRepo urls: 'https://oss.sonatype.org/content/groups/staging'
    mavenCentral()
    mavenRepo urls: new File(System.getProperty('user.home'), '.m2/repository').toURI().toString()
  }

  task sourceJar(type: Jar) {
    from sourceSets.main.java
    from sourceSets.main.resources
    classifier = 'sources'
  }

  task javadocJar(type: Jar) {
    dependsOn javadoc
    from javadoc.destinationDir
    classifier = 'javadoc'
  }

  artifacts {
    maven sourceJar
    maven javadocJar
  }

  uploadArchives {
    try {
      def x = [deployUsername, deployPassword]
    } catch (Exception e) {
      deployUsername = 'unset'
      deployPassword = ''
    }
    configuration = configurations.maven
    repositories {
      mavenDeployer {
        snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
          authentication userName: deployUsername, password: deployPassword
        }
        repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
          authentication userName: deployUsername, password: deployPassword
        }
      }
    }
  }

  install {
    configuration = configurations.maven
  }

  configureBasePom = { pom ->
    pom.project {
      modelVersion '4.0.0'
      packaging 'jar'
      scm {
        connection 'scm:git:[email protected]:Insubstantial/insubstantial.git'
        developerConnection 'scm:git:[email protected]:Insubstantial/insubstantial.git'
        url 'scm:git:[email protected]:Insubstantial/insubstantial.git'
      }
      developers {
        developer {
          name 'Kirill Grouchnikov'
          email '[email protected]'
          roles {
            role 'author'
            role 'developer'
          }
        }
        developer {
          name 'Danno Ferrin'
          email '[email protected]'
          roles {
            role 'maintainer'
          }
        }
      }
    }
  }
}

task wrapper(type: Wrapper) {
  gradleVersion = '1.0-milestone-2'
}

moreover, the main build.gradle contains the word "Exception" which raise an error from intelliJ.

此外,主 build.gradle 包含单词“Exception”,它会从 intelliJ 引发错误。

回答by Vampire

Well, your main problem is that - and both of these are valid statements, you can select for yourself which you find more appealing - the project is designed for a too old Gradle version for usage with a current Gradle integration and / or your IntelliJ version (or rather its Gradle integration) is too new for usage with that project.

嗯,你的主要问题是 - 这两个都是有效的陈述,你可以自己选择你觉得更有吸引力的 - 该项目是为太旧的 Gradle 版本而设计的,无法与当前的 Gradle 集成和/或你的 IntelliJ 版本一起使用(或者更确切地说它的 Gradle 集成)对于该项目来说太新了。

To be more technically precise, the IDE Gradle plugins use the Gradle Tooling API to interact with the Gradle build (run it, get information about source paths, dependencies, tasks, ...). The current version of the Tooling API that is used in the IDE plugins is compatible with builds down to Gradle 1.2 which is really quite ancient already. Your build though is designed for being run with Gradle 1.0-milestone-2 - which is not even a productive release - and defines this in its Gradle wrapper settings.

更准确地说,IDE Gradle 插件使用 Gradle Tooling API 与 Gradle 构建交互(运行它,获取有关源路径、依赖项、任务等的信息)。IDE 插件中使用的 Tooling API 的当前版本与 Gradle 1.2 的构建兼容,而 Gradle 1.2 已经非常古老了。您的构建虽然设计为与 Gradle 1.0-milestone-2 一起运行 - 这甚至不是一个高效的版本 - 并在其 Gradle 包装器设置中定义了这一点。

This means, that if you run Gradle from the commandline, 1.0-milestone-2 is used automatically and the build is working as designed (that's the cool magic of the wrapper). If you try to import the project with the Gradle integration of IntelliJ and tell it to use the projects default wrapper (the default choice and always the best idea and if a project does not use the wrapper, tell them to add it), IntelliJ tells you The project is using an unsupported version of Gradle. Please point to a supported Gradle version in the project's Gradle settings or in the project's Gradle wrapper (if applicable.). I guess you then discarded the message dialog and told IntelliJ to use some local Gradle installation instead and then you get the error you mentioned. (Btw. you should have mentioned that you followed this way, makes helping much easier)

这意味着,如果您从命令行运行 Gradle,将自动使用 1.0-milestone-2 并且构建按设计工作(这是包装器的酷魔法)。如果您尝试使用 IntelliJ 的 Gradle 集成导入项目并告诉它使用项目默认包装器(默认选择并且始终是最好的主意,如果项目不使用包装器,请告诉他们添加它),IntelliJ 告诉你The project is using an unsupported version of Gradle. Please point to a supported Gradle version in the project's Gradle settings or in the project's Gradle wrapper (if applicable.)。我猜你然后丢弃了消息对话框并告诉 IntelliJ 使用一些本地 Gradle 安装来代替,然后你会得到你提到的错误。(顺便说一句。你应该提到你遵循这种方式,使帮助更容易)

When there are major version bumps in Gradle version, they remove stuff they deprecated long enough before according to their deprecation and removal policy and builds might break with these changes which is exactly what you are exhibiting, as you use a Gradle version that this build is not designed for and is not compatible with.

当 Gradle 版本中存在主要版本颠簸时,他们会根据其弃用和删除策略删除他们之前弃用足够长的内容,并且构建可能会因这些更改而中断,这正是您所展示的,因为您使用此构建的 Gradle 版本不适合且不兼容。

So what you can do is two things.

所以你能做的是两件事。

Either you do not use the Gradle integration in IntelliJ with this project but use Gradle only from the commandline. You can add allprojects { apply plugin: 'idea' }to your build.gradleand then use ./gradlew ideato generate properly configured IntelliJ project files that you then can open with IntelliJ and work with the project.

您要么不将 IntelliJ 中的 Gradle 集成用于此项目,而是仅从命令行使用 Gradle。您可以添加allprojects { apply plugin: 'idea' }到您的build.gradle然后用于./gradlew idea生成正确配置的 IntelliJ 项目文件,然后您可以使用 IntelliJ 打开这些文件并使用该项目。

The other option - and I would really recommend it, even if it is more work - is to update the build to be compatible with current Gradle versions and configure the wrapper to use that new version, then the integration works flawlessly and you also benefit from all development that was done in Gradle since that really old version. You can read the release notes for important changes and breaking changes and interesting changes. To just get it done it should also be sufficient to update to the latest 1.x version, fix all deprecated warnings, update to the latest 2.x version, fix all deprecated warnings, update to the latest 3.x version, fix all deprecated warnings and then update to the latest 4.x version. This should at least make the build working with the latest version in a guided way, even though the build might not be the best one, not using some of the new things that were added to Gradle in the meantime. But this could at least be used as a starter.

另一种选择 - 我真的会推荐它,即使它需要更多的工作 - 是更新构建以与当前的 Gradle 版本兼容并配置包装器以使用该新版本,然后集成可以完美运行,您也可以从中受益自从那个真正的旧版本以来,所有的开发都是在 Gradle 中完成的。您可以阅读发行说明以了解重要更改和重大更改以及有趣的更改。为了完成它,它也应该足够更新到最新的 1.x 版本,修复所有过时的警告,更新到最新的 2.x 版本,修复所有过时的警告,更新到最新的 3.x 版本,修复所有弃用警告,然后更新到最新的 4.x 版本。这至少应该使构建以引导方式与最新版本一起工作,即使构建可能不是最好的,不使用在此期间添加到 Gradle 的一些新东西。但这至少可以用作启动器。

You might be tempted to think you can also do a middle-thing and just set the wrapper to use 1.2. As it is in the same major version, the build should work with it and as I said before, 1.2 is currently the oldest version that is working with the integration. At least partly. Things like cancelling a build will e. g. not work as 1.2 does not yet support this, even if the tooling API does. But this won't work, because the build as it is does not even with 1.0. As 1.0-milestone-2 was only a pre-release, there the stability guarantees of course did not hold yet and there was a change between that and 1.0 that breaks your build.

您可能会认为您也可以做一个中间的事情,只需将包装器设置为使用 1.2。由于它在同一个主要版本中,因此构建应该可以使用它,正如我之前所说,1.2 目前是使用集成的最旧版本。至少部分。诸如取消构建之类的事情将不起作用,因为 1.2 尚不支持此功能,即使工具 API 支持。但这行不通,因为即使使用 1.0.0.1 也无法进行构建。由于 1.0-milestone-2 只是一个预发布版本,因此稳定性保证当然还没有成立,并且 1.0 和 1.0 之间的变化会破坏您的构建。

Regarding the actual two errors you got, the first, the one with the unknown property is exactly one of the breaking changes I mentioned. Previously you could simply set any new property by doing foo = 'value'. The problem is, that people often mistyped properties, e. g. wrote fop = 'value'instead and then wondered why it doesn't work, not getting any helpful error message. So dynamically defined properties were forbidden and you have to do it in the extnamespace like ext { foo = 'value' }or ext.foo = 'value', but only on the first occurrence. This defines the new custom property and later on you can get and set it only by its name. If it shouldn't have been a property of the object in question (the project in your case) in the first place, but just a local variable in the build script, it should simply be defined as local variable like def foo = 'value'like always in Groovy which Gradle is based on.

关于您实际遇到的两个错误,第一个具有未知属性的错误正是我提到的重大更改之一。以前,您可以通过执行 来简单地设置任何新属性foo = 'value'。问题是,人们经常输入错误的属性,例如fop = 'value'改为写入然后想知道为什么它不起作用,没有得到任何有用的错误消息。因此,动态定义的属性被禁止,你必须做的ext像命名空间ext { foo = 'value' }ext.foo = 'value',但仅限于第一次出现。这定义了新的自定义属性,稍后您只能通过其名称获取和设置它。如果它首先不应该是所讨论对象(您的项目中的项目)的属性,而只是构建脚本中的局部变量,则它应该简单地定义为局部变量,就像def foo = 'value'在 Groovy 中一样Gradle 是基于.

Regarding the second error, the Exceptions IntelliJ is complaining about. Mine does not complain at all, I don't know what inspections you maybe have enabled or whatever, but if it is ok for Gradle it should be ok for IntelliJ if it is not, you should report it as bug to JetBrains, but as I said, here it is not red. But using exceptions for flow control like in that build script is very bad practice anyway, not only in build scripts, but in Java or even in programming at all.

关于第二个错误,Exceptions IntelliJ 正在抱怨。我的根本没有抱怨,我不知道您可能启用了哪些检查或其他什么,但是如果 Gradle 没问题,那么 IntelliJ 应该没问题,如果不是,您应该将其作为错误报告给 JetBrains,但是作为我说,这里不是红色的。但是在构建脚本中使用异常进行流控制无论如何都是非常糟糕的做法,不仅在构建脚本中,而且在 Java 中,甚至在编程中也是如此。

try {
  def test = pgpSecretKeyRingFile // exception will throw if not set
  apply plugin: 'sign'
  apply plugin: de.huxhorn.gradle.pgp.PgpPlugin
} catch (Exception ignore) {}

could e. g. be written like

例如可以写成

if (project.hasProperty('pgpSecretKeyRingFile')) {
  apply plugin: 'sign'
  apply plugin: de.huxhorn.gradle.pgp.PgpPlugin
}

and

try {
  def x = [deployUsername, deployPassword]
} catch (Exception e) {
  deployUsername = 'unset'
  deployPassword = ''
}

could e. g. be written like

例如可以写成

if (!(project.hasProperty('deployUsername') && project.hasProperty('deployPassword'))) {
  deployUsername = 'unset'
  deployPassword = ''
}

without changing the meaning of the code

不改变代码的含义