i have some functional tests in {project_home}/src/test/groovy/
i want to be able to use a gradle task to:
Make a jar file from all the .groovy files under {project_home}/src/test/groovy/ including all of the dependencies
be able to run this jar file as a substitute of running individual groovy files
All the example i came across have a main method , the one i have have no main()
so i tried to use the "Project's" build.gradle and appended
task fatJar(type: Jar) {
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
and ran $gradle clean fatJar
but got compile failure:
:Tests:EndToEndFunctionalTests:fatJar FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':Tests:EndToEndFunctionalTests:fatJar'.
> archive contains more than 65535 entries.
To build this archive, please enable the zip64 extension.
See: https://docs.gradle.org/2.7/dsl/org.gradle.api.tasks.bundling.Zip.html#org.gradle.api.tasks.bundling.Zip:zip64
i do have the jar file under /build/libs
task uberJar(type: Jar,dependsOn:[':compileGroovy']) {
zip64 true
from files(sourceSets.main.output.classesDir)
from configurations.runtime.asFileTree.files.collect {zipTree(it) }
with jar
}
Solved the issue.
Related
In my Gradle project, I define an additional sourceSet.
sourceSets {
sample {
java {
srcDir 'sample/java'
}
compileClasspath += sourceSets.main.output + sourceSets.main.compileClasspath
}
}
I then add a task:
task sampleJar(type: Jar) {
classifier 'sample'
from sourceSets.sample.output
}
artifacts {
archives sampleJar
}
If I do > gradle build the additional jar file builds from the additional source set. However, if I do > gradle jar, it doesn't. any reason why?
When I go through the output messages, I see:
gradle build has sampleJar in the Tasks to be executed:
but
gradle jar doesn't.
But unsure as to why?
Because jar is just the task that assembles the main jar file.
build, on the other hand, is the top-level life-cycle task, which depends on assemble. And assemble is documented as
Depends on: jar, and all other tasks that create artifacts attached to the archives configuration.
Since your sampleJar pecisely creates an artifact attached to the archives configuration, assemble, and thus build depends on it.
I have a Kotlin project with JDA API which I need to deploy on Heroku environment. To achieve that I created a JAR task in my build.gradle file.
jar {
baseName = 'discord-stats-bot'
version = 'v1'
manifest {
attributes('Main-Class': 'com.vchernogorov.discordbot.BotKt')
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
But I was unable to build this JAR file because of the following error.
16:02:03 vchernogorov $ ./gradlew jar
:kaptGenerateStubsKotlin UP-TO-DATE
:kaptKotlin UP-TO-DATE
:compileKotlin UP-TO-DATE
:compileJava UP-TO-DATE
:copyMainKotlinClasses UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:inspectClassesForKotlinIC UP-TO-DATE
:jar
FAILURE: Build failed with an exception.
* What went wrong:
Could not expand ZIP '/Users/vchernogorov/.gradle/caches/modules-2/files-2.1/club.minnced/opus-java/1.0.4/596995aaf2f5b5091c4d251fdc11fa62680cc59e/opus-java-1.0.4.pom'.
> archive is not a ZIP archive
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 6.784 secs
This library is a dependency in a JDA project. So I need help with configuring this jar task in order to correctly build my executable and deploy it to Heroku.
Edit: Here's my dependencies block in build.gradle.
dependencies {
compile 'com.google.guava:guava:23.0'
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.16"
compile "org.jsoup:jsoup:1.10.3"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "net.dv8tion:JDA:3.8.0_436"
compile "khttp:khttp:0.1.0"
compile 'com.google.code.gson:gson:2.8.1'
}
In maven there's a special artifact of type pom. It is also published and downloaded as a dependency. In jar's from is neither a directory nor a jar file, that's why processing fails. To solve it you need to exclude the *.pom before collecting, so:
from {
configurations
.compile
.findAll { !it.name.endsWith('pom') }
.collect { it.isDirectory() ? it : zipTree(it) }
}
Next time you can use gradle shadow plugin or similar - a plugin that builds uber jar, since it probably handles it correctly.
I was trying to create a small webserver using spark and kotlin.
But I am stuck at the step where I should be able to create a jar of the project and run it from the command line. But I get the following error on running java -jar pissarra-core-all-1.0-SNAPSHOT.jar
Error: Could not find or load main class co.pissarra.Mainkt
I tried using intellij idea's artifact creation without success, and moved on to creating jar using build.gradle. Following is the code for the same
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Version': version,
'Main-Class': 'co.pissarra.Mainkt'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
After running ./gradlew clean followed by ./gradlew fatJar, I am able to create jar file in build/libs/ directory. This jar also gives the same error.
On extracting the jar file, I am able to see the directory structure co/pissarra/ with the Mainkt.class file inside it. Also it has the META-INF directory with MANIFEST.MF file whose content are as follows
Manifest-Version: 1.0
Implementation-Version: 1.0-SNAPSHOT
Main-Class: co.pissarra.Mainkt
Since stackoverflow does not allows to upload files, you can find the jar file here. You can also build the jar file from the github project here.
Check your MANIFEST file. The class name should be "MainKt" and not "Mainkt"
I am new to Gradle and to shadow jar (Gradle version of Maven's Shade plugin). I am building a fat jar, in which I want to merge service files (that's why I am using shadow jar in the first place).
According to the documentation shadowJar task inherits from gradle Jar task. So, one would assume that it will work exactly as a jar task.
Here is the snippet of the jar task:
jar {
zip64 true
from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
}
As a result, it produces a fat jar with all the dependencies exploded, what is anticipated. When I change task name from jar to shadowJar, like below:
shadowJar {
zip64 true
from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
}
I get a jar file that contains only files from the current project, dependencies are excluded. Why is this happening?
You don't need to define the from... part. The plugin will include all dependencies automatically.
I would like to output a jar which internally contains my dependency jar but overrides a particular file in the dependency jar with my own. I am using gradle build. Can someone help me with this? The task which build jar for me:
jar {
archiveName "JarName-${version}.jar"
dependsOn configurations.runtime
from {
(configurations.runtime - configurations.provided).collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
Well I actually stopped duplicating the file by including the following strategy:
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
It seems like my changes becomes a part of the output jar before they are being overridden by the file in my dependency jar. So, restricting duplicacy helped in resolving the issue.