Hello! Is there JavaExec task for Gradle but with ...
# random
p
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
Just use 2 tasks and have a third task depending on both?
p
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
Did you enable Gradle parallel? It's disabled by default.
p
Yes. I did. It didnt help.
l
No task depends on one another in some way?
p
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
If one depends on the other, it needs to wait for that other
p
They are not depending on each other. I was checking that they can run in any sequence by explicetly declaring dependency.
l
I understand the reverse, and its opposite, I'm lost
p
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
Are the tasks in the same project?
p
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
I'd ask in the Gradle Community Slack then
h
You can write your own task and create two worker actions.
👍 1
p
Worker actions are strict in case of parameters they can receive. I can't pass task to them.
h
Of course not, you have to pass the actual data using worker parameters.
p
In that case i need to rewrite JavaExec logic. I was thinking to find ready to use solution.
h
I don’t get it, but you can use JavaExec in workers too.
p
Could you scatch some example? I don't understand how to do this. From worker action there is no access to Project instance.
l
You can access project instance at configuration time (e.g. within a
configure { }
block) FYI
p
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
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
Thanks, Philip! Probably thats what i was searching for! I'll try it.
Thanks, guys, for help! It is working as needed now!
104 Views