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

     2023-02-16     79

关键词:

【中文标题】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'

【讨论】:

Spring-boot Gradle shadow jar 插件 |缺少 EmbeddedServletContainerFactory

】Spring-bootGradleshadowjar插件|缺少EmbeddedServletContainerFactory【英文标题】:Spring-bootGradleshadowjarplugin|missingEmbeddedServletContainerFactory【发布时间】:2014-08-3001:03:55【问题描述】:我在SpringBoot中设置了一个简单的Web项目,只有compile("... 查看详情

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

】Gradle和Shadow插件-在shadowJar任务之后将附加文件(.jar文件)复制到最终jar【英文标题】:GradleandShadowplugin-copyadditionalfiles(.jarfiles)tofinaljaraftershadowJartask【发布时间】:2019-03-0220:16:24【问题描述】:问题描述:我正在使用GradleShade... 查看详情

wordpress 中的 Razorpay 插件不起作用

】wordpress中的Razorpay插件不起作用【英文标题】:Razorpayplugininwordpressdoesnotwork【发布时间】:2018-12-2714:28:48【问题描述】:我正在运行wordpress4.9.6并希望将Razorpay支付网关集成到我的网站。我按照https://github.com/razorpay/razorpay-quick-pa... 查看详情

IntelliJ Spring Gradle 项目注释不起作用

...9【问题描述】:所以我试图在IntelliJ中使用项目配置设置中的Spring选项启动一个新的Web项目,但是当我最终获得项目设置时,似乎我制作的任何新控制器都被忽略了,即使我注释了他们。@RequestMapping似乎永 查看详情

插件管理页面中的 Ajax 表单不起作用

】插件管理页面中的Ajax表单不起作用【英文标题】:Ajaxforminpluginadminpageisnotworking【发布时间】:2015-01-0503:03:07【问题描述】:我正在本地主机上制作一个wordpress插件。我在插件的custum管理面板中添加了一个ajax表单。但表格不起... 查看详情

javascript中的Cordova Admob免费插件-插件不起作用

】javascript中的CordovaAdmob免费插件-插件不起作用【英文标题】:CordovaAdmobfreeplugininjavascript-Pluginnotworking【发布时间】:2020-12-1810:03:28【问题描述】:我对Cordova项目有疑问。我想免费使用插件admobcordova(https://github.com/ratson/cordova-plug... 查看详情

Gradle:由 gradle bootJar 任务创建的 jar 不起作用

】Gradle:由gradlebootJar任务创建的jar不起作用【英文标题】:Gradle:createdjarbygradlebootJartaskisnotworking【发布时间】:2019-12-1303:41:16【问题描述】:由gradle任务bootJar创建的jar不起作用。获取javax.xml.bind.JAXBException。但是使用gradlebootRun... 查看详情

插件中的传播语法在 IE/edge 中不起作用

】插件中的传播语法在IE/edge中不起作用【英文标题】:SpreadSyntaxinPluginnotworkinginIE/edge【发布时间】:2019-12-0507:15:25【问题描述】:我的react应用程序中有一个插件(react-form-with-constraints)(从create-react-app开始并弹出)。由于这... 查看详情

在 jquery 中的 DataTables 插件中排序不起作用

】在jquery中的DataTables插件中排序不起作用【英文标题】:SortinginDataTablesplugininjqueryisnotworking【发布时间】:2016-08-3101:10:47【问题描述】:我的HTML代码:<tableid="sample"><thead><tr><th></th><thstyle="text-align:center 查看详情

Shopware 6 - 插件配置中的字段验证不起作用

】Shopware6-插件配置中的字段验证不起作用【英文标题】:Shopware6-Fieldvalidationinpluginconfigurationnotworking【发布时间】:2021-01-1020:16:34【问题描述】:我在config.xml中添加了以下代码用于插件验证,但它通过了。正在保存空白值。虽... 查看详情

Flutter Google Maps 插件中的原生动画不起作用

】FlutterGoogleMaps插件中的原生动画不起作用【英文标题】:NativeAnimationinFlutterGoogleMapsPlugindoesnotwork【发布时间】:2020-09-2806:51:03【问题描述】:我尝试使用google_maps_flutter插件在颤振应用程序中制作可动画标记(脉冲动画)。因为... 查看详情

任务窗格中的 Office 插件切换 div(加载面板)不起作用

】任务窗格中的Office插件切换div(加载面板)不起作用【英文标题】:OfficeAddintogglediv(loadingpanel)intaskpanenotworking【发布时间】:2022-01-0710:04:55【问题描述】:这是我遇到的另一个问题。我的插件工作正常,我只有三个API调用,它... 查看详情

Gradle zipAlign 任务不起作用?

】GradlezipAlign任务不起作用?【英文标题】:GradlezipAligntasknotworking?【发布时间】:2013-07-1009:04:53【问题描述】:GradlezipAlign任务似乎对我不起作用,不确定我做错了什么。我试过包括zipAlign任务,但不包括它,但它似乎没有什么... 查看详情

可包含的行为在 Cakephp 3.0 中的插件中不起作用

】可包含的行为在Cakephp3.0中的插件中不起作用【英文标题】:ContainablebehaviorisnotworkingacrossthePluginsinCakephp3.0【发布时间】:2015-12-2414:50:44【问题描述】:我正在开发cakephp3.0,我喜欢使用插件,我以插件的形式编写了我的应用程... 查看详情

Extjs 4.1 - CellEditing 插件中的侦听第二次不起作用

】Extjs4.1-CellEditing插件中的侦听第二次不起作用【英文标题】:Extjs4.1-ListerninginCellEditingpluginnotworkingatsecondtime【发布时间】:2013-07-2406:38:32【问题描述】:我定义了一个treeGrid与插件CellEditing类似Ext.define(\'MyExample\',extend:\'Ext.tree.Pa... 查看详情

Gradle 构建不起作用:任务“:MyApp:compileDebug”执行失败

】Gradle构建不起作用:任务“:MyApp:compileDebug”执行失败【英文标题】:Gradlebuildnotworking:Executionfailedfortask\':MyApp:compileDebug\'【发布时间】:2014-01-3111:42:58【问题描述】:我正在尝试将项目迁移到Gradle,但每次运行命令时都会引... 查看详情

kotlin中使用注解框架不起作用(代码片段)

...你使用的Kotlin的Module或者程序的build.gradle文件里加入kapt插件和相应的注解处理就 查看详情

在 gradle 中启用 Proguard 后 FCM 不起作用

】在gradle中启用Proguard后FCM不起作用【英文标题】:FCMdoesn\'tworkafterenablingProguardingradle【发布时间】:2019-11-0800:13:27【问题描述】:我之前连接了FCM并进行了测试。而且效果很好。但是,我已准备好发布我的应用并在build.gradle中... 查看详情