Where can I find gradle dependencies? - eclipse

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

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

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.

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.

Related

Understanding eclipse maven dependency hierarchy

I want to understand the dependencies for a multi-module maven project and for that referred to eclipse dependency hierarchy.
I did understand fairly, however some of the things I am not able to understand at all.
Below is the screen shot.
The things which I didn't understand are:
--> managed from 1.0.2 [Compile}
--> managed from 1.0.2 (omitted for conflict with 1.0.0) [Compile]
I did search online but I got information in traces. Can anyone help me understand what they mean in easy to understand?
Thanks.
Maven builds a flat classpath from the dependency tree each for compiling ([compile]), for testing, and for running.
In a flat classpath, unlike OSGi, a dependency can only exist in one version. In your cropped screenshot, there is on the second level among other things:
kafka-streams 1.0.2 and
kafka-clients 1.0.0.
kafka-streams 1.0.2 requires kafka-clients 1.0.2 which conflicts to kafka-clients 1.0.0. Therefore kafka-streams 1.0.2 is omitted for conflicts with 1.0.0 even if the version 1.0.2 is required here ("managed from 1.0.2").
More detailed:The classpath which is used to compile or run a plain Java application is flat: all required libraries are globally specified as an ordered list. It is not possible to use a library of a specific version for one package and for another package the same library in a different version.In Maven dependencies builds a tree: each dependency might have its own dependencies. Maven maps the tree of dependencies to the classpath, an ordered list of libraries. If in the Maven dependencies tree the same library exists in different versions, it is not possible to create a flat classpath. This is a conflict.This conflict is resolved by picking one version and omitting all other versions. At the place where the picked version is used instead of the required version, (managed from <required but not picked version>) and (omitted for conflict with <picked version to use instead>) is displayed.In addition, Maven can create different classpaths to compile, to test or to run a Java application via so-called scopes. The [compile] scope is the default scope for using a library in all tasks: compiling, testing and running.
Make sure that the versions specified in the pom.xml file are compatible with each other (which is not yet the case in your screenshot): you have to upgrade kafka-clients from 1.0.0 to 1.0.2 (or downgrade the other libraries).

Gradle tasks are disabled in eclipse

I'm trying to create a .jar file for my eclipse project. Through export option, I am able to create .jar, but I want to use Gradle build task to do so. Problem is that, all tasks are disabled for this particular project only, as showing in the following image...
For another project, these Gradle tasks are working fine. See its build.gradle file...
apply plugin : 'eclipse'
apply plugin: 'java-library'
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile group:"org.apache.pdfbox", name:"pdfbox", version:"2.0.6"
compile group:"org.apache.pdfbox", name:"pdfbox-tools", version:"2.0.6"
compile 'com.google.code.gson:gson:2.8.2'
compile 'org.json:json:20180130'
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:21.0'
testImplementation 'junit:junit:4.12'
}
It's settings.gradle file contains a single line that is...
rootProject.name = 'schedule-extractor'
Can anybody suggest what I am missing here?
Have you check this buildship's issue:
https://github.com/eclipse/buildship/issues/511
The following comment resolved it for me (I was having the same problem) :
https://github.com/eclipse/buildship/issues/511#issuecomment-367392031
For those who had a similar problem in include builds - Check the file
.setting/org.eclipse.buildship.core.prefs whether the
connection.project.dir property is empty. If not, make it empty. In
my case the grayed tasks become green again.
In my case, It happened when I was using composite build concept of Gradle. According to this approach, One build could be dependent on another build. In this case, the projects may behave like this.
For example, have a look on my composite build setup in settings.gradle...
rootProject.name = 'BackendRESTServices'
includeBuild ('../Algorithms')
includeBuild ('../Utilities')
Now I won't be able to build Algorithms & Utilities projects from Gradle Tasks tab as shown in the attached image of question.

importing dependencies into eclipse using gradle

I've got what appears to be an adequate gradle file but eclipse refuses to recognise the dependancy I'm trying to import (despite refreshing the gradle project after implementing the dependancy)
My gradle file lacks a buildscript{} block but I'm not familar enough with gradle to implement it. My build file was autogenerated by eclipse so I don't see why it'd be an issue.
I'm trying to import "com.intrinio:sdk:0.0.1"
build.gradle:
// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
compile "com.intrinio:sdk:0.0.1"
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:21.0'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
I think this is the lib you want to use.
As it's not deployed in a public repository, you have to manually install it. Checkout the project from github and do as below :
Installation
To install the Intrinio Java SDK to your local Maven repository, simply execute:
mvn install
Then, in your gradle file, add mavenLocal() in the repository section and it should just work fine.

Where do I apply the gradle idea plugin?

The documentation says:
apply plugin: 'idea'
This is easy but not very useful. It does not tell me where to apply the plug-in, for example. allProjects, perhaps? subprojects? Are there any places where it should not be applied?
Typically all plugins are applied just after buildscript section at the very top of build.gradle script, especially in single module projects.
In multimodule projects you can apply the plugin in both allprojects and subprojects. It just depends if this plugin will be required in all the projects. If not and it's applied - nothing bad happens.

How to find the parent of a Maven transitive dependency

I am building a Java (web) application with Maven and Eclipse.
When I look inside my .war file I can see the following logging libraries there:
log4j-1.2.14.jar
log4j-1.2.17.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
I did not declared these libraries in my pom.xml, so they probably are transitive dependencies (i.e. dependencies of my dependencies).
How can I find out which of my dependencies depend on these libraries?
I tried to use the mvn dependency:tree plugin, but it does not show any of these .jars.
In Eclipse, the Java Resources > Libraries > Maven Dependencies node does not show them either. Though, curiously, it shows other transitive dependencies of my project.
If you want to rely on maven only you may want to take a closer look on the dependency plugin, here are two examples:
mvn dependency:tree -Dverbose will display more detailed information - especially for example if a artifact will be omitted for conflicting with another artifacts version (e.g. convergence issue). It will also display you the hirachy with all the transitive dependencies.
To have a specific artefact analyzed (to for example find who delivers a specific transitive dependency) you can specify like so:
mvn dependency:tree -Dincludes=com.my.group.id:my-artefact-id:jar:1.0.1 -Dverbose
(Where you obviously need to adjust the artefact, packaging type and version according to your needs)
Open pom.xml in Eclipse and go to Dependency Hierarchy tab
![enter image description here][1]
It should show you the dependency tree in the Dependency Hierarchy tab. Evgeniy Dorofeev is right.
Something similar to the screenshot attached
you would probably have to expand all.
EDIT: Refined the answer.
Thats a little weird. But here is what i found.
If you go to http://mvnrepository.com/artifact/log4j/log4j/1.2.17
it will show you which dependencies are used and which does the jar file depend on.
The springframework dependency for web-mvc 3.2.4 would download the spring-core and the spring-core dependency uses log4j.