beryx / badass-runtime-plugin

Create a custom runtime image of your non-modular application
https://badass-runtime-plugin.beryx.org
Apache License 2.0
162 stars 21 forks source link

How to merge proguard with this plugin? #40

Closed KenobySky closed 5 years ago

KenobySky commented 5 years ago

Hi! Sorry for bothering you again DjVicking. But I got stuck and sincerely, couldnt find someone to help me with this.

My current gradle version is: gradle-5.6.2-all

Problem description

How do I use proguard so the code gets obfuscated and optimized before jPackage do its job? I mean, I want my code thats inside the .msi gets obfuscated.

Ive found a tutorial (Tutorial) for an older gradle version however im not sure how to mix this with the current plugins. I've tried the code below but im not sure how to merge it with jPackage task.


**My current working build.gradle**

    //unable to resolve class proguard.gradle.ProGuardTask?
    import proguard.gradle.ProGuardTask

    buildscript {
        repositories {
            mavenCentral()
        }

        dependencies {
            classpath 'net.sf.proguard:proguard-gradle:5.2.1'
        }
    }

    plugins {
        id 'maven'
        id 'java'
        id 'application'
        id 'org.openjfx.javafxplugin' version '0.0.8'
        id 'org.beryx.runtime' version '1.7.0'
        id "com.github.johnrengelman.shadow" version "5.1.0"
    }

    repositories {
        mavenCentral()
    }

    ext {
        openJfxVersion = '13'
    }

    dependencies {

            compile 'org.openjfx:javafx-base:${openJfxVersion}:win'
            compile 'org.openjfx:javafx-controls:${openJfxVersion}:win'
            compile 'org.openjfx:javafx-graphics:${openJfxVersion}:win'
            compile 'org.openjfx:javafx-fxml:13'
            compile "org.controlsfx:controlsfx:11.0.0"
            compile "eu.hansolo:tilesfx:11.13"
            compile "com.jfoenix:jfoenix:9.0.9"
            compile "org.apache.httpcomponents:httpclient:4.5.9"
            compile "org.json:json:20180813"
            compile "mysql:mysql-connector-java:8.0.17"
            compile "org.jasypt:jasypt:1.9.3"
            compile "com.mchange:c3p0:0.9.5.4"
            compile "com.sun.mail:javax.mail:1.6.2"
            compile "commons-validator:commons-validator:1.6"
        }

    group = 'br.x'
    version = '0.2'
    def javaName = archivesBaseName + '-java'
    def fullJavaName = archivesBaseName + '-full'

    sourceSets {
        main {
            java {
                srcDir 'src/main/java'
            }
        }
    }

    task fullJavaJar(type: Jar) {
        baseName = fullJavaName
        manifest {
            attributes "Implementation-Title": baseName
            attributes "Implementation-Version": version
        }
        from sourceSets.main.output
        artifacts {
            archives fullJavaJar
        }
    }

    task proguard(type: ProGuardTask, dependsOn: 'obfuscatedJar') {
        configuration 'proguard.txt'

        injars "$buildDir/libs/${fullJavaName}-${version}.jar"
        outjars "$buildDir/libs/${javaName}-${version}.jar"

        libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
        libraryjars configurations.compile.find { it.name.startsWith("gson") }
        libraryjars configurations.compile.find { it.name.startsWith("slf4j-api") }
    }

    task obfuscate(dependsOn: tasks.withType(ProGuardTask)) doLast {}

    install.dependsOn(obfuscate)

    install {
        repositories.mavenInstaller {
            addFilter(javaName) { artifact, file -> artifact.name.endsWith(javaName) }
            pom(javaName).whenConfigured {
                p ->
                p.dependencies = p.dependencies.findAll {
                    dep -> dep.artifactId != "junit"
                }
            }
        }
    }

    jar {
        baseName = javaName
        manifest {
            attributes "Implementation-Title": javaName
            attributes "Implementation-Version": version
        }
    }

    javafx {
        version = "13"
        modules = [ 'javafx.controls','javafx.graphics', 'javafx.fxml'  ]
    }

    mainClassName = 'Main'

    runtime {
        options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']

        jpackage {
            jpackageHome = 'C:/Program Files/Java/openjdk-14-jpackage+1-49_windows-x64_bin/'

            if(org.gradle.internal.os.OperatingSystem.current().windows) {
                installerType = 'msi'
                imageOptions = ['--icon', 'src/main/resources/images/logo.ico']
                installerOptions = ['--win-per-user-install', '--win-dir-chooser', '--win-menu', '--win-shortcut']
            }
        }

    }

    compileJava {
        doFirst {
            options.compilerArgs = [
                    '--module-path', classpath.asPath,
                    '--add-modules', 'javafx.controls,javafx.fxml'
            ]
        }
    }

    run {
        doFirst {
            jvmArgs = [
                    '--module-path', classpath.asPath,
                    '--add-modules', 'javafx.controls,javafx.fxml'
            ]
        }
    }

    tasks.withType(JavaCompile) {
        options.encoding = 'ISO-8859-1'
    }
KenobySky commented 5 years ago

SOLVED!

https://stackoverflow.com/questions/58344590/gradle-badass-runtime-plugin-and-proguard-gradle-plugin

paipeng commented 1 year ago

Hello,

I am facing the same problem: jpackage with obfuscated jar(proguard).

I have tried the link above, but I can't make it work.

Regards,

Pai

vewert commented 1 year ago

I'm not sure if this is the right/best way but works for my project. I have included only the relevant parts that I pulled out of my build script (kotlin dsl)

/**
 * Obfuscate with ProGuard
 */
tasks.register<ProGuardTask>("obfuscateJar") {
  dependsOn("jar")
  //Any initial setup ...

  val archiveName: String = tasks.getByName<Jar>("jar").archiveFileName.get()
  injars("${project.buildDir.absolutePath}/libs/$archiveName")
  outjars("${project.buildDir.absolutePath}/obfLibs/$archiveName")

  //Make other proguard configurations ...

  doLast {
    delete("${project.buildDir.absolutePath}/libs/$archiveName")

    copy {
      from("${project.buildDir.absolutePath}/obfLibs/$archiveName")
      into("${project.buildDir.absolutePath}/libs/")
    }
  }
}

/**
 * Hook for obfuscating. Configures installDist to
 * depend on obfuscate.
 */
tasks.installDist {
  dependsOn("obfuscateJar")
}

The key parts in the obfuscate task, are to output the obfuscated jar to some temp directory. In my case to a directory called 'obfLibs'. Then after the obfuscation is done, delete the un-obfuscated jar, and copy the obfuscated jar to where the original one was.

I then hooked into the obfuscate task by setting the installDist task, to dependOn the obfuscate task. The runtime task in turn then depends on installDist, so in the end runtime-> installDir -> obfucateJar - > jar, so now obfuscate is part of the build flow.

Hopefully, that makes sends, helps you get started.

paipeng commented 1 year ago

Thank you so much, I get it.

my example works now:

tasks.installDist { dependsOn("proguard") dependsOn("copyJar") proguard.finalizedBy copyJar }

Regards,

Pai