繁体   English   中英

无法在 build.gradle 以外的 gradle 文件中导入第三方类

[英]Could not import third-party class in gradle file other than build.gradle

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

构建.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 链接,例如:

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

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

版本.gradle:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM