How to change name of jar created using shadowjar - gradle

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")
}
}

Related

Gradle DSL method not found: 'destination()' after update to Gradle 5.2.1

After updating to Gradle 5.2.1 my build is failing with this error:
Gradle DSL method not found: 'destination()'
I figured out that this error has something todo with my analysis.gradle
My analysis.gradle looks like that
apply plugin: 'checkstyle'
apply plugin: 'pmd'
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.7.201606060606"
}
check.dependsOn 'checkstyle', 'pmd', 'lint'
task checkstyle(type: Checkstyle) {
println "----- checkstyle -----"
configFile file(projectDir.getAbsolutePath() + '/analysis/checkstyle-ruleset.xml')
source 'src'
source '../domain/src'
source '../util/src'
include '**/*.java'
exclude '**/gen/**'
exclude '**/java-gen/**'
exclude '**/androidTest/**'
exclude '**/test/**'
ignoreFailures = true
classpath = files()
reports {
xml {
destination buildDir.absolutePath + "/outputs/reports/checkstyle_report.xml"
}
}
}
I think I have to replace the destination flag but I have no idea how to replace it.
Before Gradle 5.0 the method setDestination(Object file) was already deprecated, see here : setDestination(Object file)
In Gradle 5.x this method has been removed, you must now use setDestination(File file) which takes a File parameter (see setDestination(File file) )
So you need to change your code into:
reports {
xml {
destination file("$buildDir/outputs/reports/checkstyle_report.xml")
}
}
All adjustment was done in my quality.gradle. Check config folder for quality.gradle file and change all usage of
destination "$reportsDir/pmd/pmd.xml"
to
destination file("$reportsDir/pmd/pmd.html")

How to uberjar SPECIFIC dependencies?

The typical answer for uberjar is the following code:
build.gradle for project
manifest {
attributes("Main-Class": "main.Main" )
}
jar{
//<editor-fold defaultstate="collapsed" desc="uberjar">
//uberjar start
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
//uberjar end
//</editor-fold>
}
From what I observe, this works by putting in all the jars in maven local.
How do I make it uberjar ONLY the dependencies, or only the dependencies that I choose?
You can use include or exclude configuration(or both if it suits your needs) to specify which packages you want in your uberjar.
Example:
task customJar(type: Jar) {
.....
exclude('com/somePack/excludePack1/**')
exclude('com/somePack/excludePack2/**')
include('com/somePack/**')
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
// this example will include only packages with com/somePack/
// since exclude configuration specifies com/somePack/excludePack1/ and
// com/somePack/excludePack2/ packages to be excluded.
// any other packages under com/somePack/ will be included in the jar.
If you use include it will only contain the packages that matches include definitions.
If you use exclude, it wont containt any packages that mathces exlude defitions.
You can choose to exclude your own source codes like this. I suggest creating a custom jar task and executing this task for this kind of things. (Ex: gradle yourJarTaskName)

Is it possible to use #Grab inside a Gradle build.gradle?

Is it possible to use #Grab inside a Gradle build.gradle file?
My understanding is that Gradle build scripts are written in Groovy and Grape/#Grab is built into Groovy.
But if i attempt to add a #Grab annotation into a build.gradle file it doesn't work.
e.g. adding:
#Grab(group='org.springframework', module='spring-orm', version='3.2.5.RELEASE')
import org.springframework.jdbc.core.JdbcTemplate
it gives me the error
org.gradle.groovy.scripts.ScriptCompilationException: Could not compile build file.
Is this possible?
According to this post, the answer is no, we can't use #Grab.
However, there is an equivalent way to declare a dependency and add it to the classpath, as shown below (usage for the example: gradle go):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath group: 'org.springframework', name: 'spring-orm', version: '3.2.5.RELEASE'
}
}
import org.springframework.jdbc.core.JdbcTemplate
task go() {
doLast {
println 'TRACER : ' + JdbcTemplate.class.getSimpleName()
}
}

Exclude Compiled Source From Custom Jar with Gradle

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

How to add a Jar task to a configuration in a dependencies block?

Consider the following build.gradle script:
apply plugin: 'java'
sourceSets {
common
admin
importer
}
dependencies {
commonCompile 'org.hibernate:hibernate:3.5.4-Final'
commonCompile 'org.springframework:spring-hibernate3:2.0.8'
adminCompile // How to add dependency on commonJar task here?
adminCompile ...
importerCompile // How to add dependency on commonJar task here?
importerCompile ...
}
task commonJar(type: Jar) {
from sourceSets.common.output
}
task adminWar(type: War) {
classpath sourceSets.admin.output
classpath configurations.adminCompile
...
}
task importerWar(type: War) {
classpath sourceSets.importer.output
classpath configurations.importerCompile
...
}
I want to add a dependency to to adminWar and importerWar, such that commonJar will generate a Jar if it doesn't exist, and adminWar and importerWar will contain that .jar along with the .jar's dependencies on their classpath. I can not for the life of me figure out how to do this, but feel like it is something simple.
Thank you.

Resources