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

Add Classpath in Manifest file of jar in gradle in java 8

我需要在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

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

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

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 并且每一行都以 space 开头。举个例子:

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 )
    }
}

会给你这样一个清单文件内容,比如:

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

根据文档,它必须有效。