spring-io / initializr

A quickstart generator for Spring projects
https://start.spring.io
Apache License 2.0
3.43k stars 1.73k forks source link

test-jar dependency type is not properly handled with Gradle #1159

Open williamsuane opened 3 years ago

williamsuane commented 3 years ago

Generating new Gradle projects that contains testImplementation("org.springframework.cloud:spring-cloud-stream:test-binder@test-jar") fails with the following message when building

Could not find org.springframework.cloud:spring-cloud-stream:test-binder.
Required by:
    project :

Possible solution:
 - Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

How to replicate

Generate a new project Spring Boot 2.4, Java 11, Cloud Stream and Kafka Binder

Run ./gradlew build

The solution that worked was to replace testImplementation("org.springframework.cloud:spring-cloud-stream:test-binder@test-jar") by

testImplementation("org.springframework.cloud:spring-cloud-stream") {
        artifact {
            name = "spring-cloud-stream"
            extension = "jar"
            type ="test-jar"
            classifier = "test-binder"
        }
    }
snicoll commented 3 years ago

@williamsuane thanks for the report.

With the help of @wilkinsona, we've been reminded that Maven handles certain dependency type with "magic strings", see the documentation. To make things consistent between the two model we may need to handle these too considering that type is a shared attribute between the two models.

snicoll commented 3 years ago

So that works but, unfortunately, requesting the jar without a type fails as the dependency is not found in the bom. It is declared like this (correctly according to Maven):

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-stream</artifactId>
                <version>${spring-cloud-stream.version}</version>
                <type>test-jar</type>
                <scope>test</scope>
                <classifier>test-binder</classifier>
            </dependency>

With the following: testImplementation 'org.springframework.cloud:spring-cloud-stream:test-binder we get:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileTestJava'.
> Could not resolve all files for configuration ':testCompileClasspath'.
   > Could not find org.springframework.cloud:spring-cloud-stream:test-binder.
     Required by:
         project :
   > Could not find org.springframework.cloud:spring-cloud-stream:test-binder.
     Required by:
         project :

Ignoring the type won't fix this issue as we need to definition to match what's in the bom.

wilkinsona commented 3 years ago

I'm a bit surprised that doesn't work. We could probably fix it with a change in the dependency management plugin. It would also be interesting to know how it behaves when using Gradle's Platform support.

HackerTheMonkey commented 3 years ago

I've just came across the same issue with a project I am working on which uses SpringBoot and SpringCloud with maven.

Adding the dependency on the test binder jar in this manner:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
            <scope>test</scope>
            <classifier>test-binder</classifier>
            <type>test-jar</type>
        </dependency>

results in that maven picking up the default classifier for test-jars which is tests instead of the overridden test-binder

Failure to find org.springframework.cloud:spring-cloud-stream:jar:tests:3.1.2 in ...

Now, removing the type from the above dependency declaration makes maven complain about the missing version, but now it uses the correct classifier, i.e. test-binder

'dependencies.dependency.version' for org.springframework.cloud:spring-cloud-stream:jar:test-binder is missing

Adding a version, then fixes everything and the correct jar is downloaded from Maven Central

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
            <scope>test</scope>
            <classifier>test-binder</classifier>
            <version>3.1.2</version>
        </dependency>

And we get a BUILD SUCCESS. Here we can see that the classifier is indeed test-binder and not tests

dinvlad commented 2 years ago

Any updates on this one?

snicoll commented 2 years ago

@dinvlad this issue has all the latest update. We don't have time to work on this at the moment, I am afraid.

ashwathirao commented 1 year ago

Gradle project with spring-cloud-stream does not build, Mockito dependency fails on build due to "org.springframework.cloud:spring-cloud-starter-config" Any updates on this ?

negwu01 commented 1 year ago

I tried the solution provided by @HackerTheMonkey for spring boot 3 and spring cloud v2022.0.0 when updating a project but ran into the same issue:

org.springframework.cloud:spring-cloud-stream:jar:test-binder:4.0.0 was not found in https://repo.maven.apache.org/maven2 during a previous attempt