当前位置: 首页 > 知识库问答 >
问题:

gradle中的影子插件不起作用-gradle构建不构建胖jar

巫马自明
2023-03-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'
}

问题是运行gradlebuild并没有创建一个胖jar。

通过运行gradle任务查看任务依赖关系--all显示阴影Jar任务存在,但是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.

共有1个答案

查学文
2023-03-14

Gradle中的“构建”任务通常被误认为是构建项目中所有任务的任务。“构建”任务实际上由java base插件提供,包括以下任务:

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

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

`defaultTasks 'build', 'shadowJar'`

把那条线放在你的身体里。gradle将允许在没有参数的情况下调用gradle,这样build和shadowJar都可以运行。

> gradle

我不熟悉shadowJar插件,但是如果它公开了一个名为shadowJar的任务,那么在构建中使用dependsOn行是非常必要的。gradle可以简化defaultTasks行。

project.tasks.shadowJar.dependsOn build

将默认任务行简化为

defaultTasks 'shadowJar'
 类似资料:
  • 我用的是Android studio。显示此错误的所有新项目: 生成文件'/Users/messan/AndroidStudioProjects/MyApplication/Build。格拉德尔线:3 在以下任何来源中均未找到插件[id:'com.android.application',版本:'7.1.1',apply:false]: > 例外是:org。格雷德尔。应用程序编程接口。插件。Unk

  • 问题内容: 是否有任何Gradle教程或插件显示如何借助Gradle构建来构建/分发Eclipse插件? Somethig类似于Maven的Tycho项目:http ://eclipse.org/tycho/ ? 问题答案: 我不知道是否存在与Tycho相当的插件。请参阅此Gradle论坛条目以获取相关讨论。

  • 我有Android Studio 3.1.2 我有Kotlin 1.2.41-release-Studio3。1-1. ,我只使用kotlin kapt: 我的事件日志显示0个警告/错误: 这是输出: 我已经完成了所有的清理和重建工作。有一些警告,但与kapt无关。我可以先解决这些问题吗?

  • 我正在使用Gradle构建并打包一个非常简单的Spring启动应用程序(99%的静态内容)到带有嵌入式tomcat的jar中。 我尝试创建该jar,起初结果86k,没有启动,因为它缺少一些Spring Boot类。我得出结论,我制作的这个jar不包含应用程序的任何依赖项,既然我确实想要一个完全独立的jar,我应该做更多的研究。 这时,我发现建议将{configurations.compile.co

  • 我创建了SpringBootGradle多模块项目,该项目由3个模块组成:控制器、服务、存储库。主文件位于控制器模块中,名为myspringbootplication。 我可以构建这个项目(使用GradleBuild)并获得jar文件。但是在命令行中启动这个jar之后,我遇到了下一个错误: 为了修复这个错误,我添加了属性到main中的MANIFEST. MF文件,但是这个操作没有帮助。有人能帮忙吗

  • 我刚通过自制安装了Gradle。版本7.0。我运行了一个hello world任务,没有出错。然后,我按照说明安装ml-gradle(https://github.com/marklogic-community/ml-gradle)。我得到构建失败的原因是: 错误:在任务“:MLNewProject”(键入“NewProjectTask”)的配置中发现了一些问题。 > 类型'NewProjectT