Posted By

laurenceosx on 01/30/12


Tagged

hack groovy gradle


Versions (?)

Groovy Gradle hack for keeping dependsOn execute order.


 / Published in: Groovy
 

Dummy comment to make this snippet visible.

  1. //////////////////////////////////////////////////
  2. // Note: see line after _orderTasks method body
  3. // project.ext.orderTasks = this.&_orderTasks;
  4. // This means you use orderTasks in your build file code (not _orderTasks).
  5. //////////////////////////////////////////////////
  6. def synchronized _orderTasks( Object... aTasks ) {
  7. // Laurence Toenjes 2013
  8. // preserve dependency task order for Gradle
  9. // If you had a single task that depended on tasks:
  10. // dropTables, createTables and loadTables
  11. // gradle would execute the dependencies in the following alphabetical order:
  12. // createTables, dropTables and loadTables.
  13.  
  14. // One workaround is to limit each task dependency to a single task and
  15. // chain together your desired task sequence.
  16. // Another workaround is to change the name of your tasks because the
  17. // dependencies are executed in alphabetical order.
  18. // The workaround presented here is a method called orderTasks which takes
  19. // a list of tasks and dynamically creates an alphabetical sequence of new tasks
  20. // (e.g. _seq_1001_dropTables, _seq_1002_createTables, ...)
  21. // that has a single dependency on each of the tasks passed to it.
  22.  
  23. // Although this was mentioned in the Gradle 1.6 release notes as a fixed issue:
  24. // [GRADLE-427] - dependsOn doesn't respect dependency order
  25. // http://issues.gradle.org/browse/GRADLE-427
  26. // ...
  27. // Gradle 1.6 adds support for task ordering rules, which solve many of the use cases listed in this issue.
  28. // ...
  29. // We're not finished with task ordering and we haven't yet solved all the use cases here, but I've marked this issue as 'fixed'.
  30. // ...
  31. // Please note that we're very unlikely to change dependsOn to run tasks in the order listed, as requested in the title of this issue.
  32. // ...
  33.  
  34. // A single task that depends on other tasks is still executed
  35. // in alphabetical order (this is still true as of Gradle version 1.6).
  36.  
  37. // Put this near the top of your gradle build file
  38. // or a common.gradle file and apply it to each project in build).
  39.  
  40. // example: task( 'seq-alpha', dependsOn: orderTasks( 'alpha charlie bravo' ) ) << { /* your task code */ }
  41. // To view task dependency do cmd: gradle tasks --all
  42.  
  43. /*
  44. mutli project build example
  45.  
  46. Note: There is a circular dependency in this example
  47. because both projects :Main and :Alpha use tasks from each other.
  48.  
  49.  
  50.   file: ./Main build.gradle
  51.   ...
  52.   task('go', dependsOn: orderTasks( ':how', 'now', ':Alpha:go' ) ) << {}
  53.   // also contains tasks: how, now, brown and cow
  54.   ...
  55.  
  56.   file: ./Alpha build.gradle
  57.   ...
  58.   task( 'go', dependsOn: orderTasks( ':Main:brown stepOne stepTwo stepThree :Main:cow' ) ) << { }
  59.   // also contains tasks: stepOne, stepTwo and stepThree
  60.   ...
  61.  
  62.   do command line: gradlew -q :Main:go
  63.  
  64.   result:
  65.  
  66.   >gradlew -q :Main:go
  67.   !!! 0 how
  68.   !!! 1 now
  69.   !!! 2 brown <-- this task :Main:brown was triggered by :Alpha:go
  70.   @@@ stepOne
  71.   @@@ stepTwo
  72.   @@@ stepThree
  73.   !!! 3 cow <-- this task :Main:cow was triggered by :Alpha:go
  74.   >
  75.   */
  76.  
  77. // create space delimited stringified version of aTasks named _aTasks
  78. List<String> taskPaths = [] as List<String>;
  79. aTasks.collect { it }.flatten().each {
  80. if ( (it instanceof String) || (it instanceof org.codehaus.groovy.runtime.GStringImpl) ) {
  81. // taskPaths << it.trim().toString();
  82. String theString = it.trim().toString();
  83. int colonCount = theString.count(':')
  84. if (colonCount == 0 ) {
  85. theString = project.path.toString() + ':' + theString;
  86. } else if ( (colonCount == 1) && theString.startsWith(':') ) {
  87. theString = project.path.toString() + theString;
  88. }
  89.  
  90. taskPaths << theString;
  91. } else if ( it instanceof Task ) {
  92. taskPaths << it.path.trim().toString();
  93. }
  94. }
  95. String _aTasks = taskPaths.join(' ');
  96.  
  97. if ( !(project.ext.properties.containsKey("orderTasksCtr")) )
  98. project.ext.orderTasksCtr = new java.util.concurrent.atomic.AtomicInteger(1000);
  99.  
  100. def cl_createProxyTask = { aTaskName ->
  101. def ctr = project.ext.orderTasksCtr.incrementAndGet();
  102. def newTaskName = "_seq_${ctr}_${aTaskName}";
  103. def newTask = project.task( newTaskName );
  104. newTask.dependsOn << "${aTaskName}";
  105. def result = newTask;
  106. }
  107.  
  108. def SQ = "'"; def DQ = '"'; // stop jedit syntax highlighting bug with extra line
  109. """ , $SQ $DQ [ ] ( ) """.trim().split().each {
  110. if (it in _aTasks)
  111. _aTasks = aTasks.replace( it, (32 as char).toString() );
  112. }
  113.  
  114. return _aTasks.split().collect { cl_createProxyTask(it) };
  115. }
  116.  
  117. project.ext.orderTasks = this.&_orderTasks;
  118. ///////////////////////////////////////////

Report this snippet  

You need to login to post a comment.