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

时间:2018-12-29 06:40:05

标签: java gradle intellij-idea

我想在gradle文件org.ajoberstar.grgit.Grgit中导入第三方类version.gradle。但是,这给了我一个错误,它无法解析类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链接,例如:

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

1 个答案:

答案 0 :(得分:1)

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

version.gradle:

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