提供外部依赖给可执行的JAR文件

huangapple go评论60阅读模式
英文:

Provide external dependencies to executable JAR file

问题

以下是您要翻译的内容:

我有一个简单的实用Java程序,它使用一个依赖项,而这个依赖项本身又有许多其他依赖项。我需要将这个实用程序程序打包成一个可执行的JAR文件。我使用Gradle,我的JAR任务如下:

jar {
    version = ''
    baseName = 'rpc_utility'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes 'Main-Class': 'com.example.rpcclient.MainKt'
    }
    zip64 = true

    from { (configurations.compile).collect { it.isDirectory() ? it : zipTree(it) } }
}

实际上,这可以实现目标,我得到了一个可工作的JAR文件,但它的大小达到了大约60MB。问题在于,在这个JAR文件应该运行的环境中,还有另一个“胖”JAR文件,它保证包含了这个JAR文件所需的所有依赖项。

因此,如果有一种方式可以让我的实用程序JAR文件“知道”它所需的所有依赖项都在旁边的JAR文件中,我就可以将它变成一个只有几千字节大小的小JAR文件。

那么有没有办法实现这样的目标呢?
附注:我搜索了很多内容,虽然有类似的问题,但似乎没有一个与我的问题完全相同。

英文:

I have a simple utility Java program which uses a dependency, which itself has a lot of other dependencies. I need to pack this utility program in an executable jar file. I use Gradle and my JAR task looks like this:

jar {
    version = ''
    baseName = 'rpc_utility'
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    manifest {
        attributes 'Main-Class': 'com.example.rpcclient.MainKt'
    }
    zip64=true

    from { (configurations.compile).collect { it.isDirectory() ? it : zipTree(it) } }
}

This actually does the thing, and I am getting a working JAR file, but it becomes ~60MB.
The problem is that in the environment where this jar file suppose to be run, there is another fat jar, which is guarantied to have all the dependencies that this one needs.
So if there would be a way, to "tell" my utility jar file, that all the dependencies that it needs are right there, packed in the neighbor jar file, I could make it just a tiny a few kilobytes jar file.

So is there a way to do that?
P.S. I googled a lot, and there are similar questions, but non of them seems to be the same as mine.

答案1

得分: 1

以下是您要的翻译内容:

您实际上可以在可执行的JAR包清单中定义类路径。
像这样:

清单 {
  属性(
     'Main-Class': 'com.example.rpcclient.MainKt',
     "Class-Path": "lib/all-dependencies.jar"
  )
}

然后,如果您将all-dependencies.jar文件放入名为“lib”的目录中,那么这应该可以工作。在这种情况下,您的JAR包和lib目录应该在同一级目录中。

英文:

You can actually define the classpath in your executable jar's manifest.
Like this:

manifest {
  attributes(
     'Main-Class': 'com.example.rpcclient.MainKt',
     "Class-Path": "lib/all-dependencies.jar"
  )
}

then if you put your all-dependencies.jar file into a directory named "lib" then this should work. Your jar and lib directory should be on the some directory level in this case.

huangapple
  • 本文由 发表于 2020年7月27日 19:27:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63114337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定