Подтвердить что ты не робот

Построение убержара с помощью Gradle

Я новичок Gradle. Я хочу построить uberjar (AKA fatjar), который включает в себя все транзитивные зависимости проекта. Какие строки мне нужно добавить в мой "build.gradle"?

Это то, что у меня сейчас есть: (я скопировал его откуда-то несколько дней назад, но не вспоминаю откуда.)

task uberjar(type: Jar) {
    from files(sourceSets.main.output.classesDir)

    manifest {
        attributes 'Implementation-Title': 'Foobar',
                'Implementation-Version': version,
                'Built-By': System.getProperty('user.name'),
                'Built-Date': new Date(),
                'Built-JDK': System.getProperty('java.version'),
                'Main-Class': mainClassName
    }
}
4b9b3361

Ответ 2

Я заменил task uberjar(.. следующим образом:

jar {
    from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }

    manifest {
        attributes 'Implementation-Title': 'Foobar',
                'Implementation-Version': version,
                'Built-By': System.getProperty('user.name'),
                'Built-Date': new Date(),
                'Built-JDK': System.getProperty('java.version'),
                'Main-Class': mainClassName
    }
}

Исключения необходимы, потому что в их отсутствие вы попадете в эту проблему.

Ответ 3

Я нашел этот проект очень полезным. Используя его как ссылку, моя задача Gradle uberjar будет

task uberjar(type: Jar, dependsOn: [':compileJava', ':processResources']) {
    from files(sourceSets.main.output.classesDir)
    from configurations.runtime.asFileTree.files.collect { zipTree(it) }

    manifest {
        attributes 'Main-Class': 'SomeClass'
    }
}

Ответ 4

Просто добавьте это в свой java-модуль build.gradle.

mainClassName = "my.main.Class"

jar {
  manifest { 
    attributes "Main-Class": "$mainClassName"
  }  

  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  }
}

В результате появится файл [module_name]/build/libs/[module_name].jar.