Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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:

  • 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

But I could not resolve this. Any help?

like image 771
Ankit Shubham Avatar asked Dec 29 '18 06:12

Ankit Shubham


1 Answers

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
like image 81
M.Ricciuti Avatar answered Nov 15 '22 05:11

M.Ricciuti