3

I am trying to build a spring web application with some API endpoints. Everytime I try to run "gradle build", getting error:

Entry META-INF/MANIFEST.MF is a duplicate but no duplicate handling strategy has been set.

Following is the output for build --scan:

3:23:01 pm: Executing 'build --scan'...

> Task :compileJava
> Task :processResources UP-TO-DATE
> Task :classes
> Task :bootJarMainClassName
> Task :bootJar FAILED
4 actionable tasks: 3 executed, 1 up-to-date

Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept these terms? [yes, no] 
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':bootJar'.
> Entry META-INF/MANIFEST.MF is a duplicate but no duplicate handling strategy has been set. Please refer to https://docs.gradle.org/7.2/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:duplicatesStrategy for details.

build.gradle:

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

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

jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes(
                'Main-Class': 'com.example.backendapp.BackendAppApplication'
        )
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

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

Solutions I tried till now:

  1. Add "duplicatesStrategy = DuplicatesStrategy.EXCLUDE" inside jar
  2. Downgrade gradle version from 7.5.1 to 7.2
DR93
  • 463
  • 6
  • 21
  • 1
    There are several options (including "DuplicatesStrategy.EXCLUDE", and "try a different version of Gradle") here: https://stackoverflow.com/q/67265308/421195, and here: https://youtrack.jetbrains.com/issue/KT-46165. Q: Do you happen to be using Android Studio (or IntelliJ)? – paulsm4 Nov 23 '22 at 23:33
  • @paulsm4 I am using IntelliJ 2022.2.3 version – DR93 Nov 23 '22 at 23:37
  • @paulsm4 All the solutions with withType are throwing error: Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'withType' for task set of type org.gradle.api.internal.tasks.DefaultTaskContainer. – DR93 Nov 23 '22 at 23:42
  • Thank you @paulsm4 for pointing to the thread. Managed to solve it using this, did a little bit of modification for Jar part though: tasks.withType(Jar) { duplicatesStrategy = DuplicatesStrategy.EXCLUDE manifest { attributes["Main-Class"] = "com.example.backendapp.BackendAppApplication" } } – DR93 Nov 23 '22 at 23:45

1 Answers1

4

This can be solved by adding this to build.gradle:

tasks.withType(Jar) {
   duplicatesStrategy = DuplicatesStrategy.EXCLUDE

   manifest {
      attributes["Main-Class"] = "com.example.backendapp.BackendAppApplication"
   }
}
DR93
  • 463
  • 6
  • 21
  • Glad you got it working. It's worth mentioning the alternate (Kotlin?) syntax of `tasks.withType() {...}` – paulsm4 Nov 24 '22 at 00:10