Gradle get task with - in name

So I am fighting the very stupid cpp-application plugin for gradle.
It defines tasks that have dashes in the name, such as assembleReleaseX86-64
Thus breaking commands like:
assembleReleaseX86-64.dependsOn setupJdk64
Is there any way I can reference this task? I have tried sourounding it in single and double quotes, but then gradle thinks I am referencing a string… (which makes sense).

Any ideas?

1 Like

Without seeing your build.gradle, I can only speculate.

It seems likely you defined something (a Variant?) named 'X86-64' (or maybe 'ReleaseX86-64') that the plugin is using to construct the task name. If so, then remove the hyphen from that name.

2 Likes

The plugin defines the names.

plugins {
    id 'cpp-application'
    //
    id "de.undercouch.download" version "4.0.4"
}

application {
    File jdk = new File(buildDir, 'jdk')
    println new File(jdk, "include").getPath()


    //cpp
    baseName = "OutsideClient"

    targetMachines = [
            machines.windows.x86, machines.windows.x86_64,
    ]


    binaries.configureEach(CppExecutable) {
        // Define a preprocessor macro for every binary
        compileTask.get().macros.put("NDEBUG", null)

        // Define a compiler options
        compileTask.get().compilerArgs.add '-W3'

        // Define toolchain-specific compiler options
        if (toolChain in VisualCpp) {
            //==== Compiler
            compileTask.get().compilerArgs.add('/Zi')
            compileTask.get().compilerArgs.add('/D _CRT_SECURE_NO_WARNINGS')
            compileTask.get().compilerArgs.add('-I' + new File(jdk, "include").getPath())
            compileTask.get().compilerArgs.add('-I' + new File(jdk, "include/win32").getPath())

            //==== Linker
            linkTask.get().linkerArgs.add(new File(jdk, "lib/jvm.lib").getPath())

            //Link DLL on demand in application. Allows launcher to select where the jre is at run time
            linkTask.get().linkerArgs.add('/DELAYLOAD:jvm.dll')
            linkTask.get().linkerArgs.add('Delayimp.lib')

            //Hide console window
            linkTask.get().linkerArgs.add('/SUBSYSTEM:windows')
            linkTask.get().linkerArgs.add('/ENTRY:mainCRTStartup')
        }
    }
}

task setupJdk64() {
    doLast {
        File jvmZip = new File(buildDir, 'jdk64.zip');
        download {
            src 'https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.6%2B10/OpenJDK11U-jdk_x64_windows_hotspot_11.0.6_10.zip'
            dest jvmZip
            overwrite false
        }
        File jdkDir = new File(buildDir, 'jdk');
        if (jdkDir.exists()) {
            jdkDir.deleteDir()
        }
        copy {
            from zipTree(jvmZip)
            into jdkDir
        }
        File extracted = jdkDir.listFiles()[0] //Get the contents of the jvm folder that was created.
        copy {
            from extracted
            into jdkDir
        }
        extracted.deleteDir()
        jvmZip.delete()
    }
}

task setupJdk32() {
    doLast {
        File jvmZip = new File(buildDir, 'jdk32.zip');
        download {
            src 'https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.6%2B10/OpenJDK11U-jdk_x86-32_windows_hotspot_11.0.6_10.zip'
            dest jvmZip
            overwrite false
        }
        File jdkDir = new File(buildDir, 'jdk');
        if (jdkDir.exists()) {
            jdkDir.deleteDir()
        }
        copy {
            from zipTree(jvmZip)
            into jdkDir
        }
        File extracted = jdkDir.listFiles()[0] //Get the contents of the jvm folder that was created.
        copy {
            from extracted
            into jdkDir
        }
        extracted.deleteDir()
        jvmZip.delete()
    }
}

Right now I am just doing running:
gradlew.bat setupJdk64 assembleReleaseX86-64

or
gradlew.bat setupJdk32 assembleReleaseX86

depending on which release I want, but I would like it to auto run the correct setupJdk depending on which assembleRelease I run.

1 Like

Is that a task? Probably you need to refer to the task directly like task('assembleReleaseX86-64`) instead of the autowired strings.

…but admittedly it’s been a while since I had to deal with such things.

1 Like

Hello @pspeed,
I believe it is a task, the cpp-application documentation is not very clear on how it works, but they call it a build variant: https://docs.gradle.org/current/userguide/cpp_application_plugin.html#sec:cpp_application_build_variants

I attempted to access it with task('assembleReleaseX86-64)` as you recommended, but it seems to think that it means to create a new task.

I added this to the end of the gradle file:

tasks.forEach { t -> println t }
task('assembleReleaseX86-64').dependsOn(setupJdk64)
> Configure project :
C:\Users\tlfal_000\Desktop\outside\OutsideWindows\build\jdk\include
task ':assemble'
task ':build'
task ':buildEnvironment'
task ':check'
task ':clean'
task ':components'
task ':dependencies'
task ':dependencyInsight'
task ':dependentComponents'
task ':help'
task ':init'
task ':model'
task ':projects'
task ':properties'
task ':setupJdk32'
task ':setupJdk64'
task ':tasks'
task ':wrapper'

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'outside-windows'.
> Cannot add task 'assembleReleaseX86-64' as a task with that name already exists.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 17s

What is interesting to me is that the tasks do not show up. Compared to how the project looks in IDEA:

I’m not sure how these build variant tasks work, but I get the feeling that they are not like a normal task, yet I can run them from command line like they are.

Have you tried abbreviation?

https://docs.gradle.org/current/userguide/command_line_interface.html#task_name_abbreviation

Edit: Nevermind, I see you say they work from command line.

What is your gradle version?

I am using 5.2.1, but I think I could probably use any version, that was that IDEA auto-configured the wrapper for.

I am unsure if this would help but for the example linked to that plugin they require 5.5.1 +

https://guides.gradle.org/building-cpp-applications/

1 Like

I tried different version. I just bumped it to 5.6.1 no changes in the behavior of the plugin. It still creates the variant tasks with dashes in the name, making them impossible to access as far as I can tell.

https://docs.gradle.org/2.2/release-notes.html#publishing-plugins-and-native-language-support-plugins-changes

Does this help?

Edit:
I tried it with your code and it didn’t work.

I also ran across a comment in a thread somewhere that said when they do things with hyphens in a task they are trying to force you into the command line use only.

Maybe try asking on their forum about it?