acthota
acthota

Reputation: 567

Is there a way to automatically generate the gradle dependencies declaration in build.gradle?

This is in the context of converting an existing java project into a gradle project.

Is there a tool or webservice that would help generate the dependencies declaration in the build.gradle by pointing to a directory that contain all the dependent jars ?

Upvotes: 4

Views: 2087

Answers (2)

acthota
acthota

Reputation: 567

In my comments to @Opal 's answer, I said that I was working on a quick and dirty groovy script to achieve this. I forgot to attach the script after that. Apologies for the same.

Here's my quick and dirty script. Does solve the purpose partially. Hoping that someone can improve on it.


#! /usr/bin/env groovy

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2' )

import static groovyx.net.http.ContentType.JSON

import groovyx.net.http.RESTClient
import groovy.json.JsonSlurper
import groovy.util.slurpersupport.GPathResult
import static groovyx.net.http.ContentType.URLENC


//def artifactid = "activation"
//def version = "1.1"
//def packaging = "jar"
//
//def mavenCentralRepository = new RESTClient( "http://search.maven.org/solrsearch/select?q=a:%22${artifactid}%22%20AND%20v:%22${version}%22%20AND%20p:%22${packaging}%22&rows=20&wt=json".toString() )
////basecamp.auth.basic userName, password
//
//def response = mavenCentralRepository.get([:])
//println response.data.response.docs
//
//def slurper = new JsonSlurper()
//def parsedJson = slurper.parseText(response.data.toString());
//
//println parsedJson.response.docs.id

def inputFile = new File("input.txt");
def fileList = []
fileList = inputFile.readLines().collect {it.toString().substring(it.toString().lastIndexOf('/') + 1)}
def artifactIDvsVersionMap = [:]

fileList.collectEntries(artifactIDvsVersionMap) {
	def versionIndex = it.substring(0,it.indexOf('.')).toString().lastIndexOf('-')
	[it.substring(0,versionIndex),it.substring(versionIndex+1).minus(".jar")]
}

println artifactIDvsVersionMap

new File("output.txt").delete();

def output = new File("output.txt")
def fileWriter = new FileWriter(output, true)
def parsedGradleParameters = null
try {
	parsedGradleParameters = artifactIDvsVersionMap.collect {
		def artifactid = it.key
		def version = it.value
		def packaging = "jar"

		def mavenCentralRepository = new RESTClient( "http://search.maven.org/solrsearch/select?q=a:%22${artifactid}%22%20AND%20v:%22${version}%22%20AND%20p:%22${packaging}%22&rows=20&wt=json".toString() )

		def response = mavenCentralRepository.get([:])
		println response.data.response.docs.id

		def slurper = new JsonSlurper()
		def parsedJson = slurper.parseText(response.data.toString());
		def dependency = parsedJson.response.docs.id
		fileWriter.write("compile '${dependency}'")
		fileWriter.write('\n')
		sleep (new Random().nextInt(20));
		return parsedJson.response.docs.id
	}
} finally {
	fileWriter.close()
}

println parsedGradleParameters

Groovy pros - Pardon if the code is not not clean. :)

Upvotes: 0

Opal
Opal

Reputation: 84844

In general there no such tool but if all the jars all structured (I mean paths: com/google/guava/guava and so on) it should be easy to write such script on your own.

Mind that it can also be done by importing the whole folder in the following way:

repositories {
    flatDir {
        dirs 'lib'
    }
}

or

dependencies {
    runtime fileTree(dir: 'libs', include: '*.jar')
}

Upvotes: 2

Related Questions