gpt4 book ai didi

java - 在java 8中的gradle中的jar的Manifest文件中添加Classpath

转载 作者:行者123 更新时间:2023-11-30 03:10:18 26 4
gpt4 key购买 nike

我需要以以下格式在 jar 文件的 MANIFEST.MF 中添加数据我尝试了以下方法但无法实现。我需要通过以下方式。我正在使用 gradle 2.3 和 java

java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

我的build.gradle

`apply plugin: 'java'
repositories {
mavenCentral()
}

dependencies {
compile 'org.slf4j:slf4j-api:1.7.12'
compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.4'
compile 'org.apache.logging.log4j:log4j-api:2.4'
compile 'org.apache.logging.log4j:log4j-core:2.4'
compile 'commons-cli:commons-cli:1.3.1'
compile 'commons-io:commons-io:2.4'
testCompile 'junit:junit:4.12'
}

jar {
sourceCompatibility = 1.7 //Java version compatibility to use when compiling Java source.
targetCompatibility = 1.7 //Java version to generate classes for.
compileJava.options.debugOptions.debugLevel = "source,lines,vars" // Include debug information

manifest {
attributes(
"Manifest-Version": "1.0",
"Class-Path": configurations.compile.collect { "libs/" + it.getName() }.join(' ')
)
}
}`

我在 MANIFEST.MF 中有此文本

Manifest-Version: 1.0
Class-Path: libs/slf4j-api-1.7.12.jar libs/log4j-slf4j-impl-2.4.jar li
bs/log4j-api-2.4.jar libs/log4j-core-2.4.jar libs/commons-cli-1.3.1.j
ar libs/commons-io-2.4.jar

有什么想法吗?如何在 list 中正确写下库列表?如果我用换行符删除多余的空格,则 jar 文件将运行。

最佳答案

一切都与要求有关。根据this ,在 list 文件中:

No line may be longer than 72 bytes (not characters), in its UTF8-encoded form. If a value would make the initial line longer than this, it should be continued on extra lines (each starting with a single SPACE).

Gradle 会在每 72 个字符后插入新的行分隔符,因为您有一个字符串作为集合类路径元素。但自从:

Class-Path :

The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs. URLs are separated by one or more spaces.

可以制定一个相当棘手的解决方案,您必须将所有类路径的条目收集到一个变量中,并设置该变量的格式,以便每个元素都位于长度为 72 的单独行中,并且每行都以单个空格开头。举个例子:

int i = 0;
String classpathVar = configurations.compile.collect { " libs/" + (i++==0?String.format("%0\$-50s", it.getName()):String.format("%0\$-62s", it.getName())) }.join(" ");
jar{
manifest {
attributes("Implementation-Title": "SIRIUS Workflow Executor",
"Implementation-Version": version,
"Class-Path": classpathVar )
}
}

会给你这样一个 list 文件内容,例如:

Class-Path:  libs/snmp4j-2.3.4.jar                                    
libs/jaxb
libs/datamodel.jar
libs/log4j-1.2.17.jar

根据文档,它必须有效。

关于java - 在java 8中的gradle中的jar的Manifest文件中添加Classpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33758244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com