Gradle creating two Jar Files - i only want the "fat" one

107 Views Asked by At

I've that gradle.build file:

import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocation
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
    id 'java'
    id 'com.github.johnrengelman.shadow' version '5.2.0'
}

group = 'translator'
version = '1.0'

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.jetbrains:annotations:24.0.1'
    compile group: 'org.json', name: 'json', version: '20230227'
}

compileJava {
    options.encoding = 'UTF-8'
}

task jar(overwrite: true, type: ShadowJar) {
    archiveName 'Translator.jar'
    from sourceSets.main.output
}

task build(overwrite: true, type: ShadowJar) {
    archiveName 'Translator.jar'
    from sourceSets.main.output
}

tasks.register('relocateShadowJar', ConfigureShadowRelocation) {
    target = tasks.shadowJar
}

tasks.withType(ShadowJar).configureEach {
    dependsOn relocateShadowJar
    configurations = [
            project.configurations.shadow
    ]
    from {
        project.configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
    dependencies {
        exclude 'META-INF/*.SF'
        exclude 'META-INF/*.DSA'
        exclude 'META-INF/*.RSA'
        exclude 'META-INF/*.MF'
    }
}

any ideas why this creates everytime a "Translator.jar" file and a "Translator-1.0.jar" file? I only want to have the "Translator.jar" file

I expect to only get the "Translator.jar" file in the build/libs folder

1

There are 1 best solutions below

5
tamtejszystan On

You have two tasks having the same configuration and performing the same action:

task jar(overwrite: true, type: ShadowJar) {
    archiveName 'Translator.jar'
    from sourceSets.main.output
}

task build(overwrite: true, type: ShadowJar) {
    archiveName 'Translator.jar'
    from sourceSets.main.output
}

Gradle will add a version number to the JAR file name to prevent overwriting the files. To avoid that, make these files have distinct archiveName or remove one of them as they're doing the same thing.