IntegrationTestPlugin.groovy 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2004-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package io.spring.gradle.convention
  17. import org.gradle.api.Plugin
  18. import org.gradle.api.Project
  19. import org.gradle.api.Task
  20. import org.gradle.api.plugins.GroovyPlugin
  21. import org.gradle.api.plugins.JavaPlugin
  22. import org.gradle.api.tasks.testing.Test
  23. import org.gradle.plugins.ide.eclipse.EclipsePlugin
  24. import org.gradle.plugins.ide.idea.IdeaPlugin
  25. import org.springframework.gradle.propdeps.PropDepsPlugin
  26. /**
  27. *
  28. * Adds support for integration tests to java projects.
  29. *
  30. * <ul>
  31. * <li>Adds integrationTestCompile and integrationTestRuntime configurations</li>
  32. * <li>A new source test folder of src/integration-test/java has been added</li>
  33. * <li>A task to run integration tests named integrationTest is added</li>
  34. * <li>If Groovy plugin is added a new source test folder src/integration-test/groovy is added</li>
  35. * </ul>
  36. *
  37. * @author Rob Winch
  38. */
  39. public class IntegrationTestPlugin implements Plugin<Project> {
  40. @Override
  41. public void apply(Project project) {
  42. project.plugins.withType(JavaPlugin.class) {
  43. applyJava(project)
  44. }
  45. }
  46. private applyJava(Project project) {
  47. if(!project.file('src/integration-test/').exists()) {
  48. // ensure we don't add if no tests to avoid adding Gretty
  49. return
  50. }
  51. project.sourceSets {
  52. integrationTest {
  53. java.srcDir project.file('src/integration-test/java')
  54. resources.srcDir project.file('src/integration-test/resources')
  55. compileClasspath = project.sourceSets.main.output + project.sourceSets.test.output + project.configurations.integrationTestCompileClasspath
  56. runtimeClasspath = output + compileClasspath + project.configurations.integrationTestRuntimeClasspath
  57. }
  58. }
  59. project.configurations {
  60. integrationTestCompile {
  61. extendsFrom testImplementation
  62. }
  63. integrationTestRuntime {
  64. extendsFrom integrationTestCompile, testRuntime, testRuntimeOnly
  65. }
  66. integrationTestCompileClasspath {
  67. extendsFrom integrationTestCompile
  68. }
  69. integrationTestRuntimeClasspath {
  70. extendsFrom integrationTestRuntime
  71. }
  72. }
  73. Task integrationTestTask = project.tasks.create("integrationTest", Test) {
  74. group = 'Verification'
  75. description = 'Runs the integration tests.'
  76. dependsOn 'jar'
  77. testClassesDirs = project.sourceSets.integrationTest.output.classesDirs
  78. classpath = project.sourceSets.integrationTest.runtimeClasspath
  79. shouldRunAfter project.tasks.test
  80. useJUnitPlatform()
  81. }
  82. project.tasks.check.dependsOn integrationTestTask
  83. project.plugins.withType(IdeaPlugin) {
  84. project.idea {
  85. module {
  86. testSourceDirs += project.file('src/integration-test/java')
  87. scopes.TEST.plus += [ project.configurations.integrationTestCompileClasspath ]
  88. }
  89. }
  90. }
  91. project.plugins.withType(GroovyPlugin) {
  92. project.sourceSets {
  93. integrationTest {
  94. groovy.srcDirs project.file('src/integration-test/groovy')
  95. }
  96. }
  97. project.plugins.withType(IdeaPlugin) {
  98. project.idea {
  99. module {
  100. testSourceDirs += project.file('src/integration-test/groovy')
  101. }
  102. }
  103. }
  104. }
  105. project.plugins.withType(PropDepsPlugin) {
  106. project.configurations {
  107. integrationTestCompile {
  108. extendsFrom optional, provided
  109. }
  110. }
  111. }
  112. project.plugins.withType(EclipsePlugin) {
  113. project.eclipse.classpath {
  114. plusConfigurations += [ project.configurations.integrationTestCompileClasspath ]
  115. }
  116. }
  117. }
  118. }