Add dependency jar files from outside of root project in Gradle - java

I would like to add jar files outside of root project in build.gradle.
I knew I can add jar files under root project.
Example:
dependencies {
compile fileTree('MyLibs')
}
It will include all jar files under root.Project.name/MyLibs
What I want is including my jar files are outside of root.Project.name directory.
Example: On my MacOSX
/Users/MyJava/MyLibs/alllibs.jar
I did try the following so far:
dependencies {
compile fileTree('/Users/MyJava/MyLibs')
}
But it doesn't work.
All the above jars files are inside my local file system(My MacOSX).
Here is my full build.gradle file if are wondering.
group 'try_gradle'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
dependencies {
compile fileTree('/Users/MyJava/MyLibs')
}

Related

Gradle dependencies not being exported to runnable jar by eclipse

I am trying to export my gradle eclipse project to a runnable jar, however it's dependencies are not being bundled into the runnable jar itself.
I file -> export -> runnable jar -> Extract required libraries into generated jar.
They are correctly exported if I manually do "add external jar" from the configure build path option, however I want to do this via gradle, and they are not correctly exported without the manual add.
I have tried to do a gradlew clean, and gradlew build. I have refreshed my gradle dependencies, and I have rebuilt the project, as well as cleaned it via the eclipse project -> clean option.
Below is my build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.4.RELEASE")
}
}
plugins {
id 'java'
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-activemq")
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'org.json:json:20171018'
compile 'org.mongodb:mongo-java-driver:3.6.4'
compile group: 'com.github.jai-imageio', name: 'jai-imageio-core', version: '1.4.0'
compile files ('../libs/CommonUtils.jar')
compile files ('../libs/UMS.jar')
compile files ('../libs/Cache.jar')
}
jar {
from { configurations.runtime.collect { zipTree(it) } }
}
When I look into the jar generated, I see that it doesn't contain the dependencies inside it, and when I go to run the jar via java -jar jar.jar, I get a NoClassDefFound error. I want to get all my gradle dependencies bundled into a runnable jar so I can do a java -jar jar.jar and have my jar run as if I had hit run in the eclipse editor.
The build tools itself will create the runnable jar.
gradle clean
gradle taskName
Project Jar will be created under folder :
$project/build/libs/ folder.

Is there a way to download dependencies jar from a Network location in Gradle 4.0?

Is there a way to download dependencies jar from a Network location in Gradle 4.0?
For example:
group 'com.hello'
version '1.0-SNAPSHOT'
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
compile 'https://<hostname>:port/hello.jar' // can I do this?
}
Gradle has had this capability before 4.0, but it requires you to establish your own Maven or Ivy repository.
https://docs.gradle.org/current/userguide/dependency_management.html#sub:maven_repo
While I'm sure you could alternatively write some standard Groovy code to simply download a jar into the libs folder

Gradle - Ignore specfic jars from Jar file

I am creating a project which creates Jar files out of it and i wanted some of the jars to be excluded from the final jar created from the project.
I am trying as below, even though i have given providedCompile, those jars also included in the final jar.
Tried with providedRuntime, i was getting compile time error. also i want ignore Tomcat related jars, can anyone help me?
apply plugin: "spring-boot"
apply plugin: "java"
apply plugin: "groovy"
apply plugin: "eclipse"
apply plugin: "idea"
repositories {
mavenLocal()
mavenCentral
}
configurations {
providedCompile
compile.extendsFrom providedCompile
}
jar {
exclude ("*db/**")
}
dependencies {
compile ("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") {
exclude group:"org.springframework.boot", module: "spring-boot-starter-tomcat"
}
providedCompile "com.oracle:ojdbc6:11.2.0"
providedCompile "javax.servlet:javax.servlet-api:${servletApiVersion}"
// hc-commons jars
providedCompile "com.test.hc-commons:hc-commons-exception:${hcCommonsVersion}"
providedCompile "com.test.hc-commons:hc-commons-utils:${hcCommonsVersion}"
providedCompile "com.test.hc-commons:hc-commons-typeconverters:${hcCommonsVersion}"
providedCompile "com.test.hc-commons:hc-commons-mailing:${hcCommonsVersion}"
compile ("org.springframework.boot:spring-boot-starter-velocity:${springBootVersion}") {
exclude group:"org.springframework.boot", module: "spring-boot-starter-tomcat"
}
}
Note: I have tried with provided-base plugin and other suggestions given in the google
Thanks for your help
I was able to resolve using jar method as below
jar {
from {
(configurations.runtime - configurations.providedCompile).collect {
it.isDirectory() ? it : it
}
}
}
this jar method create simple jar excluding providedCompile jars but build task still add jar. need to use 'jar' task

How do you build a .jar that contains external .jar dependencies?

I'm using the Microsoft JDBC Driver 4.1 to connect to SQL Server. In Eclipse I have added the .jar file to the class path of my project ( C:\Program Files\Microsoft JDBC Driver 4.1 for SQL Server\sqljdbc_4.1\enu\sqljdbc41.jar).
I've added this .jar to a folder called lib in my project and am trying to have this .jar added as part of my .jar:
repositories {
mavenCentral()
}
dependencies {
compile files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
However, sqljdbc41.jar is not included in my .jar. Am I missing something?
You have to create a (application) distribution to package your libraries and application jar in a folder: http://gradle.org/docs/current/userguide/application_plugin.html
Alternatively you can use a plugin like https://github.com/johnrengelman/shadow to create a self-contained uberjar.
Thanks chromanoid. I was able to solve the problem two ways. First I placed the sqljdbc41.jar in a new folder called lib in my project. Solution 1 was to create an application distribution:
apply plugin:'application'
apply plugin: 'java'
mainClassName = "main.java.application.Main"
repositories {
mavenCentral()
}
dependencies {
runtime files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
You can then create a distribution using the distZip task. Solution 2 was to create a fat jar.
apply plugin: 'java'
repositories {
mavenCentral()
}
configurations {
provided
compile.extendsFrom provided
}
dependencies {
runtime files('lib/sqljdbc41.jar')
testCompile group: 'junit', name: 'junit', version: '4.+'
}
jar {
dependsOn configurations.runtime
from {
(configurations.runtime - configurations.provided).collect {
it.isDirectory() ? it : zipTree(it)
}
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
}
and use the build task.

Gradle: How to use one-jar output as input for launch4j

I'd like to create a exe file, without having to put all required libraries beside the exe.
Formerly with ant I created a self-contained jar file with one-jar and then wrapped this into a exe file with launch4j.
Gradle has plugins for both and standalone both work very well with almost no configuration.
But how can I manage to use the created one-jar as the input for launch4j?
This is my current buildfile:
apply plugin: 'java'
apply plugin: 'launch4j'
apply plugin: 'gradle-one-jar'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'edu.sc.seis.gradle:launch4j:1.0.6'
classpath 'com.github.rholder:gradle-one-jar:1.0.4'
}
}
launch4j {
mainClassName = "de.my.umkopierer.Umkopierer"
launch4jCmd = "C:/Program Files (x86)/Launch4j/launch4j"
jar = "lib/Umkopierer-1.0.jar"
headerType = "console"
dontWrapJar = false
}
sourceCompatibility = 1.7
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Umkopierer', 'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
compile 'com.google.guava:guava:18.0'
compile 'com.fasterxml.jackson.core:jackson-core:2.4.4'
compile 'com.fasterxml.jackson.datatype:jackson-datatype-jdk7:2.4.4'
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.4'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
task oneJar(type: OneJar) {
mainClass = "de.stiffi.umkopierer.Umkopierer"
}
I solved this by making the launch4j task 'createExe' dependent on the onejar/fatjar (or any other fat jar creation method). E.g.:
tasks.createExe.dependsOn('oneJar')
task launch4j(overwrite: true, dependsOn: ['createExe']){
}
Also I think that your gradle build file should contain a main class attribute like
manifest {
attributes 'Main-Class':'com.example.MyMainClass'
}
(at least that's the case if you are using the fatjar gradle plugin).

Categories