How to exclude dependencies from BOOT-INF/classes? - java

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

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.

Successful Gradle project build produces NoClassDefFoundError at runtime

I am using Gradle to build my Java project in Eclipse. gradle.build is as follows
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.flowpowered', name: 'flow-nbt', version: '1.0.0'
compile group: 'org.reflections', name: 'reflections', version: '0.9.10'
}
All libraries are functioning properly when run through Eclipse. But sometimes it is useful to work on the command line. When run on the command line, the runtime error Exception in thread "main" java.lang.NoClassDefFoundError: com/flowpowered/nbt/regionfile/SimpleRegionFileReader occurs, even though the build is successful and the code contains imports from those libraries. I have tried cleans and rebuilds, along with gradlew build --refresh-dependencies, but I still encounter the same runtime error.
I would assume that the libraries are just never actually imported? Or that they are not being stored where the java project thinks they are? I'm unfamiliar with Gradle, so any advice on this is welcome.
Based on the posted build.gradle file you are not packaging the application as an executable JAR.
First apply the application plugin. But this will not be enough as you won't be able to run the executable as a single JAR without all of its dependencies. Apply the shadow plugin too.
These two plugins will give you access to the following tasks:
run: execute the application from gradle's command line.
runShadow: execute the application but with all dependencies packaged in a single JAR, alongside your compiled classes and resources.
shadowJar: create a single "fat JAR" with compiled classes and all dependencies.
Thus your build.gradle may look like this
plugins {
id 'java'
id 'application'
id 'com.github.johnrengelman.shadow' version '1.2.4'
}
mainClassName = 'com.acme.YourMainClassName'
repositories {
mavenCentral()
}
dependencies {
compile group: 'com.flowpowered', name: 'flow-nbt', version: '1.0.0'
compile group: 'org.reflections', name: 'reflections', version: '0.9.10'
}
Plugin documentation:
https://github.com/johnrengelman/shadow
https://docs.gradle.org/3.4/userguide/application_plugin.html#useApplicationPlugin
Another solution without using any plugins and still end up with a runnable fat jar
jar {
archiveName = 'NameOfYourApp.jar'
manifest {
attributes 'Main-Class': 'uk.co.cdl.Main',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' '),
'Implementation-Version': project.version
}
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
include/exclude anything if need to if not take the curlys off
}
}

WildFly Swarm + War + local Jar dependencies in Gradle - NullPointerException

I am trying to build a web application server using WildFly Swarm and the application has to be able to run another Java program inside (I don't want to run it as an external process). I am trying to include the external program as .jar dependency to my web application, however, wildfly-swarm-package task always fails with the following:
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:war
:wildfly-swarm-package FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':wildfly-swarm-package'.
> java.lang.NullPointerException (no error message)
This is my gradle.build file:
buildscript {
version = System.getProperty('swarmVersion') ?: '2016.10.0'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:0.5.6.RELEASE"
classpath "org.wildfly.swarm:wildfly-swarm-plugin:$version"
}
}
apply plugin: "io.spring.dependency-management"
apply plugin: 'wildfly-swarm'
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'war'
//mainClassName = 'org.siret.prank.webapp.rest.Main'
swarm {
properties {
swarm.http.port = 8181
}
}
repositories {
mavenLocal()
mavenCentral()
maven {
url 'https://maven.repository.redhat.com/nexus/content/repositories/releases/'
}
maven {
url 'https://maven.repository.redhat.com/nexus/content/repositories/thirdparty-releases/'
}
flatDir {
dirs 'libs'
}
}
dependencyManagement {
imports {
mavenBom "org.wildfly.swarm:bom-all:$version"
}
}
dependencies {
compile group: 'org.biojava', name: 'biojava-core', version: '4.2.4'
compile group: 'org.biojava', name: 'biojava-structure', version: '4.2.4'
compile "org.wildfly.swarm:jaxrs"
compile group: 'org.wildfly.swarm', name: 'undertow', version: '2016.10.0'
compile 'org.codehaus.groovy:groovy-all:2.4.7'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: 'libs/lib', include: ['*.jar'])
}
task debugJar(dependsOn:"wildfly-swarm-package") << {
javaexec {
main="-jar";
args=[
"-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006",
"build/libs/prank-webapp-swarm.jar"
]
}
}
task runJar(dependsOn:"wildfly-swarm-package") << {
javaexec {
main="-jar";
args=[
"build/libs/prank-webapp-swarm.jar"
]
}
}
War plugin works fine, I can find the jars in WEB-INF/lib directory in the archive.
As an experiment, I tried to empty the libs folder and the error still persists.
Thanks,
Lukas
I migrated to Maven to find out that importing jar to local maven repository is the answer to the problem.
Now, both Maven an Gradle work.

Endpoints backend module fails gradle build, why?

When compiling and building my android app, an error as shown below keeps occurring. I have been using this stack overflow answer to solve the error and exclude the duplicate files.
Here is the error I get when I try to build my project.
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK com/google/appengine/repackaged/org/apache/commons/codec/language/bm/sep_approx_spanish.txt
File1: /Users/tomfinet/.gradle/caches/modules-2/files-2.1/com.google.appengine/appengine-api-1.0-sdk/1.9.28/e92c18272b555027d9ec666e7a89162f10638314/appengine-api-1.0-sdk-1.9.28.jar
File2: /Users/tomfinet/.gradle/caches/modules-2/files-2.1/com.google.appengine/appengine-endpoints/1.9.28/bf2e8a74bd28e388b3487fc78a0c7adfa592fd5d/appengine-endpoints-1.9.28.jar
When I run the terminal command ./gradlew dependencies as instructed in the stack overflow answer, I am supposed to see the entire dependency tree for my project.
However all I see is this:
Downloading https://services.gradle.org/distributions/gradle-2.10-all.zip
.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Unzipping /Users/tomfinet/.gradle/wrapper/dists/gradle-2.10-all/a4w5fzrkeut1ox71xslb49gst/gradle-2.10-all.zip to /Users/tomfinet/.gradle/wrapper/dists/gradle-2.10-all/a4w5fzrkeut1ox71xslb49gst
Set executable permissions for: /Users/tomfinet/.gradle/wrapper/dists/gradle-2.10-all/a4w5fzrkeut1ox71xslb49gst/gradle-2.10/bin/gradle
google-services plugin could not detect any version for com.google.android.gms or com.google.firebase, default version: 9.0.0 will be used.
please apply google-services plugin at the bottom of the build file.
Incremental java compilation is an incubating feature.
:dependencies
------------------------------------------------------------
Root project
------------------------------------------------------------
No configurations
BUILD SUCCESSFUL
Total time: 53.171 secs
This build could be faster, please consider using the Gradle Daemon: https://docs.gradle.org/2.10/userguide/gradle_daemon.html
Here is my gradle file for the backend:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.28'
}
}
repositories {
jcenter();
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.28'
compile 'com.google.appengine:appengine-endpoints:1.9.28'
compile 'com.google.appengine:appengine-endpoints-deps:1.9.28'
compile 'javax.servlet:servlet-api:2.5'
compile 'com.googlecode.objectify:objectify:5.0.3'
}
appengine {
downloadSdk = true
appcfg {
oauth2 = true
}
endpoints {
getClientLibsOnBuild = true
getDiscoveryDocsOnBuild = true
}
}
So how do I resolve this conflict between the dependencies? I am pretty sure I have to exclude the conflicting files. But how do I find the files that are causing this?

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/

Categories