3

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 ?

acthota
  • 557
  • 5
  • 22
  • No, jars contain no information about their Maven coordinates. – Oliver Charlesworth Jan 10 '15 at 12:22
  • Thanks @Oliver. I wish we had a good naming convention to derive the Maven coordinates from the JAR. Your thoughts? – acthota Jan 12 '15 at 02:54
  • Your jars are *probably* named in the form [name]-[version].jar, so missing any group info. In which case, you may be able to get away with a search, but will probably not be 100% reliable. So you could check MD5s, but this is beginning to get pretty complex! – Oliver Charlesworth Jan 12 '15 at 07:01
  • Agree that it would be pretty complex to reliably develop a tool to get the maven coordinates from JAR name. I wish the entire developer community decides on a convention now that Maven coordinates seem to be the defacto standard for lot of build tools. (Maven, Gradle etc..) :) – acthota Jan 12 '15 at 10:00
  • This question may be of interest to you: http://stackoverflow.com/questions/3063215/finding-the-right-version-of-the-right-jar-in-a-maven-repository – Oliver Charlesworth Jan 12 '15 at 10:05
  • Thanks for the link @OliverCharlesworth. It is very pertinent. – acthota Jan 12 '15 at 10:07

2 Answers2

2

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')
}
Opal
  • 81,889
  • 28
  • 189
  • 210
  • Thanks for the answer @Opal. I did not want to use the flatDir since I did not want to add the jars to the version control. I am writing a quick an need dirty groovy script that acts as a rest client for the Maven Central repository search. Will post it when done. By the way, your profile pic is very lively and happy :-) – acthota Jan 12 '15 at 02:49
  • You're welcome, thank You very much :) I realize that this isn't exactly what You're looking for but nevertheless added it as a possible solution. Indeed, adding jars to VCS isn't good idea. It's quite funny - in java world we don't that but iOS developers still discuss it ;] Let me know if you have any further problems. – Opal Jan 12 '15 at 18:02
  • Please check my answer above @Opal. Added the groovy script. – acthota Apr 12 '15 at 16:22
0

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. :)

acthota
  • 557
  • 5
  • 22