Gradle- Importing a JAR file with absolute windows path? - java

How do I import a JAR file as a dependency through a full Windows URL? I'm trying to import JAR file on a network drive (labeled "H") and I cannot use Maven Central easily because of our silly firewall.
when I try to use compile files I still get compile errors due to the dependency classes not being found.
apply plugin: 'java'
dependencies {
compile files ("H/Processes/Development/libraries/hikari-cp/HikariCP-2.4.1.jar")
}
sourceSets {
main.java.srcDir "src/main/java"
main.resources.srcDir "src/main/resources"
}
jar {
from configurations.compile.collect { entry -> zipTree(entry) }
}

It looks like there is an error in your path. It should be corrected like this.
H:/Processes/Development/libraries/hikari-cp/HikariCP-2.4.1.jar
If you want to add more than one jar file as dependencies, here is how to do it(relative path is used in here)
dependencies {
compile fileTree(dir: '../../lib', include: '**/*.jar')
}

Related

Gradle (java): test task should use generated .jar and not the .class files in classpath

Gradle with apply plugin: 'java' in build.gradle. The file will create a .jar file and the test task is running junit tests:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
This is working. But to make sure the public API tests are working with the generated .jar file I want that the 'test' task is running the test with the generated .jar file from the build/libs folder in classpath and not with the generate .class files from folder build/classes in in classpath.
Not working because the sourceSets is set global:
tasks.withType(Test) {
sourceSets {
main {
java {
exclude '**'
}
}
}
}
Partly working: multiproject (test and jar separated in two gradle projects):
dependencies {
compile project(":jar_project")
testCompile 'junit:junit:4.12'
}
in this case jar_project.jar is used but package private test are also executed without an error.
Do somebody have an idea how to run the tests with the .jar as dependency and ignoring the .class files?
Thank you, pulp
The problem is that the test task does not depend on the jar task, which sort of makes sense because the jar is supposed to package the classes, so there should be no reason for tests to depend on that jar.
You can force the dependency by:
test {
dependsOn jar
doFirst {
classpath += jar.outputs.files
}
}
Now the jar will be on the test classpath

Changing resource target dir in gradle built jar

I have the following structure in the project:
Project_1 -> src->main->java->all_java_files
-> src->main->resources-> all_prop_files
I want to build the jar so that the property files will be put under 'main/resources' inside the jar. By default gradle puts them under root.
Here is my build file with my fix. But this causes sonarqube indexing issues.Is there an easy way to change output directory for the resource files?:
apply plugin: 'java'
dependencies {
compile libraries.log4j
compile libraries.apache_commons_net
compile libraries.apache_commons_lang2
compile libraries.apache_velocity
compile libraries.ehcache_15
compile libraries.apache_commons_validator
compile libraries.apache_xmlbeans
compile libraries.spring_framework
}
compileJava.classpath += was70ServerLibraryJars
sourceSets {
main {
resources{
srcDirs = ['src']
exclude 'main/java'
}
}
}
jar {
}
Why don't you move
src->main->resources-> all_prop_files
to
src->main->resources->main->resources-> all_prop_files
By convention everything in src/main/resources will be copied in the root of the JAR.

Dependencies not showing up in jar with Gradle?

I am using this build.grade. When I run gradlew build, it generates a jar file only with the source, not the stone.jar in the libs folder. How should I be doing this?
apply plugin: 'java'
apply plugin: 'eclipse'
// Source sets in the project, specify source directories
sourceSets {
main {
java.srcDir("${projectDir}/src/")
resources.srcDir("${projectDir}/src/")
}
}
// Dependencies for the project are stored in the libs directory
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
// Control what goes into the JAR
jar {
manifest {
attributes 'Main-Class': 'com.elsea.sublimelauncher.Driver'
}
// Include all the classes except the tests
include("com/elsea/sublimelauncher/**")
}
By default, jar task in gradle builds an executable jar file from your project source files. It will not contain any transitive libs that are needed for your program. It is good for web servers, because they usually keep all jars in a special lib folder, but is not good for many standalone programs.
What you want to do is to create a fat jar, that will contain all classes and resources in a single jar file.
If you used Maven you could see files like 'my-program-v1.0-jar-with-dependcies.jar'
There is a shadow plugin for gradle that can do the same thing. Wiki on github contains all the information about how to use it in your project.

How to recursively add a list of Jars to your classpath in gradle with groovy script [duplicate]

I have tried to add my local .jar file dependency to my build.gradle file:
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src/model'
}
}
}
dependencies {
runtime files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
runtime fileTree(dir: 'libs', include: '*.jar')
}
And you can see that I added the .jar files into the referencedLibraries folder here: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1/referencedLibraries
But the problem is that when I run the command: gradle build on the command line I get the following error:
error: package com.google.gson does not exist
import com.google.gson.Gson;
Here is my entire repo: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1
According to the documentation, use a relative path for a local jar dependency as follows.
Groovy syntax:
dependencies {
implementation files('libs/something_local.jar')
}
Kotlin syntax:
dependencies {
implementation(files("libs/something_local.jar"))
}
If you really need to take that .jar from a local directory,
Add next to your module gradle (Not the app gradle file):
repositories {
flatDir {
dirs("libs")
}
}
dependencies {
implementation("gson-2.2.4")
}
However, being a standard .jar in an actual maven repository, why don't you try this?
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.code.gson:gson:2.2.4")
}
You could also do this which would include all JARs in the local repository. This way you wouldn't have to specify it every time.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
The following works for me:
compile fileTree(dir: 'libs', include: '*.jar')
Refer to the Gradle Documentation.
You can try reusing your local Maven repository for Gradle:
Install the jar into your local Maven repository:
mvn install:install-file -Dfile=utility.jar -DgroupId=com.company -DartifactId=utility -Dversion=0.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 {
implementation ("com.company:utility:0.0.1")
}
Now you should have the jar enabled for implementation in your project
A solution for those using Kotlin DSL
The solutions added so far are great for the OP, but can't be used with Kotlin DSL without first translating them. Here's an example of how I added a local .JAR to my build using Kotlin DSL:
dependencies {
compile(files("/path/to/file.jar"))
testCompile(files("/path/to/file.jar"))
testCompile("junit", "junit", "4.12")
}
Remember that if you're using Windows, your backslashes will have to be escaped:
...
compile(files("C:\\path\\to\\file.jar"))
...
And also remember that quotation marks have to be double quotes, not single quotes.
Edit for 2020:
Gradle updates have deprecated compile and testCompile in favor of implementation and testImplementation. So the above dependency block would look like this for current Gradle versions:
dependencies {
implementation(files("/path/to/file.jar"))
testImplementation(files("/path/to/file.jar"))
testImplementation("junit", "junit", "4.12")
}
The accepted answer is good, however, I would have needed various library configurations within my multi-project Gradle build to use the same 3rd-party Java library.
Adding '$rootProject.projectDir' to the 'dir' path element within my 'allprojects' closure meant each sub-project referenced the same 'libs' directory, and not a version local to that sub-project:
//gradle.build snippet
allprojects {
...
repositories {
//All sub-projects will now refer to the same 'libs' directory
flatDir {
dirs "$rootProject.projectDir/libs"
}
mavenCentral()
}
...
}
EDIT by Quizzie: changed "${rootProject.projectDir}" to "$rootProject.projectDir" (works in the newest Gradle version).
Shorter version:
dependencies {
implementation fileTree('lib')
}
The Question already has been answered in detail. I still want to add something that seems very surprising to me:
The "gradle dependencies" task does not list any file dependencies. Even though you might think so, as they have been specified in the "dependencies" block after all..
So don't rely on the output of this to check whether your referenced local lib files are working correctly.
A simple way to do this is
compile fileTree(include: ['*.jar'], dir: 'libs')
it will compile all the .jar files in your libs directory in App.
Some more ways to add local library files using Kotlin DSL (build.gradle.kts):
implementation(
files(
"libs/library-1.jar",
"libs/library-2.jar",
"$rootDir/foo/my-other-library.jar"
)
)
implementation(
fileTree("libs/") {
// You can add as many include or exclude calls as you want
include("*.jar")
include("another-library.aar") // Some Android libraries are in AAR format
exclude("bad-library.jar")
}
)
implementation(
fileTree(
"dir" to "libs/",
// Here, instead of repeating include or exclude, assign a list of paths
"include" to "*.jar",
"exclude" to listOf("bad-library-1.jar", "bad-library-2.jar")
)
)
The above code assumes that the library files are in libs/ directory of the module (by module I mean the directory where this build.gradle.kts is located).
You can use Ant patterns in includes and excludes as shown above.
See Gradle documentations for more information about file dependencies.
Thanks to this post for providing a helpful answer.
I couldn't get the suggestion above at https://stackoverflow.com/a/20956456/1019307 to work. This worked for me though. For a file secondstring-20030401.jar that I stored in a libs/ directory in the root of the project:
repositories {
mavenCentral()
// Not everything is available in a Maven/Gradle repository. Use a local 'libs/' directory for these.
flatDir {
dirs 'libs'
}
}
...
compile name: 'secondstring-20030401'
The best way to do it is to add this in your build.gradle file and hit the sync option
dependency{
compile files('path.jar')
}
The solution which worked for me is the usage of fileTree in build.gradle file.
Keep the .jar which need to add as dependency in libs folder. The give the below code in dependenices block in build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
You can add jar doing:
For gradle just put following code in build.gradle:
dependencies {
...
compile fileTree(dir: 'lib', includes: ['suitetalk-*0.jar'])
...
}
and for maven just follow steps:
For Intellij:
File->project structure->modules->dependency tab-> click on + sign-> jar and dependency->select jars you want to import-> ok-> apply(if visible)->ok
Remember that if you got any java.lang.NoClassDefFoundError: Could not initialize class exception at runtime this means that dependencies in jar not installed for that you have to add all dependecies in parent project.
For Gradle version 7.4 with Groovy build file
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation ':gson-2.2.4'
}
If you are on gradle 4.10 or newer:
implementation fileTree(dir: 'libs', includes: ['*.jar'])
Goto File -> Project Structure -> Modules -> app -> Dependencies Tab -> Click on +(button) -> Select File Dependency - > Select jar file in the lib folder
This steps will automatically add your dependency to gralde
Very Simple
Be careful if you are using continuous integration, you must add your libraries in the same path on your build server.
For this reason, I'd rather add jar to the local repository and, of course, do the same on the build server.
An other way:
Add library in the tree view. Right click on this one. Select menu "Add As Library".
A dialog appear, let you select module. OK and it's done.

Gradle to pull down dependencies into local lib dir

I have the following list of JAR dependencies (the following is my entire build.gradle file):
apply plugin: 'java'
dependencies {
compile 'com.fasterxml.jackson.core:jackson-annotations:2.2.3'
compile 'com.wordnik:swagger-annotations:1.3.4'
compile 'javax.validation:validation-api:1.0.0.GA'
compile 'io.dropwizard:dropwizard-core:0.7.0'
compile 'io.dropwizard:dropwizard-client:0.7.0'
compile 'com.wordnik:swagger-jaxrs_2.10:1.3.4'
compile 'org.eclipse.jetty:jetty-servlets:8.1.14.v20131031'
}
I would like to run Gradle and have it pull all of these JARs (and their transitive deps) from Maven and place the JARs in a local lib directory.
When I run this I get a BUILD SUCCESSFUL message, but I don't see a lib directory under my main TestGradleroot` dir. I was expecting to see a directory that would contain all of these JARs as well as their transitive dependencies.
Try to use separate 'compile' for each library.
This is how it looks on mine android project:
dependencies {
compile 'com.google.android.support:wearable:+'
compile 'com.google.android.gms:play-services-wearable:+'
}

Categories