5

I'm trying to learn Gradle in Eclipse.

Is there any good integration where you can search for a Jar to include in the project?

Right now it's a hit-and-miss operation from my side.

Example:

I want to add the CLI library from Apache Commons. Also the Codec library.

I have added both jcenter() and mavenCentral like this:

repositories {
    jcenter()
    mavenCentral()
}

and I have tried this (and variations) in the dependencies section:

compile 'org.apache.commons:cli:1.2'
compile 'org.apache.commons:codec:1.10'

but all I get is

Could not resolve: org.apache.commons:cli:1.2
Could not resolve: org.apache.commons:codec:1.10

When searching in search.maven.org, if I search on org.apache.commons I get 111 pages of hits... I haven't found the time to step thru them all.

When searching for commons-cli, it finds a version from 2005... plus a library called

org.mod4j.org.apache.commons cli

No idea what the "mod4" means.

Is the conclusion that Apache commons doesnt exist in these repositories?

How do you do in these cases? How do you come up with the correct "compile"-specification?

Can I for example say "get latest version of this jar" ?

Would love to have a way to do:

gradle search apache-commons --only-latest-version

or something similar. Something like the wonderful GEM/BUNDLE commands in Ruby.

Thanks for all help

Peter Andersson
  • 1,947
  • 3
  • 28
  • 44
  • " Could not resolve: " might be due to your connection issue to the repositories. Does this happen for all the jars or just for apache.commons ? – Tito Jan 26 '17 at 17:44
  • just those, others work fine, like this one: compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.8' – Peter Andersson Jan 26 '17 at 17:48
  • Could you please edit the question and add the build.gradle and your gradle version ? – Tito Jan 26 '17 at 17:54
  • @PeterAndersson, did you really wrote `compile 'org.org.apache.commons:cli:1.2'`? There is `org.org`. Is it a typo in question? – Andrii Abramov Jan 26 '17 at 20:09
  • yeah sorry that was a typo when I copied and pasted. Fixed now. – Peter Andersson Jan 26 '17 at 20:33
  • @PeterAndersson if anyone of the below answers solved your problem, could please accept one ? – Tito Feb 08 '17 at 14:30

3 Answers3

6

You can search the Maven Central repository to find specific versions of artifacts, but in some cases you have to know the exact name (artifactId) of the artifact. Unfortunately the apache artifacts are not consistent in their groupId or artifactId naming schemes. For example, Apache Commons CLI is commons-cli:commons-cli.

Here is the latest version of that artifact. There is even a convenient panel that shows the exact line to use based on your dependency tool (maven, Gradle, Ivy, etc.)

enter image description here

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • Can I ask what you typed in the search box to find that one? No matter how I try I can't find it by searching! Typing "commons-cli:commons-cli:1.3.1.jar" just gives me nothing... – Peter Andersson Jan 26 '17 at 20:41
  • @PeterAndersson Search string is `g:"commons-cli" AND a:"commons-cli" AND v:"1.3.1"`. GAV is _Group ID_, _Artifact ID_ and _Version_. – thokuest Jan 27 '17 at 08:55
  • I searched for "commons cli" (without the quotes) and scanned the results until I noticed the group and artifact IDs "commons-cli. Like I said, Apache projects' ID naming is inconsistent and some of them are just plain stoopid (like this one); it helps that I knew that from experience. But like someone else mentioned in another answer, going to the library project's web page is usually a good place to start. – E-Riz Jan 27 '17 at 15:39
0

The below build.gradle works. Have a look at the syntax for dependency declaration. Please read this documentation for syntax.

apply plugin: 'java'

repositories {

    jcenter()
    mavenCentral()
}

dependencies {

    compile group: 'commons-cli', name: 'commons-cli', version: '1.2'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.10'
    testCompile 'junit:junit:4.12'
}

And as for getting the 'latest' version of a library in gradle, I am afraid thats not possible currently like in maven.

Community
  • 1
  • 1
Tito
  • 8,894
  • 12
  • 52
  • 86
0

Is there any good integration where you can search for a Jar to include in the project?

As far as I know Gradle itself has no specific support for this.

How do you do in these cases? How do you come up with the correct "compile"-specification?

Before searching in Maven repositories for the desired dependency, best is to consult the project's homepage first. commons-cli for example has a separate Dependency Information section and lists the required information for all kinds of build tools. For Gradle/Grails and the latest SNAPSHOT version this is

compile 'commons-cli:commons-cli:1.4-SNAPSHOT'

The list of valid versions is compiled in the Changes Reports section.

Can I for example say "get latest version of this jar"?

There is a plugin called gradle-versions-plugin that can list updates of defined dependencies. Of course, this requires that you have already defined your dependencies correctly.

thokuest
  • 5,800
  • 24
  • 36