Creating a Fat Jar in Gradle – 在Gradle中创建一个胖罐

最后修改: 2017年 12月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick article, we’ll cover creating a “fat jar” in Gradle.

在这篇快速文章中,我们将介绍在Gradle中创建一个 “胖子罐”。

Basically, a fat jar (also known as uber-jar) is a self-sufficient archive which contains both classes and dependencies needed to run an application.

基本上,肥大的jar(也被称为uber-jar)是一个自给自足的归档文件,它包含了运行一个应用程序所需的类和依赖性。

2. Initial Setup

2.初始设置

Let’s start with a simple build.gradle file for a Java project with two dependencies:

让我们从一个简单的build.gradle文件开始,它是一个有两个依赖关系的Java项目。

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
}

3. Using the Jar Task From the Java Plugin

3.使用Java插件的Jar任务

Let’s start with modifying the jar task from the Java Gradle plugin. By default, this task produces jars without any dependencies.

让我们从修改Java Gradle插件的jar任务开始。默认情况下,这个任务产生的jar没有任何依赖性。

We can overwrite this behavior by adding a few lines of code. We need two things to make it work:

我们可以通过添加几行代码来重写这一行为。我们需要两样东西来使其发挥作用。

  • a Main-Class attribute in the manifest file
  • Include dependencies jars

Let’s add few modifications to the Gradle task:

让我们在Gradle任务中添加一些修改。

jar {
    manifest {
        attributes "Main-Class": "com.baeldung.fatjar.Application"
    }

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

4. Creating a Separate Task

4.创建一个独立的任务

If we want to leave the original jar task as it is, we can create a separate one which will do the same job.

如果我们想让原来的jar任务保持原样,我们可以创建一个单独的任务,它将做同样的工作。

The following code will add a new task called customFatJar:

以下代码将添加一个新的任务,名为customFatJar:

task customFatJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'com.baeldung.fatjar.Application'
    }
    baseName = 'all-in-one-jar'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

5. Using Dedicated Plugins

5.使用专用的插件

We can also use existing Gradle plugins in order to build a fat jar.

我们也可以使用现有的Gradle插件,以便建立一个肥大的jar。

In this example we’ll use the Shadow plugin:

在这个例子中,我们将使用Shadow>插件。

buildscript {
    repositories {
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath "gradle.plugin.com.github.johnrengelman:shadow:7.1.2"
    }
}

apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'

Once we apply the Shadow plugin, the shadowJar task will be ready to use.

一旦我们应用Shadow插件,shadowJar任务就可以使用了。

6. Conclusion

6.结论

In this tutorial, we presented a few different ways of creating fat jars in Gradle. We overrode the default jar task, created a separated task and used the shadow plugin.

在本教程中,我们介绍了在Gradle中创建胖子罐的几种不同方法。我们覆盖了默认的jar任务,创建了一个分离的任务并使用了阴影插件。

Which approach is recommended? The answer is – it depends.

建议采用哪种方法?答案是–这取决于。

In simple projects, it’s enough to override the default jar task or create a new one. But as the project grows we highly recommend to use plugins, because they already have solved more difficult problems like conflicts with external META-INF files.

在简单的项目中,覆盖默认的jar任务或创建一个新的任务就足够了。但随着项目的发展,我们强烈建议使用插件,因为它们已经解决了更多困难的问题,如与外部META-INF文件的冲突。

As always, the full implementation of this tutorial can be found over on GitHub.

一如既往,本教程的完整实现可以在GitHub上找到over

« Previous

Writing Custom Gradle Plugins