如何在Gradle中操作OsgiManifest的结果Import-Package语句?

时间:2014-09-19 10:22:10

标签: gradle osgi manifest

我想过滤掉Import-Package指令的条目,因为它是一个太阳原生包。

目前生成的Manifest读取

Import-Package: javax.net.ssl,javax.security.auth.kerberos,org.jivesof
 tware.smack;version="[4.1,5)",org.jivesoftware.smack.initializer;vers
 ion="[4.1,5)",org.jivesoftware.smack.util.stringencoder;version="[4.1
 ,5)",sun.security.util

但是,我想删除sun.security.util,以便它变为

Import-Package: javax.net.ssl,javax.security.auth.kerberos,org.jivesof
 tware.smack;version="[4.1,5)",org.jivesoftware.smack.initializer;vers
 ion="[4.1,5)",org.jivesoftware.smack.util.stringencoder;version="[4.1
 ,5)"

我认为应该可以使用像

这样的东西
afterEvaluate { project ->
    def importPackages = ((OsgiManifest) project.jar.manifest).instructionValue('Import-Packages')
    def filteredImportPackages = importPackages.findAll { it != 'sun.security.util' }
    ((OsgiManifest) project.jar.manifest).instructionReplace('Import-Packages', filteredImportPackages)
}

但是这会导致ClassCastException:

Cannot cast object 'org.gradle.api.java.archives.internal.DefaultManifest@136bec49' with class 'org.gradle.api.java.archives.internal.DefaultManifest' to class 'org.gradle.api.plugins.osgi.OsgiManifest'

如何在gradle配置之后操作(Osgi)清单?

1 个答案:

答案 0 :(得分:4)

您应该只使用这样的bnd说明删除sun.security.util包:

jar {
    manifest { // the manifest of the default jar is of type OsgiManifest
        instruction 'Import-Package', '!sun.security.*, *'
    }
}