yWorks / yGuard

The open-source Java obfuscation tool working with Ant and Gradle by yWorks - the diagramming experts

Home Page:https://yworks.github.io/yGuard/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

compileOnly - Task FAILED Unable to load class 'com.yworks.yguard.YGuardTask'.

eltonsandre opened this issue · comments

compileOnly - Task FAILED Unable to load class 'com.yworks.yguard.YGuardTask'.

version:
JDK 17
Gradle 8.3

//build.gradle

dependencies {
    compileOnly 'com.yworks:yguard:4.1.0'
}

tasks.register('yguard') {
    dependsOn jar
    group 'obfuscate'
    description 'Obfuscates and shrinks the java archive.'

    doLast {
        ant.taskdef(name: 'yguard',
                classname: 'com.yworks.yguard.YGuardTask',
                classpath: sourceSets.main.runtimeClasspath.asPath)

        def archivePath = jar.archiveFile.get().asFile.path

        ant.yguard {
            inoutpair(in: archivePath, out: archivePath)

            attribute(name: "Deprecated")

            rename(logfile: "${buildPath}/ofuscation-rename.log.xml") {
                property(name: "naming-scheme", value: "mix")

                adjust(replaceContent: true, replaceContentSeparator: '.', includes: 'META-INF/*.xml')
            }

        }

    }
}

Starting Gradle Daemon...
Connected to the target VM, address: '127.0.0.1:39767', transport: 'socket'
Gradle Daemon started in 1 s 334 ms

Configure project :
Task :clean
Task :initializeIntelliJPlugin
Task :patchPluginXml FROM-CACHE
Task :verifyPluginConfiguration
Task :compileKotlin FROM-CACHE
Task :processResources
Task :generateEffectiveLombokConfig
Task :compileJava FROM-CACHE
Task :classes
Task :instrumentCode SKIPPED
Task :jar
Task :inspectClassesForKotlinIC
Task :compileTestKotlin NO-SOURCE
Task :classpathIndexCleanup
Task :instrumentedJar
Task :yguard FAILED

FAILURE: Build failed with an exception.

  • Where:
    Script '/home/user/develop/launch/gradle/build.gradle' line: 63

  • What went wrong:
    Execution failed for task ':yguard'.

taskdef class com.yworks.yguard.YGuardTask cannot be found
using the classloader AntClassLoader[/home/user/develop/launch/build/classes/java/main:/home/user/develop/launch/build/classes/kotlin/main:/home/user/develop/launch/build/resources/main]

  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
Get more help at https://help.gradle.org.

BUILD FAILED in 16s

You specified yGuard to be a compile-only dependency, but are trying to reference the yGuard jar through a runtime classpath.

Follow the approach in the application example and create a separate configuration for yGuard:

...

configurations {
  yguard
}

dependencies {
  yguard 'com.yworks:yguard:4.1.0'
}

task obfuscate {
  dependsOn jar
  group 'yGuard'
  description 'Obfuscates the java archive.'

  doLast {
    def archivePath = jar.archiveFile.get().asFile.path
    def unobfJar = archivePath.replace(".jar", "_unobf.jar")

    ant.move(file: archivePath, tofile: unobfJar, verbose: true)

    ant.taskdef(
      name: 'yguard',
      classname: 'com.yworks.yguard.YGuardTask',
      classpath: configurations.yguard.asPath
    )
    ant.yguard {
      inoutpair(in: unobfJar, out: archivePath)
      rename(logfile: "${buildDir}/${rootProject.name}_renamelog.xml") {
        ...
      }
    }
  }
}

And do not try to overwrite the input jar with the output jar in the yguard task. I am pretty sure that will not work correctly.

Hi @thomasbehr,

Adding yguard configuration and adjusting classpath, it worked, Thanks!

//build.gradle
configurations { // Add here
    yguard 
}

dependencies {
    yguard 'com.yworks:yguard:4.1.0' //edit here
}

tasks.register('yguard') {
    dependsOn jar
    group 'obfuscate'
    description 'Obfuscates and shrinks the java archive.'

    doLast {
        ant.taskdef(name: 'yguard',
                classname: 'com.yworks.yguard.YGuardTask',
                classpath: configurations.yguard.asPath) //edit here

        def archivePath = jar.archiveFile.get().asFile.path

        ant.yguard {
            inoutpair(in: archivePath, out: archivePath)

            attribute(name: "Deprecated")

            rename(logfile: "${buildPath}/ofuscation-rename.log.xml") {
                property(name: "naming-scheme", value: "mix")

                adjust(replaceContent: true, replaceContentSeparator: '.', includes: 'META-INF/*.xml')
            }

        }

    }
}