Override file in Gradle build dependency jar - gradle

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.

Related

gradle: exclude a dependency in making fatJar

There are lots of repeated answers on how to exclude an individual file from a fatJar. Typically, the file is excluded are in META-INF and they are excluded either because of a filename conflict, or because it is a signature copied from a dependency libarar Jar file which isn't valid for the newly created Jar file.
Example for maven:
How can I tell which signed jar is causing maven-shade-plugin to fail?
Example for gradle:
Removing Jar Signatures in Gradle Build
These solutions, however, only removed the offending file individually.
How can we make a fatJar with a specific dependency library (not individual files in that library) excluded?
For example, in question 36226033, it's easy to exclude the signature copied over from BouncyCastle, but is there a way to exclude the dependency library bcprov-jdk15on-*.jar entirely, so that the user must have the library available in order to execute the generated fat Jar?
This is proven not working:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.alphawallet.scripttool.Main'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
exclude('**/bcprov-jdk15on-1.62.jar')
with jar
}
With exclude('**/bcprov-jdk15on-1.62.jar'), the content of that jar file is still copied over to the fat jar generated.
Thanks. The motivation is to ship my Java application to systems that provides their own security library BouncyCastle (e.g. Debian Linux), instead of embeding an unsigned copy of that security library.
I don't know how excludes and includes works, but I would expect that these configurations work on a class file level, because that what the jar is working on.
It's not working on jars.
I would go for this solution:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': 'com.alphawallet.scripttool.Main'
}
baseName = project.name + '-all'
configurations.compile.findAll { file ->
// file is of type java.io.File
// when true, jar file is unziped and added
file.name != "bcprov-jdk15on-1.62.jar"
}.sort { it.name }
.collect { file ->
logger.info("Including file: ${file.name}")
file.isDirectory() ? file : zipTree(file)
}
with jar
}

Why won't gradle jar build a jar that gradle build does?

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.

Shadow Jar doesn't include dependencies into fat jar

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.

gradle to bundle nested jar dependencies into module output jar

How to make gradle to included some dependencies into module resulted jar as jar? i.e. to make jar with nested jar's e.g. in lib folder ?
This is not Android project, and this should be done for many modules in multi-module project, so fatJar, uberJar, shadowJar alike solutions seem not to fit.
You just need to add an additional from directive to include dependencies in your jar:
task jarJar(type: Jar) {
baseName = project.name + '-jarjar'
from { configurations.compile }
with jar
}

Gradle Multiproject jar doesnt include dependent classes

This might be a silly question to ask but i m having some serious issues understanding gradle compile
-Lets say i have i have project structure like this
proj-- pro1--src
--build.gradle
-- pro2--src
--build.gradle
--build.gradle
--settings.gradle
and pro2 has 2 dependencies say log4j and pro1
now when i try to run gradle jar task it generates a build directory containing the jar "pro2.jar"
but it doesn't include the dependent classes of pro1
but in maven this works fine
please help me out
Thanks
I found the solution for above.
Turns out i was wrong. It did the same thing in maven also.
To include all the dependecies in maven an additional plugin is used .
"maven-dependency-plugin"
And in case of gradle we need to create a fatJar. Or for more customization we can create a ShadowJar
fatJar
jar{
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

Resources