【问题标题】:Could not import third-party class in gradle file other than build.gradle无法在 build.gradle 以外的 gradle 文件中导入第三方类
【发布时间】:2018-12-29 06:40:04
【问题描述】:

我想在 gradle 文件 version.gradle 中导入第三方类 org.ajoberstar.grgit.Grgit。但是它给了我一个错误,它无法解析类 org.ajoberstar.grgit.Grgit(我在 build.gradle 中使用apply from: "version.gradle")。但是如果我将它导入build.gradle,它就可以正常工作。这是我正在做的事情:

build.gradle:

plugins {
  id "org.ajoberstar.grgit" version "1.5.0"
}
apply from: "version.gradle"

// Version of jar file.
version = 1.0

jar
 {
  destinationDir = file(JAR_DIR)
  from {
    (configurations.runtime).collect {
      it.isDirectory() ? it : zipTree(it)
    }
  }
  manifest {
    attributes 'Main-Class': 'com.awesomeness.Main'
  }
}

jar.dependsOn versionTxt

// Artifact dependecies.
    dependencies {
      compile files("$TOOLS/lib/grgit-1.5.0.jar")
      compile files("$TOOLS/lib/groovy-all-2.4.7.jar")
      compile files("$TOOLS/lib/org.eclipse.jgit-4.2.0.201601211800-r.jar")
    }

这是 version.gradle 文件:

import org.ajoberstar.grgit.Grgit
//
import java.text.DateFormat

task versionTxt()  {
    doLast {
        Grgit grgit = Grgit.open()
        File file = new File("$buildDir/version.txt")
        file.write("")
        file.append("Version: $version")
        file.append("Branch: "+grgit.branch.current.trackingBranch.name)
        file.append("Build-Type: ")
        file.append("Build-Date: " + DateFormat.getDateInstance(DateFormat.SHORT).format(new Date((long)grgit.head().time*1000l)))
        file.append("Commit-Id: "+ grgit.head().abbreviatedId)
    }
}

我尝试了一些 SO 链接,例如:

但我无法解决这个问题。有什么帮助吗?

【问题讨论】:

    标签: java gradle intellij-idea


    【解决方案1】:

    您需要使用 buildscript 块在使用它的 Gradle 脚本中使 org.ajoberstar.grgit 包类可用:

    version.gradle:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath("org.ajoberstar:grgit:1.5.0")
        }
    }
    import org.ajoberstar.grgit.Grgit
    // .. rest of your build script
    

    【讨论】:

      猜你喜欢
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多