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

Ankit Shubham :

I want to import a third-party class org.ajoberstar.grgit.Grgit in a gradle file version.gradle. However it is giving me the error that it is unable to resolve class org.ajoberstar.grgit.Grgit(I am using apply from: "version.gradle" in build.gradle). But if I import it in build.gradle, it works fine. Here is what I am doing:

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")
    }

Here is the version.gradle file:

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)
    }
}

I tried a few SO links like:

But I could not resolve this. Any help?

M.Ricciuti :

You need to make the org.ajoberstar.grgit package classes available in the Gradle script where you use it, using buildscript block:

version.gradle:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=97428&siteId=1