Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid Gradle build configuration when Spring Cloud Contract is included #938

Closed
shanman190 opened this issue Jun 11, 2022 · 11 comments
Closed
Assignees

Comments

@shanman190
Copy link

As reported in spring-cloud/spring-cloud-contract gh-1795, the Gradle configuration as created by Initializer is invalid with respect to configuring the contracts extension. Presently, SpringCloudContractGradleBuildCustomizer.java#L75-L83, adds the configuration to a task with the name contracts which does not exist. This then results in build failures with the default project configuration directly provided by Initializer.

Incorrect:

tasks.named("contracts") {
    testFramework = org.springframework.cloud.contract.verifier.config.TestFramework.JUNIT5
}

Correct:

contracts {
    testFramework = org.springframework.cloud.contract.verifier.config.TestFramework.JUNIT5
}

Minimal reproducer: https://start.spring.io/#!type=gradle-project&language=java&platformVersion=2.7.0&packaging=jar&jvmVersion=17&groupId=com.example&artifactId=demo&name=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.demo&dependencies=cloud-contract-verifier

@wilkinsona
Copy link
Contributor

wilkinsona commented Jun 14, 2022

From the linked issue contracts is an extension, not a task. I suspect that this was broken by spring-io/initializr#1292 which means that abusing the task functionality to configure an extension no longer works.

@wilkinsona wilkinsona added type: bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 14, 2022
@shanman190
Copy link
Author

Yeah, that's probably the case.

I would think adding an GradleBuild.extensions() would probably be advantageous as extensions are normally eagerly configured and in terms of Kotlin DSL could include an import and using the configure block for type assistance.

@wilkinsona
Copy link
Contributor

wilkinsona commented Jun 20, 2022

spring-io/initializr#1079 touches on the need for this.

@bwgjoseph
Copy link

@wilkinsona does your fix in #1079 resolve this?

@snicoll
Copy link
Contributor

snicoll commented Nov 18, 2022

@bwgjoseph It doesn't but we should be able to generate the right bits now. I'll try to spare some cycles to look at it.

@snicoll snicoll self-assigned this Nov 18, 2022
@snicoll
Copy link
Contributor

snicoll commented Nov 18, 2022

So I am now generating this locally:

plugins {
  id 'java'
  id 'org.springframework.boot' version '2.7.5'
  id 'io.spring.dependency-management' version '1.0.15.RELEASE'
  id 'org.springframework.cloud.contract' version '3.1.5'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
  mavenCentral()
}

ext {
  set('springCloudVersion', "2021.0.5")
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  testImplementation 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
}

dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}

tasks.named('contractTest') {
  useJUnitPlatform()
}

tasks.named('test') {
  useJUnitPlatform()
}

contracts {
  testFramework = org.springframework.cloud.contract.verifier.config.TestFramework.JUNIT5
}

Which fails with

Spring Cloud Contract Verifier Plugin: Falling back to legacy contracts directory in 'test' source set. Please switch to 'contractTest' source set as this will be removed in a future release.

> Task :copyContracts FAILED
Spring Cloud Contract Verifier Plugin: Falling back to legacy contracts directory in 'test' source set. Please switch to 'contractTest' source set as this will be removed in a future release.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':copyContracts'.
> Contracts could not be found: [/Users/snicoll/Downloads/demo-cv2/src/test/resources/contracts]
  Please make sure that the contracts were defined, or set the [failOnNoContracts] flag to [false]

A Maven-based project doesn't fail the same way so I am not keen to proceed. If anyone can help crafting the correct build definition, that would be helpful. cc @marcingrzejszczak

@marcingrzejszczak
Copy link
Contributor

just write

plugins {
  id 'java'
  id 'org.springframework.boot' version '2.7.5'
  id 'io.spring.dependency-management' version '1.0.15.RELEASE'
  id 'org.springframework.cloud.contract' version '3.1.5'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
  mavenCentral()
}

ext {
  set('springCloudVersion', "2021.0.5")
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  testImplementation 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
}

dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}

tasks.named('contractTest') {
  useJUnitPlatform()
}

tasks.named('test') {
  useJUnitPlatform()
}

contracts {
  testFramework = org.springframework.cloud.contract.verifier.config.TestFramework.JUNIT5
  failOnNoContracts = false // THIS WAS THE MISSING PIECE
}

@snicoll
Copy link
Contributor

snicoll commented Nov 18, 2022

// THIS WAS THE MISSING PIECE

I don't think that's right. I don't have to do that with Maven. Should we ignore the fact there isn't a contract?

@snicoll
Copy link
Contributor

snicoll commented Nov 18, 2022

With the help of Marcin, creating a src/contractTest/resources/contracts structure makes that warning go away.

@bwgjoseph
Copy link

Thanks @snicoll. I just went to try at start.spring.io but it seems that it does not generate anything under contracts task now.

plugins {
	id 'java'
	id 'org.springframework.boot' version '2.7.5'
	id 'io.spring.dependency-management' version '1.0.15.RELEASE'
	id 'org.springframework.cloud.contract' version '3.1.5'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
	mavenCentral()
}

ext {
	set('springCloudVersion', "2021.0.5")
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

tasks.named('contractTest') {
	useJUnitPlatform()
}

tasks.named('test') {
	useJUnitPlatform()
}

contracts {
}

Does that mean that testFramework = org.springframework.cloud.contract.verifier.config.TestFramework.JUNIT5 isn't required as well, and JUNIT5 is the default?

I saw that in the docs that JUNIT4 is the default? https://docs.spring.io/spring-cloud-contract/docs/current/reference/html/project-features.html#features-messaging-test-generation

Having the input or outputMessage sections in your DSL results in creation of tests on the publisher’s side. By default, JUnit 4 tests are created. However, there is also a possibility to create JUnit 5, TestNG, or Spock tests.

I couldn't find any other mention of testFramework configuration in the docs.


I also went to download new project generated by start.spring and wasn't able to build the project

 ./gradlew clean build
Downloading https://services.gradle.org/distributions/gradle-7.5.1-bin.zip
...........10%............20%...........30%............40%...........50%............60%...........70%............80%...........90%............100%

Welcome to Gradle 7.5.1!

Here are the highlights of this release:
 - Support for Java 18
 - Support for building with Groovy 4
 - Much more responsive continuous builds
 - Improved diagnostics for dependency resolution

For more details see https://docs.gradle.org/7.5.1/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)
> Task :copyContracts FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':copyContracts'.
> Contracts could not be found: [Z:\Downloads\Chrome\verifier\src\contractTest\resources\contracts]
  Please make sure that the contracts were defined, or set the [failOnNoContracts] flag to [false]

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2m 51s

@snicoll
Copy link
Contributor

snicoll commented Nov 19, 2022

Yep, junit5 is the default. The error is to be expected. You need a contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants