Construyendo un uberjar con Gradle


Soy un novato de Gradle. Quiero construir un uberjar (TAMBIÉN conocido como fatjar) que incluya todas las dependencias transitivas del proyecto. Qué líneas tengo que añadir a mi " build.gradle"?

Esto es lo que tengo actualmente: (Lo copié de algún lugar hace unos días, pero no recuerdo de dónde.)

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
    }
}
Author: cmcginty, 2012-06-11

4 answers

¿Has probado el ejemplo de fatjar en el libro de cocina de gradle?

Lo que estás buscando es el complemento shadow para gradle

 31
Author: tim_yates,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-07 06:04:06

He sustituido el task uberjar(.. por el siguiente:

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

Las exclusiones son necesarias porque en su ausencia se dará este problema.

 37
Author: missingfaktor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-11 20:05:21

Simplemente agregue esto a la compilación de su módulo java.gradle.

MainClassName = " my.principal.Clase "

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

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

Esto resultará en [module_name]/build/libs/[module_name].archivo jar.

 6
Author: Bao Le,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-26 04:45:02

Encontré este proyecto muy útil. Usándolo como referencia, mi tarea Gradle uberjar sería

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'
    }
}
 5
Author: boechat107,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-14 15:12:37