Gradle dependency management using pom.xml - maven

In build.gradle we specify the dependencies as
compile group: 'org.apache.pig', name: 'pigunit', version: '0.11.0-cdh4.5.0', transitive: true
Running gradle cleanEclipse eclipse sets up the projects(adds the jar to classpath)
However there are only maven dependencies available for some APIs
(I am trying to run jersey 2.x examples bundle from https://jersey.java.net/download.html and it provides only pom.xml)
EDIT:
I know i can specify
compile group: 'groupId', name: 'artifactId', version: 'version' gradle but doing it manually for all dependencies or writing a program to do so should not be natural gradle way.
Gradle provides a maven plugin http://gradle.org/docs/current/userguide/maven_plugin.html.I haven't tried it out but it should be able to do it

Gradle supports Maven dependencies. Just specify the dependencies in the same way as your example:
compile group: 'groupId', name: 'artifactId', version: 'version'
To lookup the the artifact coordinates, you can use sites like http://search.maven.org
The only thing you have to make sure is to include either your internal Maven repository (if you are in a company which has one) or Maven Central:
repositories {
mavenCentral()
}
or
repositories {
maven {
url "http://repo.mycompany.com/maven2"
}
}

Related

Maven Project in IntelliJ, include Gradle Plugin

I'm new to IntelliJ and Gradle
I've got a Maven Project with a lot of dependencies which works on it's own.
Now I should use the libraries from that Project and create a Plugin for IntelliJ in Gradle.
I tried various ways to add the dependencies in the IntelliJ Module Settings, which allowed me to use the needed classes to write my code and build it. However, when I tried to start the plugin, it couldn't find the classes anymore.
I think I need to specify these in the build.gradle but I don't understand exactly how, as all the ways I tried didn't worked.
build.gradle:
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.6.5'
}
group 'com.ast.devmate.intellij'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
**compile 'com.ast.devmate.intellij:root:1.0.0-SNAPSHOT'**
}
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2019.1'
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}
gets me this:
Could not find com.ast.devmate.intellij:root:1.0.0-SNAPSHOT.
without the line marked with ** I got a lot of
error: package foo does not exist
import foo;
It looks like you're trying to access a custom library using Gradle. You will probably need to use a file dependency: How to add local .jar file dependency to build.gradle file?

gradle dependency with classifier

I am using gradle version 4.6:
in dependencies I add this:
testCompile "com.kuku:kuku:1.0:tests"
also tried this and same:
testCompile group: "com.kuku", name: "kuku", version: "1.0", classifier: "tests"
then gradle throw error:
Could not resolve all files for configuration ':testCompileClasspath'.
Could not find kuku-tests.jar (project :serializer_v9).
what I am doing wrong? needless to say I see the kuku tests jar in the build directory
So I found that the problem is Gradle includeBuild does not support yet publication that does not default: here
So the only way is to create separate jar with the test classes

How to use Oracle JDBC driver in Gradle project

I'm new with Gradle projects and I have one question. I've searched in Internet but I couldn't find what I need or maybe I couldn't know how to search it.
First I'm going to tell you my case. I have a Gradle project and I would like to execute several automated tests, in the future with jenkins, but now I want to try on Eclipse.
I have the oracle jdbc driver in /lib directory, and this is my build.gradle
apply plugin: 'java'
// In this section you declare where to find the dependencies of your project
repositories {
jcenter()
//mavenCentral()
}
// In this section you declare the dependencies for your production and test code
dependencies {
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'org.seleniumhq.selenium:selenium-java:2.+'
compile 'org.testng:testng:6.+'
//compile 'com.oracle:ojdbc14:10.2.0.4.0'
//testCompile 'net.sourceforge.jexcelapi:jxl:2.6.12'
testCompile 'info.cukes:cucumber-core:1.+'
testCompile 'info.cukes:cucumber-java:1.+'
testCompile 'info.cukes:cucumber-junit:1.+'
testCompile 'junit:junit:4.12'
}
repositories {
flatDir(dir: 'libs')//, name: 'Local libs'
}
dependencies {
compile name: 'ojdbc7'
}
I'd like to use this jdbc driver in one class but I don't know how to use it. When I tried with Maven I used this way "import oracle.jdbc.driver.OracleDriver;" but I guess this is not valid for Gradle project.
Can you help me, please?
Thanks in advance
You can try reusing your local Maven repository for Gradle:
Download ojdbc7.jar from Oracle site
Install the jar into your local Maven repository:
mvn install:install-file -Dfile=ojdbc7.jar -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.1 -Dpackaging=jar
Check that you have the jar installed into your ~/.m2/ local Maven repository
Enable your local Maven repository in your build.gradle file:
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile ("com.oracle:ojdbc7:12.1.0.1")
}
Now you should have the jar enabled for compilation in your project
You can simply add a jar as dependency, like so:
compile files('libs/ojdbc7.jar')
And there is no need to add a flatDir repository in that case. Read about it in the official user guide
Time is 2019 and Oracle finally decided to let "Maven Central becomes a distribution center for the Oracle JDBC drivers".
For example, if you want to use OJDBC version 19 with Java 8, you can find ojdbc jar in Maven Central. Please be aware there is a typo in group name. It should have been com.oracle.ojdbc instead of com.oracle.jdbc
repositories {
mavenCentral()
}
dependencies {
compile "com.oracle.ojdbc:ojdbc8:19.3.0.0"
}
In addition to correct answer, I want to share my experience how I solve a problem with ojdbs dependence (used gradle and Intellij Idea).
Go to the oracle site and download jdbs file(s). I chose to download the full archive - ojdbc8-full.tar.gz
Unpack the archive in someone directory (for example c:\folder\OJDBC8-Full)
In Intellij Idea go to the Project Structure/Libraries, press "+" symbol and specify a path to the folder there archive unpacked (OJDBC8-Full). Specify name:
In build.gradle add:
dependencies {
...
compile files('libs/OJDBC8-Full') //OJDBC8-Full - it is name what you specify for librare
...
}
Since SSO-based authentications are not available in gradle:
Currently you have 3 alternatives:
download manually and copy the file (see above)
use a proxy to authenticate (and register an account for oracle maven repo)
if you have an internal repository: you can use your repo to proxy/cache oracle's one (e.g.: Nexus Oracle settings: https://support.sonatype.com/hc/en-us/articles/213465728-How-to-configure-a-proxy-repository-to-maven-oracle-com)
(+1 use maven)
see: https://discuss.gradle.org/t/support-for-maven-repositories-that-use-realm-based-sso/14456
other than mavenCentral use local maven repository as well for our dependencies.
The reason for using the local maven repository is because the jdbc driver from Oracle is not publicly accessible.
We will have to download the driver from Oracle and install it in our local maven repo.
repositories {
mavenLocal()
}
dependencies {
compile ("com.oracle:ojdbc6:12.2.0.1")
}
mvn install:install-file -Dfile="\ojdbc6.jar" -DgroupId="com.oracle" -DartifactId="ojdbc6" -Dversion="12.2.0.1" -Dpackaging="jar" -DgeneratePom="true"
Oracle Site for driver:
https://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
Maven site:
https://maven.apache.org/download.cgi
repositories {
flatDir { dirs "libs" }
}
dependencies {
compile files( 'libs/ojdbc-16.jar')
}
create "libs" directory under project root and put that into it.
Below is a simple gradle build that uses the new 19.7 JDBC driver from Maven central. gradle run will start com.oracle.demo.App which, of course, has to be changed to run your class.
apply plugin: 'java'
apply plugin: 'application'
repositories {
mavenCentral()
}
dependencies {
implementation 'com.oracle.database.jdbc:ojdbc8-production:19.7.0.0'
testImplementation 'junit:junit:4.+'
}
sourceCompatibility = 1.11
targetCompatibility = 1.11
mainClassName = 'com.oracle.demo.App'
repositories {
mavenCentral()
}
dependencies {
https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc10
implementation group: 'com.oracle.database.jdbc', name: 'ojdbc10', version: '19.12.0.0'
}
simply add this maven dependency or any ojdbc version you want,
please make sure to click on maven link too to check if jar is present on that link if you get error while building gradle
run this command in cmd to check if all dependencies added
gradle dependencies

gradle configuration to download custom plugin dependencies

I created a gradle plugin (short named as testinfra), which has compile time and runtime dependency on 2 other jars. Here is my build.gradle for my plugin:
/**
* Define the dependencies for compiling the plugin code
*/
dependencies {
compile gradleApi()
compile localGroovy()
compile group:'com.mycompany.tools', name: 'lrgmanager', version: '1.0+'
compile group: 'com.mycompany.tools', name: 'LrgParser', version: '1.0+'
}
/**
* This task is responsible for publishing the plugin jar in the artifactory
*/
group = 'com.mycompany.tools'
version = '1.0'
publishing {
publications {
publishPlugin(MavenPublication) {
artifact jar
}
}
}
I am able to push my plugin jar to artifactory. But when I want to use the plugin, I have to specify the GAV of my plugin and also its dependencies. See below:
/**
* define classpath for buildscript{}, to download the regression plugin and its dependencies
*/
buildscript {
dependencies {
classpath group: 'com.mycompany.tools', name: 'testinfra', version: '1.0+'
classpath group:'com.mycompany.tools', name: 'lrgmanager', version: '1.0+'
classpath group: 'com.mycompany.tools', name: 'LrgParser', version: '1.0+'
}
}
apply plugin: 'testinfra'
My requirement is to avoid specifying the dependent jar's GAV along with the testinfra plugin GAV. Is there a way where I can configure my pom.xml (publish task) during publish such that when I specify GAV of my plugin, gradle identifies the dependencies and downloads them as well ?
I tried to reference the gradle user guide for this but I don't see any clear example for this.
Instead of artifact jar, use from components.java. Then the dependencies should appear in the POM, and Gradle will pull them in automatically.

How do I define a simple Gradle project with only a single jar file?

I have a Gradle project that depends on an external jar file. Currently I'm defining the dependency like this:
dependencies {
compile files('/path/to/my/jar/library.jar')
}
However I want to include it as a project dependency instead, like this:
dependencies {
compile project(':whatGoesHere?')
}
I assume I need to define a new Gradle project that contains the jar file but I don't know how to do this. I'm wondering about things like:
Do I just need to create a new build.gradle or are there more steps?
What would go in the build.gradle file?
Assume the new project contains nothing but the jar file (since it does). Also assume I know almost nothing about Gradle (because I don't!).
P.S. If it matters, this is an Android Gradle project.
As a roundup for our discussion, I'll bring simple example of "build.gradle" file, using maven local and maven central repositories:
apply plugin: 'maven'
apply plugin: 'java'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile 'commons-io:commons-io:2.4'
testCompile 'junit:junit:4.11'
}
Explanation:
"apply plugin: 'maven'" enables maven plugin, which is needed for dependency download.
"apply plugin: 'java'" enables java compilation tasks for your project.
"repositories" declares one or more repositories (maven or ivy), from where artifacts (jar libraries) will be downloaded.
"mavenLocal" refers to so-called local maven repository, which is located in "~/.m2/repository" folder on your computer. local maven repository effectively caches external repositories, but it also allows installation of local-only artifacts.
"mavenCentral" refers to maven central.
"dependencies" lists your project dependencies, either other projects or artifacts (jars).
"compile" is a configuration supported by "java" and "groovy" plugins, it tells gradle: "add these libraries to the classpath of the application during compilation phase".
"testCompile" is another configuration supported by "java" and "groovy" plugins, it tells gradle: "add these libraries to the classpath of the application during test phase".
'commons-io:commons-io:2.4' is "coordinates" of the artifact within maven repository, in form group:name:version.
You can search for well-known java libraries at address: http://mvnrepository.com/ and then include their coordinates in "build.gradle". You don't need to download anything - gradle does it for you automatically.

Resources