Why is my gradle task skipped when executed during build?

Viewed 8354

I am currently migrating an application from maven to gradle (4.10.2). In maven, a tar.gz-file would be generated with configuration, dependencies and some empty directories that would then be used by the application during runtime.

In gradle i am using the distribution plugin to create the tar.gz using the distTar-Task.

To create the empty directories inside the tar.gz, i create them in the build-directory and then have them copied by distTar into the tar.gz.

I managed to finally get this by creating a task createEmptyDirectories that would just create the directories and be done with it. distTar would dependOn this task so that the directories are created before the tar.gz or so i thought.

The task createEmptyDirectories however is always skipped during build (using gradle clean build):

:createEmptyDirectories (Thread[Daemon worker Thread 7,5,main]) started.

> Task :createEmptyDirectories 
Skipping task ':createEmptyDirectories' as it has no actions.

:createEmptyDirectories (Thread[Daemon worker Thread 7,5,main]) completed. Took 0.0 secs.

When i run the task by itself using gradle createEmptyDirectories the task is executed properly:

> Configure project : 
build/mailmanager/var/rtc/command_control created[true]
build/mailmanager/var/rtc/resources created[true]
build/mailmanager/var/log created[true]

What am i doing wrong? And is there a better way to get those empty directories into the tar?

Here is my build.gradle:

    buildscript {
    dependencies {
        classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.8'
        classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.15'
    }
}

plugins {
    id 'org.asciidoctor.convert' version '1.5.3'
    id 'java'
    id 'distribution'
}


group = 'de.printcom'
version = '4.4.0'
description = 'MailManager'

sourceCompatibility = 1.8
targetCompatibility = 1.8

//mainClassName = 'de.printcom.mailmanager.StartManagerWithoutUi'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}


task('createEmptyDirectories') {
    dependsOn jar

    boolean created =  new File(project.buildDir.name + "/mailmanager/var/rtc/command_control").mkdirs()
    System.println(project.buildDir.name + "/mailmanager/var/rtc/command_control created[" + created + "]")

    created = new File(project.buildDir.name + "/mailmanager/var/rtc/resources").mkdirs()
    System.println(project.buildDir.name + "/mailmanager/var/rtc/resources created[" + created + "]")

    created = new File(project.buildDir.name + "/mailmanager/var/log").mkdirs()
    System.println(project.buildDir.name + "/mailmanager/var/log created[" + created + "]")
}

distTar {
    dependsOn check, createEmptyDirectories
    compression = Compression.GZIP
    extension = 'tar.gz'
}

distributions {
    main {
        contents {
            into("mailmanager/lib") {
                from jar
                from configurations.runtime
                fileMode = 744
            }

            from("src/main/scripts/release_preprocessing/Release_4_3_0/Preprocessing") {
                into("mailmanager/sql")
                fileMode = 750
            }
            from("src/main/scripts/release_preprocessing/Release_4_3_0/Rollback") {
                into("mailmanager/sql")
            }
            from("src/main/scripts/") {
                include("startup.sh")
                into("mailmanager/bin")
            }
            from("src/main/scripts/commands") {
                include("*.mmc")
                into("mailmanager/commands")
                fileMode = 640
            }
            from("src/main/scripts/configuration") {
                include("*.mmc")
                into("mailmanager/commands")
                fileMode = 640
            }
            from("src/main/resources/properties/deploy") {
                include("default.properties", "live.properties", "test.properties", "wallet_example.properties")
                into("mailmanager/conf")
                rename("default.properties", "default.properties.template")
                rename("live.properties", "live.properties.template")
                rename("test.properties", "test.properties.template")
                rename("wallet_example.properties", "wallet_example.properties.template")
                fileMode = 640
            }
            from("src/main/resources") {
                include("quartz.properties", "ehcache.xml")
                into("mailmanager/conf")
                fileMode = 640
            }
            from(project.buildDir.name + "/classes") {
                include("log4j-updated.xml")
                into("mailmanager/conf")
                rename("log4j-updated.xml", "log4j.xml")
                fileMode = 644
            }
            from(project.buildDir.name + "/resources/main/version.html") {
                include("version.html")
                into("mailmanager")
                fileMode = 644
            }
            from("README.txt") {
                into("mailmanager")
                fileMode = 644
            }
            from("KnownBugs.txt") {
                into("mailmanager")
                fileMode = 644
            }
            from("ReleaseNotes.txt") {
                into("mailmanager")
                fileMode = 644
            }
            into("mailmanager/docs") {
                from asciidoctor
                include("Handbuch.pdf")
                fileMode = 644
            }
            into ("var") {
                from createEmptyDirectories
                include("rtc/command_control")
                include("rtc/resources")
                include("log")
                fileMode = 644
                includeEmptyDirs=true
            }
//            from(project.buildDir.name + "/mailmanager/var") {
//                include("rtc/command_control")
//                include("rtc/resources")
//                include("log")
//                into("var")
//                fileMode = 644
//                includeEmptyDirs=true
//            }
        }
    }
}

asciidoctorj {
    version = '1.5.5'
}

asciidoctor {
    backends 'pdf'
    attributes 'build-gradle': file('build.gradle'),
            'sourcedir': 'src/docs/asciidoc',
            'source-highlighter': 'coderay',
            'imagesdir': 'images',
            'toc': 'left',
            'icons': 'font',
            'setanchors': '',
            'idprefix': '',
            'idseparator': '-',
            'docinfo1': '',
            'revnumber': project.version,
            'revdate': getDate()
}

def getDate() {
    new Date().format('d.MM.yyyy')
}

test {
    testLogging {
        events "started", "passed", "skipped", "failed", "standardOut", "standardError"
    }
}

dependencies {
    compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
    compile group: 'commons-io', name: 'commons-io', version: '2.4'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.10'
    compile group: 'org.apache.ant', name: 'ant', version: '1.9.6'
    compile group: 'org.antlr', name: 'antlr', version: '3.5.2'
    compile group: 'javax.el', name: 'javax.el-api', version: '2.2.4'
    compile group: 'org.glassfish.web', name: 'javax.el', version: '2.2.4'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.5'
    compile group: 'org.apache.commons', name: 'commons-compress', version: '1.17'
    compile group: 'org.projectlombok', name: 'lombok', version: '1.18.2'
    compile group: 'com.google.guava', name: 'guava', version: '18.0'
    compile group: 'javax.annotation', name: 'jsr250-api', version: '1.0'
    compile group: 'org.eclipse.persistence', name: 'javax.persistence', version: '2.1.0'
    compile group: 'org.springframework', name: 'spring-core', version: '4.1.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-beans', version: '4.1.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-orm', version: '4.1.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-jdbc', version: '4.1.7.RELEASE'
    compile group: 'org.springframework', name: 'spring-tx', version: '4.1.7.RELEASE'
    compile(group: 'org.springframework', name: 'spring-context', version: '4.1.7.RELEASE') {
        exclude(module: 'commons-logging')
    }
    compile group: 'org.springframework', name: 'spring-context-support', version: '4.1.7.RELEASE'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '4.3.10.Final'
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '4.3.10.Final'
    compile group: 'org.hibernate', name: 'hibernate-c3p0', version: '4.3.10.Final'
    compile group: 'org.hibernate', name: 'hibernate-ehcache', version: '4.3.10.Final'
    compile group: 'org.hibernate.common', name: 'hibernate-commons-annotations', version: '4.0.5.Final'
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.0.Final'
    compile group: 'org.hibernate', name: 'hibernate-validator', version: '4.3.2.Final'
    compile group: 'javax.validation', name: 'validation-api', version: '1.1.0.Final'
    compile group: 'net.sf.ehcache', name: 'ehcache-core', version: '2.6.11'
    compile group: 'com.mchange', name: 'c3p0', version: '0.9.5.1'
    compile group: 'com.sun.mail', name: 'javax.mail', version: '1.6.0'
    compile group: 'dom4j', name: 'dom4j', version: '1.6.1'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.12'
    compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
    compile(group: 'org.quartz-scheduler', name: 'quartz', version: '2.2.1') {
        exclude(module: 'c3p0')
    }
    compile group: 'org.quartz-scheduler', name: 'quartz-jobs', version: '2.2.1'
    compile group: 'com.cronutils', name: 'cron-utils', version: '5.0.5'
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.36'
    compile group: 'hsqldb', name: 'hsqldb', version: '1.8.0.10'
    compile group: 'com.oracle', name: 'ojdbc7', version: '12.1.0.2'
    compile group: 'com.oracle', name: 'oraclepki', version: '12.1.0.2'
    compile group: 'com.oracle', name: 'osdt_cert', version: '12.1.0.2'
    compile group: 'com.oracle', name: 'osdt_core', version: '12.1.0.2'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.12'
    compile group: 'log4j', name: 'log4j', version: '1.2.17'
    testCompile group: 'com.h2database', name: 'h2', version: '1.4.193'
    testCompile group: 'org.junit.vintage', name: 'junit-vintage-engine', version: '4.12.2'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.0'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0'
    testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.0.2'
    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.19.1'
    testCompile group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.16.0'
    testCompile group: 'net.jodah', name: 'concurrentunit', version: '0.4.2'
    testCompile group: 'org.springframework', name: 'spring-test', version: '4.1.7.RELEASE'
    testCompile group: 'org.assertj', name: 'assertj-core', version: '3.2.0'
    testCompile(group: 'org.dbunit', name: 'dbunit', version: '2.5.1') {
        exclude(module: 'junit')
    }
    testCompile group: 'com.github.springtestdbunit', name: 'spring-test-dbunit', version: '1.2.1'
    testCompile group: 'pl.pragmatists', name: 'JUnitParams', version: '1.0.5'
    testCompile group: 'com.tngtech.archunit', name: 'archunit', version: '0.8.0'
}
1 Answers

First thing: you should wrap custom stuff that you want your task to do in a doLast { ... } closure.

task createDirectories {
  doLast {
    // do stuff
  }
}

Second, to create directories you should use mkdir method: https://docs.gradle.org/current/userguide/working_with_files.html#sec:creating_directories_example which by the way will default to creating directories under the main project directory.

So your task should become:

task createEmptyDirectories {
    dependsOn jar
    doLast {
      mkdir "mailmanager/var/rtc/command_control"
      mkdir "mailmanager/var/rtc/resources"
      mkdir "mailmanager/var/log"
    }
}

Here is a working example that you can adapt to your needs:

Directory structure (very basic)

gradle-test
  src/main/java/Main.java
  build.gradle
  settings.gradle

Main.java

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello world");
  }
}

settings.gradle

rootProject.name = 'gradle-test'

build.gradle

apply plugin: 'java'
apply plugin: 'distribution'

task createEmptyDirectories {
    doLast {
        mkdir 'var/test'
    }
}

distTar.dependsOn createEmptyDirectories

distributions {
    main {
        contents {
            into('lib') {
                from jar
            }
            into('var') {
                from 'var'
            }
        }
    }
}

After running gradle distTar, build/distributions/gradle-test.tar will be produced.

gradle-test> tar tvf build/distributions/gradle-test.tar

drwxr-xr-x  0 0      0           0  1 Nov 00:51 gradle-test/
drwxr-xr-x  0 0      0           0  1 Nov 00:51 gradle-test/lib/
-rw-r--r--  0 0      0         684  1 Nov 00:44 gradle-test/lib/gradle-test.jar
drwxr-xr-x  0 0      0           0  1 Nov 00:51 gradle-test/var/
drwxr-xr-x  0 0      0           0  1 Nov 00:45 gradle-test/var/test/

As you can see, the jar file is in the lib folder and /var/ has the desired directory structure. Hope this makes it clear. It's likely not working for you now because you're using from createEmptyDirectories but that takes the output of the createEmptyDirectories task which is empty (mkdir does not add the created directories to the tasks outputs where it is used, so you have to be explicit)

Related