Gradle 创建两个 Jar 文件 - 我只想要“胖”一个

问题描述 投票:0回答:1

我有 gradle.build 文件:

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'
    }
}

有什么想法为什么每次都会创建“Translator.jar”文件和“Translator-1.0.jar”文件? 我只想拥有“Translator.jar”文件

我希望只获得 build/libs 文件夹中的“Translator.jar”文件

java gradle shadow fatjar shadowjar
1个回答
0
投票

您有两个任务具有相同的配置并执行相同的操作:

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 会在 JAR 文件名中添加版本号,以防止覆盖文件。为了避免这种情况,请使这些文件具有不同的

archiveName
或删除其中一个,因为它们正在执行相同的操作。

© www.soinside.com 2019 - 2024. All rights reserved.