How do I display log4j output for a Gradle JavaExec task?

798 Views Asked by At

I would like to use Gradle tasks to execute java commands for the salesforce dataloader so that we can have our data loading tasks in a self-contained project. I've installed the dataloader into my local maven repo and set up my gradle project as follows:

build.gradle:

apply plugin: 'groovy'

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile gradleApi()
    compile 'com.force:dataloader:42.0.0'
    compile 'org.codehaus.groovy:groovy-all:2.4.7'
}

task generateKey(type: JavaExec) {
    classpath sourceSets.main.runtimeClasspath

    main = 'com.salesforce.dataloader.security.EncryptionUtil'

    args '-g', 'seedText'
}

The Problem

When I execute gradle generateKey, the task completes successfully but none of the output from the EncryptionUtil is shown. I suspect this is because the dataloader uses a log4j logger and not printing directly to standard out? Is there some further configuration I need to do?

I've also tried a task that calls EncryptionUtil.main('-g', 'seedText') directly, but that also won't show any output unless I run the task in --info mode.

Thanks for your help! I appreciate feedback and any options I'm not thinking of. We're a gradle shop but maybe there is a better solution.

2

There are 2 best solutions below

0
David T On BEST ANSWER

Blargh. I wasn't using ./gradlew. Move along, nothing to see here.

0
Yefym Dmukh On
  1. In order to inject the log4j configuration to your javaexec task you need to use the following structure (see classpath param):

         //print you classpath fileCollection
         sourceSets.main.runtimeClasspath.each { println it}
    
         def result = javaexec {
                logger.lifecycle "executing cli command PoC"
                classpath sourceSets.main.runtimeClasspath  //inject classpath
                main = "io.github.rockitconsulting.test.rockitizer.cli.RockitizerCLI"
                jvmArgs  = ["-Dapp.home=${config.testProjectDir}"] 
                args = ["create-test","AlexOne"]
         }           
    
  2. Add your directory with log4j.xml or log4j.properties the runtime classpath via adding it to dependencies block in build.gradle as follows:

       dependencies {
       runtime files(projectDir)
       compile fileTree(dir: 'lib/ib10', include: '*.jar')
    }