我正在尝试将Gradle 6.0.1 ProGuard插件创建的混淆JAR发布到Maven存储库。在过去的几天里,我对Gradle的配置,变体和工件有了很多了解,但是我似乎无法完成这项工作。我相信相关的文档就是here。
我创建了一个minimal example showing my intended setup on Github。
如果我在此项目上运行else exit 1
,则会出现以下错误。
awk
我还尝试了不推荐使用的find
插件。不幸的是,该插件忽略了自定义的ProGuard jar。
有人可以给我任何有关如何用proguard任务中模糊的Jar替换JAR任务中的原始JAR的建议吗?
编辑: 我能够从this thread得出解决方案。 Github上的最小示例包含修补版本。随意看看。我将工作的build.gradle放在下面的答案中。
答案 0 :(得分:1)
添加classifier = "proguard"
可以解决问题,尽管我不熟悉proguard来确定出版物是否正确。
artifacts {
proguard(new File(proguard.outJarFiles[0])) {
builtBy(proguard)
classifier = "proguard"
}
}
以下是为我发布的内容:
我可以看到原始的JAR是592个字节,而proguard一个是410个字节,所以我假设它可以正常工作。
此外,在调试问题时,我重构为使用建议使用的Kotlin DSL。
import proguard.gradle.ProGuardTask
plugins {
`java-library`
`maven-publish`
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("net.sf.proguard:proguard-gradle:6.2.2")
}
}
group = "org.example"
version = "1.0.0"
repositories {
jcenter()
}
configurations {
val implementation by this
val runtimeOnly by this
create("proguard") {
isCanBeConsumed = false
isCanBeResolved = false
extendsFrom(implementation, runtimeOnly)
attributes {
attribute(Attribute.of("org.gradle.usage", String::class.java), Usage.JAVA_API)
attribute(Attribute.of("org.gradle.category", String::class.java), Category.LIBRARY)
attribute(Attribute.of("org.gradle.libraryelements", String::class.java), LibraryElements.JAR)
attribute(Attribute.of("org.gradle.dependency.bundling", String::class.java), Bundling.EXTERNAL)
}
}
}
dependencies {
api("org.apache.commons:commons-math3:3.6.1")
implementation("com.google.guava:guava:23.0")
testImplementation("junit:junit:4.12")
}
val proguard by tasks.registering(ProGuardTask::class) {
description = "Optimizes and obfuscates the created distribution jar."
dependsOn(tasks.named("jar"))
verbose()
injars("$buildDir/libs/${project.name}-${project.version}.jar")
outjars("$buildDir/proguard/${project.name}-${version}.jar")
if (System.getProperty("java.version").startsWith("1.")) {
libraryjars("${System.getProperty("java.home")}/lib/rt.jar")
} else {
libraryjars(mapOf("jarfilter" to "!**.jar", "filter" to "!module-info.class"), "${System.getProperty("java.home")}/jmods/java.base.jmod")
}
libraryjars(configurations.named("runtimeClasspath").get().files)
printmapping("out.map")
keep("""
public class * {
public protected *;
}
""".trimIndent())
keepclassmembers(mapOf("allowoptimization" to true), """
enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
""".trimIndent())
keepclassmembers("""
class * implements java.io.Serializable {
static final long serialVersionUID;
static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace ();
java.lang.Object readResolve ();
}
""".trimIndent())
overloadaggressively()
}
val javaComponent = project.components.findByName("java") as AdhocComponentWithVariants
javaComponent.addVariantsFromConfiguration(configurations.getByName("proguard")) {
// I have no idea what I should do here and the documentation is very confusing
}
publishing {
publications {
create<MavenPublication>("myLibrary") {
from(components["java"])
artifact(proguard.get().outJarFiles[0]) {
builtBy(proguard.get())
classifier = "proguard"
}
}
}
repositories {
maven {
name = "myRepo"
url = uri("file://${buildDir}/repo")
}
}
}
tasks.named("publish").configure {
dependsOn(tasks.named("proguard"))
}
答案 1 :(得分:0)
由于已经对问题进行编辑,所以我找到了解决方案,并相应地更新了Github-Project。由于Stackoverflow喜欢拥有本地信息并且不依赖链接,因此我在这里发布了有效的build.gradle文件作为答案。 Github项目的其余部分只是由gradle init
创建的Java库项目。
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'net.sf.proguard:proguard-gradle:6.2.2'
classpath 'net.sf.proguard:proguard-base:6.2.2'
}
}
plugins {
id 'java-library'
id 'maven-publish'
}
group = 'org.example'
version = '1.0.0'
repositories {
jcenter()
}
dependencies {
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:23.0'
testImplementation 'junit:junit:4.12'
}
task createObfuscated(type: proguard.gradle.ProGuardTask, dependsOn: 'jar') {
description 'Optimizes and obfuscates the created distribution jar.'
verbose
injars "${buildDir}/libs/${project.name}-${version}.jar"
outjars "${buildDir}/obf/${project.name}-${version}.jar"
if (System.getProperty('java.version').startsWith('1.')) {
libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
} else {
libraryjars "${System.getProperty('java.home')}/jmods/java.base.jmod", jarfilter: '!**.jar', filter: '!module-info.class'
}
libraryjars configurations.findByName('runtimeClasspath').getFiles()
printmapping 'out.map'
keep 'public class * { \
public protected *; \
}'
keepclassmembers allowoptimization: true, 'enum * { \
public static **[] values(); \
public static ** valueOf(java.lang.String); \
}'
keepclassmembers 'class * implements java.io.Serializable { \
static final long serialVersionUID; \
static final java.io.ObjectStreamField[] serialPersistentFields; \
private void writeObject(java.io.ObjectOutputStream); \
private void readObject(java.io.ObjectInputStream); \
java.lang.Object writeReplace(); \
java.lang.Object readResolve(); \
}'
overloadaggressively
}
task copyObfuscated(type: Copy, dependsOn: createObfuscated) {
from "${buildDir}/obf"
into "${buildDir}/libs"
include '*.jar'
}
task deleteObfuscated(type: Delete, dependsOn: copyObfuscated) {
delete '${buildDir}/obf'
}
task proguard dependsOn deleteObfuscated
publishing {
publications {
myLibrary(MavenPublication) {
from components.java
}
}
repositories {
maven {
name = 'myRepo'
url = "file://${buildDir}/repo"
}
}
}
publish.dependsOn proguard
wrapper {
gradleVersion '6.0.1'
}