java 如何使用 FatJar (Gradle) 创建单个 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37807562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:49:56  来源:igfitidea点击:

How to create single jar with FatJar (Gradle)

javagradlejar

提问by Patrick

I want to generate a single Jar with multiple modules and dependencies. I installed FatJar Pluginbut I got the below error:

我想生成一个具有多个模块和依赖项的 Jar。我安装了,FatJar Plugin但出现以下错误:

enter image description here

在此处输入图片说明

My Code:

我的代码:

enter image description here

在此处输入图片说明

Trying shadowJar

尝试 shadowJar

Error:(61, 0) Could not find method shadowJar() for arguments [build_eqgfg4x39smehqcteaccdy4k6$_run_closure4@780b32c6] on project ':SDKFramework' of type org.gradle.api.Project. Open File

错误:(61, 0) 无法在 org.gradle.api.Project 类型的项目“:SDKFramework”上找到参数 [build_eqgfg4x39smehqcteaccdy4k6$_run_closure4@780b32c6] 的方法 shadowJar()。打开文件

My build.gradle (module)

我的 build.gradle(模块)

apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'com.android.library'
apply plugin: 'groovyx.grooid.groovy-android'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
        versionCode 1
        versionName "0.1.1"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
}

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.10'
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile project(':framework2')
    compile 'com.google.code.gson:gson:2.6.1'
    compile 'junit:junit:4.12'
}

shadowJar {

}

采纳答案by Patrick

Well, I found solution:

好吧,我找到了解决方案:

task createJar(type: Jar) {
    from {
        List<File> allFiles = new ArrayList<>();
        configurations.compile.collect {
            for (File f : zipTree(it).getFiles()) {
                if (f.getName().equals("classes.jar")) {
                    allFiles.addAll(zipTree(f).getAt("asFileTrees").get(0).getDir())
                }
            }
        }
        allFiles.add(new File('build/intermediates/classes/release'))
        allFiles // To return the result inside a lambda
    }
    archiveName('MySDK.jar')
}

This codegenerate a single jar with all classes.

此代码生成一个包含所有类的 jar。

回答by yonisha

Very simple solution is to use the shadow plugin.
Using the plugin is very straightforward:

非常简单的解决方案是使用shadow 插件
使用插件非常简单:

  1. Declare the plugin as the first statement in your build.grade:
  1. 将插件声明为 build.grade 中的第一条语句:

plugins {
id "com.github.johnrengelman.shadow" version "1.2.3"
}

插件 {
id "com.github.johnrengelman.shadow" 版本 "1.2.3"
}

  1. Apply either javaor groovyplugin.
  1. 应用javagroovy插件。

apply plugin: 'java'

应用插件:'java'

or

或者

apply plugin: 'groovy'

应用插件:'groovy'

  1. Refresh the project and run the newly 'shadowJar' task.
  1. 刷新项目并运行新的“shadowJar”任务。

This plugin also enable you to exclude dependencies, redirect (rename) packages' names and much more.

该插件还使您能够排除依赖项、重定向(重命名)包的名称等等。

回答by Ivan

You should add the following line to your build.gradle:

您应该将以下行添加到您的build.gradle

apply plugin: 'java'

Because, gradle don't have "Jar" task by default. You can read about that, here https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html

因为,gradle 默认没有“Jar”任务。您可以在此处阅读有关内容 https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html

If you using android, then add here:

如果您使用android,请在此处添加:

apply plugin: 'android'

or

或者

apply plugin: 'com.android.library'