I'm trying to build a JAR from a specific package of classes so I want to exclude all the other packages from this JAR. This is what I have...
task receiverJar(type: Jar) {
baseName = "receivers"
from sourceSets.main.output
include 'com/foo/receivers/**'
exclude 'com/foo/cli/**'
exclude 'com/foo/tdl/**'
with jar
}
When I execute gradle receiverJar I still get all the other packages and classes in my JAR file.
task receiverJar(type: Jar) {
enabled = true
baseName this.name + '-receivers'
from sourceSets.main.output
manifest {
attributes 'Implementation-Version': project.version
}
include 'com/foo/tdl/**'
exclude 'com/foo/cdl/**'
}
Related
I would like to create fat jar using gradle. I am using the shadowJar plugin(com.github.johnrengelman.shadow).
The problem that I am facing is that I cannot find a way to instruct the plugin to package a certain libraries with their transitive dependencies.
For example: I am using hive-jdbc which depends on org.apache.httpcomponents:httpclient:4.5.2, however the final jar packaged by shadowJar does not include httpclient
Another option is to drop the shadowJar section from the gradle.build file, but then the plugin will include everything that is part of the compile classpath and huge jar file will be created.
I am using this build file in order to package a jar for spark-submmit that includes many libraries as part of the runtime environment, so I do not need all the compile dependencies to be packaged (just the ones I am specifying explicitly and their transitive dependencies)
build.gradle:
import com.github.jengelman.gradle.plugins.shadow.transformers.AppendingTransformer
plugins {
id "com.github.johnrengelman.shadow" version "2.0.3"
}
apply from: "../Parent_2.2/build.gradle"
group 'com.ebates.bigdata'
version '1.0-SNAPSHOT'
dependencies{
compile("com.databricks:spark-avro_2.11:4.0.0"){force = true}
compile("org.apache.hadoop:hadoop-client:2.6.5"){force = true}
compile("org.apache.hadoop:hadoop-aws:2.6.5"){force = true}
compile("org.postgresql:postgresql:9.4.1207"){force = true}
compile("org.apache.hive:hive-jdbc:2.2.0"){force = true}
}
configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
shadowJar {
mergeServiceFiles()
transform(AppendingTransformer) {
resource = 'reference.conf'
shadowJar.classifier = ""
zip64 true
dependencies {
include(dependency("com.databricks:spark-avro_2.11:4.0.0"))
include(dependency("org.postgresql:postgresql:9.4.1207"))
include(dependency("org.apache.hive:hive-jdbc:2.2.0"))
}
}
}
I am trying to create a jar of jars without unzipping class files from each jar. Unfortunately shadowJar unzip's all jars and resulting jars contain directories instead of jars.
My build.gradle file is:
apply plugin: 'com.github.johnrengelman.shadow'
dependencies {
compile("ldapjdk:ldapjdk:1.0"){
transitive = false
}
compile("support:activemq-core:5.3.1"){
transitive = false
}
compile("support:concurrent:0.0"){
transitive = false
}
compile("dom4j:dom4j:${dom4j_version}"){
transitive = false
}
compile("commons-lang:commons-lang:${commons_lang_version}") {
transitive = false
}
}
task copyRuntimeLibs(type: Copy) {
from(project(':server').configurations.runtime)
include 'commons-lang2*'
include 'ldapjdk*'
include 'dom4j*'
include 'concurrent*'
}
task copyFiles(dependsOn: [copyRuntimeLibs])
task copyToLib( type: Copy ) {
into "$buildDir/lib"
from configurations.runtime
}
jar { dependsOn copyToLib }
shadowJar {
zip64 true
baseName = "service"
from("$buildDir/lib") {
include '**'
}
}
I've tried from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }. This also extracts classes, MANIFEST, etc.
Is there a way to not extract classes from each jar & instead make a jar of jars?
Thanks a lot.
You may want to see this GitHub issue.
https://github.com/johnrengelman/shadow/issues/111
It is actually not solved yet in the Shadow plugin, there some workarounds are shown there.
For a multi-project Gradle project, foo, bar and baz. I'm trying to create a task which creates a zip file with both libraries and javadoc, i.e. foo.jar, AND foo-javadoc.jar..
./build.gradle
./settings.gradle
./foo/build.gradle
./bar/build.gradle
./baz/build.gradle
settings.gradle
include ":foo"
include ":bar"
include ":baz"
Top level build
allprojects {
apply plugin: 'java'
task generateJavadoc (type : Javadoc) {
source = sourceSets.main.allJava
classpath = sourceSets.main.compileClasspath
failOnError = false
}
task javadocJar(type: Jar, dependsOn: generateJavadoc) {
classifier = 'javadoc'
from generateJavadoc.destinationDir
}
artifacts {
archives javadocJar
}
}
task buildZip(type: Zip, dependsOn: build) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
from (project(':foo').configurations.runtime) {
into 'jars'
}
from (project (':foo').configurations.archives.all) {
into 'jars'
}
}
When I invoke this with gradle clean buildZip a zip file is created, but without the any -javadoc JARs I was expecting... The JavaDoc jars are generated into the project build directories, e.g. foo/build/lib/foo-javadoc.jar I've tried multiple combinations of from project (':foo').artifacts etc.
This is possible using the following. Note javadocJar is the name of the task defined in the allprojects block
from (rootProject.allprojects.javadocJar.outputs) {
into 'javadoc'
}
I am using shadowJar plugin to build/create my fatJar . Inside my build.gradle I have this
shadowJar{
mergeServiceFiles('META-INF/spring.*')
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
exclude "META-INF/LICENSE"
}
Using gradle shadowJar creates my fat jar . However the name of the fat jar created is something sample-SNAPSHOT-ns.r100-all.jar . I want to change it to sample-SNAPSHOT-ns.r100-deploy.jar . How do u overwrite jar Name using ShadowJar.
The ShadowJar plugin provides a Jar task extension. Configure it using the archiveFileName property, such as:
shadowJar{
mergeServiceFiles('META-INF/spring.*')
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
exclude "META-INF/LICENSE"
archiveFileName = "sample-${classifier}-ns.r100-deploy.${extension}"
}
You can use placeholders like ${baseName}, ${appendix}, ${version}, ${classifier} and ${extension}.
Note that archiveName is now archiveFileName, as of ShadowJar version 4.
For people looking how to do this with the Kotlin DSL it this:
tasks.withType<ShadowJar> {
archiveFileName.set("${archiveBaseName}-${archiveVersion}-${archiveClassifier}.${archiveExtension}")
}
combining the answers of #richard and #christian-dräger
tasks.withType<ShadowJar> {
archiveFileName.set("${project.name}-${project.version}.jar")
}
this outputs the format myProject-0.0.1.jar
It seems you want to replace -all by -deploy only, just add classifier option to shadowjar:
shadowJar{
mergeServiceFiles('META-INF/spring.*')
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
exclude "META-INF/LICENSE"
archiveName = "sample-${classifier}-ns.r100-deploy.${extension}"
classifier = 'deploy'
}
in addition to Richards answer i like to do the following to configure the task by usage of the kotlin DSL:
tasks {
val shadowJar by getting(ShadowJar::class) {
archiveFileName.set("${project.name}-${project.version}-foo.jar")
}
}
I have Gradle JAR (Main) project. I have other commons-io jars (dependent) that I would like to package them with the Main JAR. I tried below, but when I decompile Main JAR, I see all the dependent JARS inside libs folder with .jar extension.
jar {
from("$projectDir") {
include 'libs/**'
}
}
What I want is the class files from all the dependent JARs into Main JAR. I am doing this because the Main JAR is going to be used by multiple projects. So that way, I do not have to include all the dependent JARs.
Thanks
As far as I know You cannot pack other jars into single jar just like that. What You need to do is to extract all the jars and pack the content into single file.
Following piece of code prepares such jar for declared dependencies
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:16.0.1'
compile 'com.google.inject:guice:3.0'
}
task fatJar(type: Jar, dependsOn: jar) {
baseName = project.name + '-fat'
deps = configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
from(deps) {
exclude 'META-INF/MANIFEST.MF'
}
}
use this.
into(libForderName){
from {
configurations.compile.collect {
it.isDirectory() ? it : it.getAbsoluteFile()
}
}
}