Gradle Build Jar with Local Dependencies - java

I am building a jar file using gradle. I have local jar files as dependencies. Those jars have additional local dependencies stored in the same directory. I used the following build.gradle file
plugins { id 'application' }
repositories {
flatDir{ dirs 'lib'
}
dependencies {
implementation name: 'localjar1'
implementation name: 'localjar2'
}
application { mainClass = 'mypackage.MyApp' }
jar {
from { configurations.runtimeClasspath.collect {it.isDirectory ? : zipTree(it) } }
manifest { attributes 'Main-Class': 'mypackage.MyApp' }
}
I built the jar using gradle jar, but when I try to run the jar:
java -jar myapp\build\libs\myapp.jar
I get the following error:
Unable to initialize main class mypackage.MyApp
Caused by java.lang.NoClassDefFoundError: com/localpackage/SomeClass
How do bring that dependency in correctly?
I also tried to include the entire local library as a dependency
implementation fileTree(dir: 'lib', include: '*jar')
But when I do this I get the error: java.lang.ClassNotFoundException: mypackage.MyApp
What else should I try?
** Note: I can run the application using gradle run

Related

Error: impossible to find or load main class when running jar file on windows

I am working on a java project using gradle for a homework assignment and I got troubles running JAR files built off gradle via the windows command prompt :
image
Here's my build.gradle txt :
apply plugin: 'java'
apply plugin: 'antlr'
repositories {
mavenCentral()
}
dependencies {
antlr 'org.antlr:antlr4:4.7.1'
testCompile 'junit:junit:4.12'
}
generateGrammarSource {
arguments += ['-no-visitor', '-no-listener']
outputDirectory = new File(buildDir.toString() + "/generated-src/antlr/main/TP2/")
}
jar {
manifest {
attributes (
'Main-Class': 'TP2.Main',
'Class-Path': configurations.runtimeClasspath.files.join(' ')
)
}
}
I've already checked my classpath directory, class name... When I type the next command : jar -tf build\libs\TP2.jar I can see my class well packed in my jar and my class is at this directory : build\classes\java\main\TP2\Main.class
So I don't see why it is not working.
I'm not an expert with gradle that's why any help would be really appreciated but I think the problem propably come from the manifest.

Building gradle plugin

I'm developing a gradle plugin alongside with a project:
plugin_consumer
|
+ build.gradle
+ src/
plugin
|
+ build.gradle
+ libs/
+ src/
All jars are moved to plugin/libs after built, and plugin consumer module uses plugin like so:
buildscript {
repositories {
// ...
}
dependencies {
classpath fileTree(dir:'../plugin/libs', include: ['*.jar'])
}
}
apply plugin: 'my.custom.plugin'
I tested and this setup worked fine, however the plugin .jar is being built without dependencies, and as soon as I add some dependencies to the project the consumer buildscript started crashing because of NoClassDefFoundError exceptions. The problem is when I build a fat jar instead the script no longer finds plugin:
// from plugin/build.gradle
// Configurations to build jar with dependencies
configurations {
jar.archiveName = 'plugin.jar'
}
jar {
archiveName = 'plugin.jar'
baseName = project.name + '-all'
from { configurations.compile.collect {
println it
it.isDirectory() ? it : zipTree(it)
} }
}
Running commands on plugin_consumer project throws error:
> Failed to apply plugin [id 'my.custom.plugin']
> Plugin with id 'my.custom.plugin' not found.
I haven't identified the problem, but have found a solution. Somehow the jar instruction to package all of the sources have messed up the plugin jar file. The following configurations have worked on plugin side instead:
//...
jar {
from {
configurations.compileWithDependencies.collect { it.isDirectory() ? it : zipTree(it) }
}
}
configurations {
compileWithDependencies
}
dependencies {
compile gradleApi()
compileWithDependencies 'my.dependency1'
compileWithDependencies 'my.dependency1'
testCompile 'test.dependency'
configurations.compile.extendsFrom(configurations.compileWithDependencies)
}
From the looks of it I would guess that gradleApi() dependencies should not be compiled into the plugin jar.

Gradle dependency issue, java.lang.NoClassDefFoundError, but compiles

new to gradle and java here and I am trying to use Android's Log method from android.util.Log. It seems I can compile and it finds what it needs, but it can not find it at runtime. I have tried using 'runtime' instead of 'compile' in the dependencies section, but not luck.
java -jar build/libs/testJavaHttp.jar
Exception in thread "main" java.lang.NoClassDefFoundError: android/util/Log
at myproject.test.HttpToFile.downloadFile(HttpToFile.java:20)
at myproject.test.Main.main(Main.java:12)
Caused by: java.lang.ClassNotFoundException: android.util.Log
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
In build.gradle
apply plugin: 'java'
dependencies {
compile files('../androidsdk/platforms/android-25/android.jar')
}
jar {
manifest {
attributes 'Main-Class': 'myproject.test.Main'
}
}
You are trying to run the jar and the jar is not packed with dependencies, use shadow jar plugin or pack required artifacts in, by extending the jar task.
jar {
archiveName = 'Name.jar'
manifest {
attributes 'Main-Class': 'myproject.test.Main',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' '),
'Implementation-Version': 1.0
}
from(configurations.myconfig.collect { it.isDirectory() ? it : zipTree(it) }) {
// in here you can exclude what you need as well if needed.
}
}
To not pull the 'entire world' into a jar you can create configuration just with libraries you require:
configurations{
myconfig // to create configuration
compile.extendsFrom(myConfig) //to include it in compile as well
}
and then use this config in the jar creation and in dependecies.
dependencies {
myconfig files('../androidsdk/platforms/android-25/android.jar')
}
But looking at your code there is not much to pull there. You need to consider packing all needed artifacts and transitives if you expect it to be a runnable jar, as you run it with 'java -jar [...]' command.
PS. Everything that is in compile is going to be included in runtime configuration as well.

How do i fix my dependencies in my build.gradle file

I am new to gradle, i need to configure my build.gradle file . Am using selenium webdriver and i have list of .jar files. how do i include this jar files as dependencies in my build.gradle file?. i have this .jar in a folder called lib in my package. and i have
dependencies {
compile fileTree(dir: 'lib', includes: '*.jar')
}
but i keep having the error below:
FAILURE: Build failed with an exception.
Where:Build file '/home/ola/workspace/build.gradle' line: 20
What went wrong:
A problem occurred evaluating root project 'workspace'. Cannot cast object '*.jar' with class 'java.lang.String' to class 'java.lang.Iterable'
please can anyone point me to how to write dependencies for a webdriver project using gradle.This is the path to my lib folder: "/home/user/workspace/mainsite_automation/src/autoTest/lib/"
Thanks
You just need to specify the dependencies repository and the selenium webdriver dependencies so you will end up with a build.gradle similar to this:
apply plugin: 'java'
apply plugin: 'jetty'
repositories {
mavenCentral()
}
sourceSets {
selenium
}
dependencies {
seleniumCompile 'junit:junit:4.11'
seleniumCompile 'org.seleniumhq.selenium:selenium-java:2.30.0'
}
task jettyDaemon(type: org.gradle.api.plugins.jetty.JettyRun) {
daemon = true
}
task selenium(type: Test, dependsOn: jettyDaemon) {
testClassesDir = sourceSets.selenium.output.classesDir
classpath = sourceSets.selenium.runtimeClasspath
}
for Eclipse, you can add selenium dependencies to the classpath adding this to your build.gradle:
eclipse {
classpath {
plusConfigurations += configurations.seleniumCompile
}
}
then you can use grade clean selenium to rebuild your project.
Sources:
https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html
http://www.dreamchain.com/gradle-selenium-webdriver-task/

Gradle- Importing a JAR file with absolute windows path?

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')
}

Categories