【已解决】gradle打包(以及如何处置第三方依赖)

您所在的位置:网站首页 uap打包 【已解决】gradle打包(以及如何处置第三方依赖)

【已解决】gradle打包(以及如何处置第三方依赖)

2024-01-05 11:38| 来源: 网络整理| 查看: 265

文章目录 前言正文小结参考文献

前言

如题。我做了很多尝试,都有点记不住了,这里记录一个可行的方案。

正文

话不多说,直接上build.gradle的代码:

// 这里的application这个plugin是必须要有的。其隐式地使用了Java plugin和Distribution plugin // 其中关于Distribution plugin的描述:A main distribution is created that packages up the application, including code dependencies and generated start scripts. plugins { id 'java' id 'application' } // 依赖仓库 repositories { mavenLocal() mavenCentral() maven { url '/home/apr/env/mavenDownload/' } // Use JCenter for resolving dependencies. jcenter() } // 项目基本信息 version = '0.0.1' group = 'xxx' mainClassName = 'xxx.Main' // 依赖声明 dependencies { // Use JUnit test framework. testImplementation 'junit:junit:4.13' implementation fileTree(dir: "/home/apr/xxx/gumtree/dist/build/install/gumtree/lib-1", includes: ['*.jar']) // parameter parser implementation "commons-cli:commons-cli:1.4" } // 方案1:打包成jar,包含依赖,即运行:./gradlew fatJar //create a single Jar with all dependencies /* //./gradlew fatJar task fatJar(type: Jar) { manifest { attributes 'Implementation-Title': 'Fat Jar', 'Implementation-Version': version, 'Main-Class': 'Python_Patch_Parser.Main' } baseName = project.name + '-jar-with-dependencies' from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } with jar } */ // 方案2:打包成Jar,包含依赖,即运行:./gradlew jar -PallInOne jar { manifestContentCharset 'utf-8' metadataCharset 'utf-8' manifest { attributes "Main-Class": mainClassName } if (project.hasProperty("allInOne")) { // ./gradlew jar -PallInOne baseName = project.name + '-jar-with-dependencies' from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } } }

如上,对应的代码我都解释了一下。

其中对应2种方案,但是呢,我发现都没有达到预期效果。方案1和方案2,都没办法打出像在maven项目下面的那种jar-with-dependencies(即包含第三方依赖)的jar包。

那怎么办呢?

由于我添加了application plugin,只需要运行./gradlew install,就可以得到如图所示的output:

在这里插入图片描述

其中: build/install/core/lib/文件夹下对应这个: 在这里插入图片描述

build/scripts/下有可执行脚本core,直接就告诉你依赖是什么了:

在这里插入图片描述 执行的指令也告诉你了: 在这里插入图片描述

所以我感觉,jar里面没有第三方依赖也可以接受了。

以后直接参考这个core脚本里面的指令就可以运行jar包了。还行。

小结

以上。以后有更好的方案再更新。

参考文献 Gradle打可运行Jar包(包含依赖第三方库中的类)gradle 将依赖打入Jar包的方法Gradle – Create a Jar file with dependencies

+how to copy the dependencies libraries JARs in gradle

The Application PluginUsing Gradle Plugins Jar build for Gradle Hello World project can not be run #18Configuration compile and runtime for gradleHow to include dependencies in jar?


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3