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

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/

Related

Gradle Build Jar with Local Dependencies

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

Gradle Builds an Empty Jar File

I converted a project to gradle using gradle init from maven after updating the dependencies. gradle test works as expected. However, when I run gradle build, the jar file that's generated is entirely empty.
I've attempted to tweak the source set to make something happen, but that doesn't seem to solve the problem. The directory structure matches what gradle expects from what I can tell everything is nested in src/main/groovy
The project's full code is available on Github.
In general what causes no files to be added to a build? Is there additional configuration I need to add besides whatever gradle init creates?
Gradle build file:
plugins {
id 'java'
id 'maven-publish'
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
dependencies {
implementation 'org.codehaus.groovy:groovy-all:3.0.5'
implementation 'com.github.javafaker:javafaker:1.0.2'
testImplementation 'org.spockframework:spock-core:2.0-M3-groovy-3.0'
testCompileOnly 'org.projectlombok:lombok:1.18.12'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
}
group = 'nl.topicus.overheid'
version = '0.2.0'
description = 'java-factory-bot'
sourceCompatibility = '1.8'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
What going on now is the project is try to build as Java project and get src from src/main/java as it is default from Java project. So, you need id 'groovy' not id 'java' in plugins section to make it look into src/main/groovy and build both .java and .groovy files in there.

How to exclude dependencies from BOOT-INF/classes?

I have a spring boot project and I get this error when I try to build it:
> gradle build
:processResources
:compileJava
:classes
:jar FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':jar'.
> archive contains more than 65535 entries.
To build this archive, please enable the zip64 extension.
See: https://docs.gradle.org/3.5.1/dsl/org.gradle.api.tasks.bundling.Zip.html#org.gradle.api.tasks.bundling.Zip:zip64
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Ok, I added the zip64 = true option to the jar task in the gradle configuration.
Now it can build the jar successfully but when I try to execute the jar, I get the following exception:
Exception in thread "main" java.lang.IllegalStateException: java.lang.IndexOutOfBoundsException
at org.springframework.boot.loader.ExecutableArchiveLauncher.<init>(ExecutableArchiveLauncher.java:43)
at org.springframework.boot.loader.JarLauncher.<init>(JarLauncher.java:37)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58)
Caused by: java.lang.IndexOutOfBoundsException
at org.springframework.boot.loader.jar.AsciiBytes.<init>(AsciiBytes.java:69)
at org.springframework.boot.loader.jar.CentralDirectoryFileHeader.load(CentralDirectoryFileHeader.java:95)
at org.springframework.boot.loader.jar.CentralDirectoryParser.parseEntries(CentralDirectoryParser.java:68)
at org.springframework.boot.loader.jar.CentralDirectoryParser.parse(CentralDirectoryParser.java:57)
at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:118)
at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:106)
at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:92)
at org.springframework.boot.loader.jar.JarFile.<init>(JarFile.java:83)
at org.springframework.boot.loader.archive.JarFileArchive.<init>(JarFileArchive.java:61)
at org.springframework.boot.loader.archive.JarFileArchive.<init>(JarFileArchive.java:57)
at org.springframework.boot.loader.Launcher.createArchive(Launcher.java:129)
at org.springframework.boot.loader.ExecutableArchiveLauncher.<init>(ExecutableArchiveLauncher.java:40)
... 2 more
It turned out that SpringBoot doesn't even support the zip64 format, so I had to make further investigation.
Where do the 65535+ entries come from? Obviously these come from dependencies, because the issue exists since
I added a new dependency in the build.gradle file. While examining the zip64 jar, I have found that
all the dependency classes! are under the BOOT-INF/classes folder.
As I understand, the structure must look like
BOOT-INF/
classes/
<only this application's compiled classes>
libs/
<all the dependency jars>
But my classes folder has all the dependency jars "extracted" to it.
(As you can see)
I extracted the jar, removed all the dependency classes from this folder and rezipped it. (Like this)
This way it can be run without any problem, so I'm sure these files are unneccesary.
Can somebody help me, how to exclude these dependency classes from the structure? Thank you in advance!
Here's my relevant gradle configuration:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
ext.springBootVersion = '1.4.2.RELEASE'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")
}
}
configurations {
provided.all*.exclude group: 'javax.servlet'
}
mainClassName = 'com.path.to.my.MainClass'
repositories {
mavenCentral()
maven {
url "https://repository.jboss.org/nexus/content/repositories/releases"
}
maven {
url "https://repo.eclipse.org/content/groups/releases/"
}
}
jar {
//zip64 = true
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes("Main-Class": mainClassName)
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile 'aopalliance:aopalliance:1.0'
compile 'com.google.code.gson:gson:2.7'
compile 'com.google.gdata:core:1.47.1'
compile 'com.google.guava:guava:19.0'
compile 'commons-io:commons-io:2.4'
compile 'javax.json:javax.json-api:1.0'
compile 'mysql:mysql-connector-java:5.1.22'
compile 'org.apache.commons:commons-csv:1.4'
compile 'org.flywaydb:flyway-core:4.0.3'
compile 'org.glassfish:javax.json:1.0.4'
......
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
bootRun {
addResources = true
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}

Why Gradle is compiling .java files but not creating my .jar?

I recently started using Gradle but i'm having an issue compiling my application.
This is my build.gradle:
group 'net.zentya'
version '1.0-SNAPSHOT'
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()
maven { url "https://hub.spigotmc.org/nexus/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/groups/public" }
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile 'org.spigotmc:spigot-api:1.11.2-R0.1-SNAPSHOT'
compile 'org.bukkit:bukkit:1.11.2-R0.1-SNAPSHOT'
compile 'net.md-5:bungeecord-chat:1.11-SNAPSHOT'
}
sourceSets {
main {
java {
srcDir 'src'
}
}
}
When building with the IntelliJ's pre-made button i get my project compiled's class files into the path {project}/gradle/classes/main . But i don't have the final JAR.
How i can fix?
gradle jar or gradle shadowJar commands can be used to create the jar file of gradle based applications.

How to use Gradle to build a JAR that depends on another JAR

I have a Eclipse workspace with a declared workset configured to have several projects. Some are to generate JAR files and others are web applications that use those JAR files. In my architecture I have a JAR that will consist of domain core services and another one that depends on the first one that will consist of higher level services. Finally I will have some web applications that use those both JARs.
The first JAR project is build with Gradle, based on the following script
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
repositories {
mavenLocal()
mavenCentral();
}
jar {
baseName = 'br.ufpr.unidades.dominio'
version = '0.1.0'
}
dependencies {
compile 'org.hibernate:hibernate-core:4.3.7.Final'
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
As anyone can see, it´s a very simple build.gradle file and the build works fine with it. The expected JAR file is generated in the expected destination folder.
Now, here comes the build script for the second JAR:
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
repositories {
mavenLocal()
mavenCentral()
}
jar {
baseName = 'br.ufpr.unidades.dominio.hibernate'
version = '0.1.0'
}
dependencies {
runtime fileTree(dir: '../dominio/build/libs', include: '*.jar')
compile 'org.hibernate:hibernate-core:4.3.7.Final'
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
The second file is very similar to the first one, except it has a dependency on generated JAR:
runtime fileTree(dir: '../dominio/build/libs', include: '*.jar')
Eclipse doesn´t show any problems, but when I try to build the second JAR project I get many Class not found error messages, like the one below:
[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks:
[sts] build
[sts] -----------------------------------------------------
:compileJava
D:\Users\alex\Documents\Eclipse\workspace\unidades\dominio.hibernate\src\main\java\dominio\hibernate\HibernateCargoRepository.java:7: error: package unidades.dominio does not exist
import unidades.dominio.Cargo;
The message is clear: I´m importing a package that is not being found during the build, so the classes such a package has cannot be referenced in my code. Such a package is declared in the first and perfectly generated JAR file. It also is visible under Referenced Libraries item in the Eclipse project, so Gradle was able to find it to reference it in design time, but not to build the second JAR.
After all that, I suspect it´s a dependency management problem, but I can´t see which it is and how to fix it.
Thanks in advance!
Are You using classes from the jar under dominio/build/libs in the second project? If so, this should be a compile dependency. I'd also recommend setting a multimodule gradle project. Here are the docs.

Categories