gradle 中的 shadow 插件不起作用 - gradle build 不会构建胖 jar

Posted

技术标签:

【中文标题】gradle 中的 shadow 插件不起作用 - gradle build 不会构建胖 jar【英文标题】:shadow plugin in gradle is not working - gradle build does not build a fat jar 【发布时间】:2018-05-28 16:05:14 【问题描述】:

我有以下build.gradle内容:

group 'com.example'
version '1.0-SNAPSHOT'

buildscript 
    repositories 
        mavenLocal()
        mavenCentral()
        jcenter()
        maven  url "https://plugins.gradle.org/m2/" 
    
    dependencies 
        classpath "com.github.jengelman.gradle.plugins:shadow:2.0.1"
    



apply plugin: 'scala'   
apply plugin: "com.github.johnrengelman.shadow"
sourceCompatibility = java_version


repositories 
    mavenCentral()


dependencies 
    // my dependencies:
    compile group: 'org.apache.spark', name: "spark-sql_$scala_major", version: spark_version
    testCompile group: 'junit', name: 'junit', version: '4.12'

问题是运行gradle build 不会创建一个胖罐子。

通过运行gradle tasks --all查看任务依赖可知shadowJar任务存在,但build任务不依赖:

:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
scaladoc - Generates Scaladoc for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'scalaGradleHW'.
components - Displays the components produced by root project 'scalaGradleHW'. [incubating]
dependencies - Displays all dependencies declared in root project 'scalaGradleHW'.
dependencyInsight - Displays the insight into a specific dependency in root project 'scalaGradleHW'.
dependentComponents - Displays the dependent components of components in root project 'scalaGradleHW'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'scalaGradleHW'. [incubating]
projects - Displays the sub-projects of root project 'scalaGradleHW'.
properties - Displays the properties of root project 'scalaGradleHW'.
tasks - Displays the tasks runnable from root project 'scalaGradleHW'.

Shadow tasks
------------
knows - Do you know who knows?
shadowJar - Create a combined JAR of project and runtime dependencies

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Other tasks
-----------
compileJava - Compiles main Java source.
compileScala - Compiles the main Scala source.
compileTestJava - Compiles test Java source.
compileTestScala - Compiles the test Scala source.
processResources - Processes main resources.
processTestResources - Processes test resources.

【问题讨论】:

使用application pluginapply plugin: 'application',隐式应用Distribution plugin,这会导致以下依赖链:build -> assemble -> shadowDistTar -> shadowJar。这样调用 gradle build 会生成我期待的 fatJar 非常感谢,它对我有用 【参考方案1】:

The 'build' task in Gradle is commonly mistaken 用于构建项目中所有任务的任务。 'build' 任务实际上是由 java-base 插件提供的,包括以下任务:

compileJava, processResources, classes, jar, assemble, compileTestJava, processTestResources, testClasses, test, and check

我建议使用 defaultsTasks 而不是运行“gradle build”

`defaultTasks 'build', 'shadowJar'`

将该行放入您的 build.gradle 将允许在不带参数的情况下调用 Gradle,以便 build 和 shadowJar 都运行。

> gradle

我不熟悉 shadowJar 插件,但如果它暴露了一个名为 shadowJar 的任务,在 build.gradle 中使用 dependsOn 行可以简化 defaultTasks 行。

project.tasks.shadowJar.dependsOn build

将 defaultTasks 行简化为

defaultTasks 'shadowJar'

【讨论】:

以上是关于gradle 中的 shadow 插件不起作用 - gradle build 不会构建胖 jar的主要内容,如果未能解决你的问题,请参考以下文章

Gradle 和 Shadow 插件 - 在 shadowJar 任务之后将附加文件(.jar 文件)复制到最终 jar

如何使用gradle任务运行shadow jar?

Kotlin中使用注解框架不起作用

需要访问源的Gradle自定义插件

Polymer-project:#shadow-root下的纸卡图像样式不起作用

IntelliJ Spring Gradle 项目注释不起作用