在Java 8的gradle中的jar清单文件中添加Classpath [英] Add Classpath in Manifest file of jar in gradle in java 8

查看:630
本文介绍了在Java 8的gradle中的jar清单文件中添加Classpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to add the data in MANIFEST.MF of jar file in below format I tried the below method but not able to achieve it. i need it in below way. I'm using gradle 2.3 and 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

My 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中得到了这段文字

i've got this text in 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文件将运行。

any ideas? How to properly write down a list of libraries in manifest? If I remove extra spaces with newline, the jar file is runs.

推荐答案

所有这些都与要求有关。根据清单文件中的

It's all about the requirements. According to this, in the manifest file:


以UTF8编码的形式,任何行都不能超过72个字节(不是字符)。如果某个值会使初始行长于该行,则应在额外的行上继续(每行都以单个SPACE开头)。

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个字符后使用新的行分隔符,因为您有一个字符串作为集合classpath元素。但是由于:

And Gradle inserts the new line separator after every 72 characters, since you have a single string as a collection classpath elements. But since:


Class-Path:

Class-Path :

此属性的值指定扩展名或库的相对URL该应用程序或扩展程序需要的。 URL由一个或多个空格分隔。

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的单独行中,并且每行都以单个空格分隔。例如:

It's possible to make a quite a tricky solution, where you have to collect all the classpath's entries into a single variable and make the formatting of this variable so, that every element will be in a separate line with the length of 72 and every line strarts with single space. Just for example:

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

会给您这样的清单文件内容,例如:

Will give you such a manifest file conent, like:

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

根据文档,此文件必须有效。

According to documentation it has to be valid.

这篇关于在Java 8的gradle中的jar清单文件中添加Classpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆