logo
down
shadow

How to call a Gradle subproject’s task from a Gradle test?


How to call a Gradle subproject’s task from a Gradle test?

By : Paul Hottell
Date : November 22 2020, 03:01 PM
hope this fix your issue There a multiple things not working in your example:
You should never call execute on tasks! NEVER! Tasks are called by Gradles task system automatically and calling execute may break this system. The closure ({ }) you use when creating a task is for configuration. It is not executed when the task gets executed, but when it is created. Subprojects in Gradle are created and evaluated after the root project is created and evaluated. So tasks from subprojects do not even exist when the root project gets evaluated.
code :
task myTest (type: Test) {
    dependsOn ':Subproject:myCopyTask'
}


Share : facebook icon twitter icon
Task in a Multiproject Gradle Project that Depends on Multiple Subproject Gradle Plugin Tasks

Task in a Multiproject Gradle Project that Depends on Multiple Subproject Gradle Plugin Tasks


By : user6577697
Date : March 29 2020, 07:55 AM
it should still fix some issue As far as I know, the Android plugin creates tasks very late in the game, so you might have to wrap your logic in gradle.projectsEvaluated { ... }.
Have all gradle subproject 'test' tasks depend on rootproject 'setup' task

Have all gradle subproject 'test' tasks depend on rootproject 'setup' task


By : 吳伯煒
Date : March 29 2020, 07:55 AM
To fix the issue you can do Double check your settings.gradle and if the subproject is include there
code :
root (deleteJavaCache)
  |- subprojects (test.dependsOn deleteJavaCache from the root)
test.dependsOn (":deleteJavaCache")
//in root build.gradle
subprojects {
    test.dependsOn (":deleteJavaCache")
}
test.dependsOn deleteJavaCache
How to run a Gradle subproject task from another Gradle subproject via GradleBuild task type

How to run a Gradle subproject task from another Gradle subproject via GradleBuild task type


By : Vrushali
Date : March 29 2020, 07:55 AM
help you fix your problem I have a Gradle project with the next structure: , This should call callee just before caller
code :
task caller (dependsOn: [':subprj2:callee']) {

}
task caller {
  dependsOn ':subprj2:callee'
}
caller.dependsOn ':subprj2:callee'
How can I call code from one subproject in a gradle tasks of another subproject?

How can I call code from one subproject in a gradle tasks of another subproject?


By : user2854923
Date : March 29 2020, 07:55 AM
Hope that helps You have a chicken or egg problem where all of the Gradle project classloaders are resolved before any classes are compiled. This can be resolved using a custom configuration and a Classloader
Eg:
code :
configurations {
   custom 
} 
dependencies {
   custom project(':subproject-a')
} 
task customTask {
   doLast {
      def urls = configurations.custom.files.collect { it.toURI().toURL() } 
      ClassLoader cl = new java.net.URLClassLoader(urls as URL[]) 
      Class myClass = cl.loadClass('com.foo.MyClass')

      // assuming zero args constructor 
      Object myObject = myClass.newInstance()

      // assuming method which accepts single String argument 
      java.lang.reflect.Method myMethod = myClass.getMethod('myMethodName', String.class)  
      myMethod.invoke(myObject, 'methodArg')
   } 
} 
How to reference a subproject dir from a root gradle task when calling task from subproject

How to reference a subproject dir from a root gradle task when calling task from subproject


By : user3187515
Date : March 29 2020, 07:55 AM
Any of those help You could do it slightly more controlled when you register an action to wait for the java plugin to be applied on the subproject. With this you can create the task only in subprojects who contain the desired compileJava task and configure everything from the root project.
code :
subprojects { sub ->
    //register an action which gets executed when the java plugins gets applied.
    //if the project is already configured with the java plugin
    //then this action gets executed right away.
    sub.plugins.withId("java") {
      //create the task and save it.
      def createVersionTxtResourceFile = sub.tasks.create("createVersionTxtResourceFile") {
          doLast {
              def webAppVersionFile = new File("${sub.projectDir}/src/main/resources/VERSION.txt")
              def appVersion = rootProject.full_version

              println "writing VERSION.txt to " + webAppVersionFile + ", containing " + appVersion
              webAppVersionFile.delete()
              webAppVersionFile.write(appVersion)
          }
      }
      // set the task dependency
      sub.tasks.compileJava.dependsOn createVersionTxtResourceFile
    }
}
shadow
Privacy Policy - Terms - Contact Us © voile276.org