https://kotlinlang.org logo
#random
Title
# random
p

PHondogo

11/19/2023, 10:45 PM
Hello! Is there JavaExec task for Gradle but with worker api support? I need to run in one project server and client as 2 parallel tasks.
l

louiscad

11/20/2023, 7:01 AM
Just use 2 tasks and have a third task depending on both?
p

PHondogo

11/20/2023, 9:13 AM
That don't work. Task1 waiting for Task2 even if they are not depend on one another. I search for solution - only with suppot of worker api by task this is possible in one project.
l

louiscad

11/20/2023, 9:13 AM
Did you enable Gradle parallel? It's disabled by default.
p

PHondogo

11/20/2023, 9:14 AM
Yes. I did. It didnt help.
l

louiscad

11/20/2023, 9:15 AM
No task depends on one another in some way?
p

PHondogo

11/20/2023, 9:17 AM
Yes. I checked that. For example make explicetly dependency for 1 to 2 and run. And next run opposite. They run as it was declared, no cyclic dependency error were triggered.
l

louiscad

11/20/2023, 9:18 AM
If one depends on the other, it needs to wait for that other
p

PHondogo

11/20/2023, 9:20 AM
They are not depending on each other. I was checking that they can run in any sequence by explicetly declaring dependency.
l

louiscad

11/20/2023, 9:21 AM
I understand the reverse, and its opposite, I'm lost
p

PHondogo

11/20/2023, 9:24 AM
Here is from Gradle docs: Most builds consist of more than one project and some of those projects are usually independent of one another. Yet Gradle will only run one task at a time by default, regardless of the project structure (this will be improved soon). By using the --parallel switch, you can force Gradle to execute tasks in parallel as long as those tasks are in different projects.
l

louiscad

11/20/2023, 9:24 AM
Are the tasks in the same project?
p

PHondogo

11/20/2023, 9:25 AM
Yes
An to make it run parrallel, tasks must support Worker api
JavaExec task doesn't
So I was trying to find solution like JavaExec but with worker api support
But with no luck so far
l

louiscad

11/20/2023, 9:26 AM
I'd ask in the Gradle Community Slack then
h

hfhbd

11/20/2023, 2:01 PM
You can write your own task and create two worker actions.
👍 1
p

PHondogo

11/20/2023, 2:17 PM
Worker actions are strict in case of parameters they can receive. I can't pass task to them.
h

hfhbd

11/20/2023, 2:18 PM
Of course not, you have to pass the actual data using worker parameters.
p

PHondogo

11/20/2023, 2:19 PM
In that case i need to rewrite JavaExec logic. I was thinking to find ready to use solution.
h

hfhbd

11/20/2023, 2:20 PM
I don’t get it, but you can use JavaExec in workers too.
p

PHondogo

11/20/2023, 2:25 PM
Could you scatch some example? I don't understand how to do this. From worker action there is no access to Project instance.
l

louiscad

11/20/2023, 2:26 PM
You can access project instance at configuration time (e.g. within a
configure { }
block) FYI
p

PHondogo

11/20/2023, 2:29 PM
Worker action is submitted by passing it's class name and specifying only serializable parameters. So Gradle create instance by itself. It is not possible access project instanse in execution phase for them.
h

hfhbd

11/20/2023, 2:29 PM
Copy code
import org.gradle.api.*
import org.gradle.api.file.*
import org.gradle.api.tasks.*
import org.gradle.api.tasks.PathSensitivity.*
import org.gradle.process.*
import org.gradle.workers.*
import javax.inject.*

@CacheableTask
abstract class GenJavaFromWSDLTask : DefaultTask() {

    @get:InputFiles
    @get:PathSensitive(RELATIVE)
    @get:SkipWhenEmpty
    @get:IgnoreEmptyDirectories
    abstract val wsdlFiles: ConfigurableFileCollection

    @get:InputFile
    @get:PathSensitive(RELATIVE)
    abstract val wsdlCustom: RegularFileProperty

    @get:OutputDirectory
    abstract val generatedJavaFolder: DirectoryProperty

    @get:Inject
    abstract val workerExecutor: WorkerExecutor

    @get:Classpath
    abstract val classpath: ConfigurableFileCollection

    init {
        generatedJavaFolder.convention(project.layout.buildDirectory.dir("generated/wsdl/"))
    }

    @TaskAction
    fun generate() {
        val workQueue = workerExecutor.noIsolation()
        for (wsdlFile in wsdlFiles) {
            workQueue.submit(WsdlGeneration::class.java) {
                this.classpath.from(this@GenJavaFromWSDLTask.classpath)
                this.wsdlFile.set(wsdlFile)
                this.wsdlCustom.set(this@GenJavaFromWSDLTask.wsdlCustom)
                this.generatedJavaFolder.set(this@GenJavaFromWSDLTask.generatedJavaFolder)
            }
        }
    }
}

interface WsdlParameters : WorkParameters {
    val classpath: ConfigurableFileCollection
    val wsdlFile: RegularFileProperty
    val wsdlCustom: RegularFileProperty
    val generatedJavaFolder: DirectoryProperty
}

abstract class WsdlGeneration : WorkAction<WsdlParameters> {
    @get:Inject
    abstract val execOperations: ExecOperations

    override fun execute() {
        execOperations.javaexec {
            classpath(parameters.classpath)
            jvmArgs("-Duser.language=en-US")

            mainClass.set("org.apache.cxf.tools.wsdlto.WSDLToJava")
            argumentProviders.add {
                listOf(
                    "-b",
                    parameters.wsdlCustom.asFile.get().absolutePath,
                    "-d",
                    parameters.generatedJavaFolder.asFile.get().absolutePath,
                    "-suppress-generated-date",
                    "-bareMethods",
                    parameters.wsdlFile.asFile.get().absolutePath,
                )
            }
        }
    }
}
Basically, it is injectable:
@get:Inject abstract val execOperations: ExecOperations
p

PHondogo

11/20/2023, 2:31 PM
Thanks, Philip! Probably thats what i was searching for! I'll try it.
Thanks, guys, for help! It is working as needed now!
7 Views