问题
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)
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(' ')
)
}
}`
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
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.
回答1:
It's all about the requirements. According to this, in the manifest file:
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).
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 :
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.
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.
来源:https://stackoverflow.com/questions/33758244/add-classpath-in-manifest-file-of-jar-in-gradle-in-java-8