I have a gradle project from which I want to generate two artifacts - a java jar, and a tarball of additional files.
So I added the following to my build.gradle
def serviceName = "kafka-schemas"
task packageDistribution(type: Copy) {
from "$buildDir/resources/main"
include "*.avsc"
into "$buildDir/schemas"
}
task archive(type: Tar) {
dependsOn 'packageDistribution'
compression = 'GZIP'
from("$buildDir/schemas") {
include "**/*.avsc"
}
into("${serviceName}")
}
project.tasks.findByName('build') dependsOn archive
project.tasks.findByName('build') dependsOn packageDistribution
However when I run gradle clean build it does not run my tasks.
What am I doing wrong?
Try this:
processResources.dependsOn('packageDistribution')
task packageDistribution(type: Zip) {
archiveFileName = "xxxxx.zip"
destinationDirectory = file("$buildDir/dist")
from "xxxx"
}
Related
I'm wondering how I can pass a parameter (PARAM) to a Gradle task from another Gradle task that depends on it.
For example something along these lines
task buildDist(PARAM) {
copy {
from "$projectDir/src/{PARAM}/ClientBanner.json"
into "$buildDir/${project.name}/"
}
and call this Gradle task from another one like:
task dist(type: Zip) {
from "$buildDir"
include "${project.name}/**/*"
archiveFileName = project.name + '.zip'
destinationDirectory = layout.buildDir.dir('dist')
dependsOn('buildDist(PARAM)')
}
#Shapebuster
The Gradle build script is Groovy (or Kotlin) based and can be extended using the supported syntax of that language. So, in your example, you should be able to use the PARAM like so:
task buildDist(String param1) {
copy {
from "$projectDir/src/$param1/ClientBanner.json"
into "$buildDir/${project.name}/"
}
}
and then:
task dist(type: Zip) {
from "$buildDir"
include "${project.name}/**/*"
archiveFileName = project.name + '.zip'
destinationDirectory = layout.buildDir.dir('dist')
dependsOn buildDist("$param1")
}
Before is a snippet of my build.gradle, it lists a series of tasks which should ideally be run in the following order:
createPropertiesFile
jar
obfuscate
deleteNonObfuscatedJar
deletePropertiesFile
I need createPropertiesFile to run before jar, as users are building the .jar using ./gradlew jar.
The problem I'm seeing at the moment is this error:
> Could not find method doFirst() for arguments [task ':createAuthenticationPropertiesFile']
on task ':jar' of type org.gradle.api.tasks.bundling.Jar.
I've also tried jar.dependsOn(project.tasks.createAuthenticationPropertiesFile) but I've seen that custom.properties is not included in the generated .jar when using jar.dependsOn
build.gradle:
nonObfuscatedJar = 'appNotObfuscated.jar'
task createPropertiesFile << {
def props = project.file('src/main/resources/custom.properties')
props << 'prop1=value1'
}
jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
archiveName = "$nonObfuscatedJar"
from sourceSets.main.output.classesDir
from configurations.compileOnly.asFileTree.files.collect { zipTree(it) }
include '**/*.class'
include '**/custom.properties'
manifest {
attributes 'Main-Class': 'com.bobbyrne01.App'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
task obfuscate(type: proguard.gradle.ProGuardTask) {
configuration 'proguard.txt'
injars "$libsDir/$nonObfuscatedJar"
outjars "$libsDir/app.jar"
libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
}
task deleteNonObfuscatedJar (type: Delete) {
delete "$libsDir/$nonObfuscatedJar"
}
task deletePropertiesFile (type: Delete) {
delete project.file('src/main/resources/custom.properties')
}
jar.doFirst(project.tasks.createPropertiesFile)
jar.finalizedBy(project.tasks.obfuscate)
jar.finalizedBy(project.tasks.deleteNonObfuscatedJar)
jar.finalizedBy(project.tasks.deletePropertiesFile)
As a workaround, move the properties file creation to the start of the jar task:
jar {
// Create properties file
def props = project.file('src/main/resources/custom.properties')
props << 'prop1=value1'
// Build jar
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
archiveName = "$nonObfuscatedJar"
from sourceSets.main.output.classesDir
from configurations.compileOnly.asFileTree.files.collect { zipTree(it) }
include '**/*.class'
include '**/custom.properties'
manifest {
attributes 'Main-Class': 'com.bobbyrne01.App'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
I have a multi-project gradle build where not all of the subprojects have the same plugin, but I would like to define tasks in the root build.gradle file like this:
subprojects {
task continuousBuild(dependsOn: ["clean", "check", "jacocoTestReport", "integrationTests"]
}
Not all subprojects have "jacocoTestReport" or "integrationTests" defined, but this task will fail because of that fact. How do I configure this to work, and frankly, why is the default behavior so strict?
This is what ended up working for me:
task continuousBuild(dependsOn: ['clean', 'check']) {
def uncommonTasks = ['jacocoTestReport', 'integrationTests']
dependsOn += tasks
.findAll { uncommonTasks.contains(name) }
}
And I forgot that I actually needed to run integrationTests in a doLast, which looks like this:
task continuousBuild(dependsOn: ['clean', 'check']) {
dependsOn += tasks
.findAll { 'jacocoTestReport' == name) }
if (tasks.findByName('integrationTests')) {
doLast {
integrationTests
}
}
}
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 have a gradle task to create a zip archive of a directory.
The gradle task is:
task archiveReports(type: Zip) {
from '/projects/Reports/*'
archiveName 'Reports.zip'
}
When I am running the command 'gradle archiveReports', its showing the build is successful. However, no zip file is getting created.
Am I missing something here?
I figured out a way for this:
Its working for me now.
task myZip(type: Zip) {
from 'Reports/'
include '*'
include '*/*' //to include contents of a folder present inside Reports directory
archiveName 'Reports.zip'
destinationDir(file('/dir1/dir2/dir3/'))
}
With Gradle 6.7,
task packageDistribution(type: Zip) {
archiveFileName = "my-distribution.zip"
destinationDirectory = file("$buildDir/dist")
from "$buildDir/toArchive"
}
Note : archiveName is deprected.
With Kotlin DSL
tasks.register<Zip>("packageDistribution") {
archiveFileName.set("my-distribution.zip")
destinationDirectory.set(layout.buildDirectory.dir("dist"))
from(layout.buildDirectory.dir("toArchive"))
}
With Groovy
tasks.register('packageDistribution', Zip) {
archiveFileName = "my-distribution.zip"
destinationDirectory = layout.buildDirectory.dir('dist')
from layout.buildDirectory.dir("toArchive")
}
Taken from the official docs
Just in case anyone will come here to find out how to zip your project e.g. to use it as an AWS lambda zip, here you go:
tasks {
val zipTask by register("createZip", Zip::class) {
from(processResources)
from(compileKotlin)
archiveFileName.set("app.zip")
into("lib") {
from(configurations.runtimeClasspath)
}
}
build {
dependsOn(zipTask)
}
}