我想在 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链接,例如:
- How can I import one Gradle script into another?
- Gradle dependencies not working (Cannot import)
- How do I import a class in gradle outside of build.gradle in a apply from: file
但我无法解决这个问题。有什么帮助吗?
最佳答案
您需要使用 buildscript
block 使 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
关于java - 无法在 gradle 文件中导入除 build.gradle 之外的第三方类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53967319/