Skip to content

About

Gradle plugin for transforming libraries and (output) jar files.

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

JarTransformer

Gradle plugin for transforming libraries and (output) jar files.
It also includes a jar merger to provide a merged jar file with all dependencies.

Usage

Adding the plugin to your gradle project works like every other plugin.

First, you need to add the plugin repository to your settings.gradle file:

pluginManagement {
    repositories {
        gradlePluginPortal()
    }
}

Then, you can add the plugin to your build.gradle file:

plugins {
    id "net.lenni0451.jar-transformer" version "x.x.x"
}

Transformer extension

To use the transformer extension, you need to add the jarTransformer block to your build.gradle file:

jarTransformer {
    jar { // Register a jar transformation
        inputFile = jar.archiveFile //The input jar file to transform
        // 'buildLibs' is a shortcut for 'project.layout.buildDirectory.dir("libs").get()' added by the plugin
        outputFile = buildLibs.file("${project.name}-repackaged.jar") // The output jar file (optional, will be same as input if not set)
        repackage { // Register a transformer
            ... // See the Transformers section for a list of available transformers and their configuration
        }
        ... // Multiple transformers can be registered here
    }
    dependency { // Register a dependency transformation
        configuration = configurations.repackageInclude // The configuration to transform the dependencies of
        repackage { // Register a transformer
            ... // See the Transformers section for a list of available transformers and their configuration
        }
        ... // Multiple transformers can be registered here
    }
    ... // Multiple jar and dependency transformations can be registered here
}

It is possible to register multiple jar and dependency transformations and each transformation can have multiple transformers.
The order of the transformers is the order in which they are applied.

Merger extension

To use the merger extension, you need to add the jarMerger block to your build.gradle file:

jarMerger {
    // The configuration containing the dependencies to merge
    configuration = configurations.runtimeClasspath // Default: configurations.runtimeClasspath
    // The destination directory for the merged jar file
    destinationDirectory = project.layout.buildDirectory.dir("libs").get() // Default: project.layout.buildDirectory.dir("libs").get()
    // The name of the merged output jar file
    fileName = "merged.jar"
    // The way duplicate files in the output jar should be handled
    // Possible values: INCLUDE (overwrite duplicates/the last file wins)
    //                  EXCLUDE (skip duplicates/the first file wins)
    //                  WARN (skip duplicates, but log a warning)
    //                  FAIL (throw an exception if duplicates are found)
    duplicatesStrategy = DuplicatesStrategy.WARN // Default: DuplicatesStrategy.WARN
    // Files to exclude from dependency jars
    // The files in your project output jar will not be affected by this
    excludes = [
        "META-INF/*.SF",
        "META-INF/*.DSA",
        "META-INF/*.RSA",
        "module-info.class",
    ]
    // Whether to merge service files in the output jar
    mergeServices = true // Default: true
    // Whether to merge log4j plugin cache files in the output jar
    mergeLog4jPlugins = true // Default: true
}

The task is automatically registered as jarMerger and will run after the assemble task.

Transformers

RepackageTransformer

This transformer can be used to remap packages in the jar file. It supports remapping of classes, strings, services and the manifest.
The whitelist and blacklist can be mixed to control which files are transformed.

repackage {
    // Whitelist of paths to transform
    whitelist = [
        // Only transform files in the "com/example" package
        "com/example",
        // Only transform files in the "META-INF/services" directory
        "META-INF/services",
    ]

    // Blacklist of paths to not transform
    blacklist = [
        // Do not transform files in the "com/example/internal" package
        "com/example/internal",
        // Do not transform files in the "META-INF" directory
        "META-INF",
    ]

    // The relocations to apply
    relocations = [
        // Remap all classes in packages starting with "com.example" to "repackaged.com.example"
        // Example: "com.example.MyClass" -> "repackaged.com.example.MyClass", "com.examplepkg.MyOtherClass" -> "repackaged.com.examplepkg.MyOtherClass"
        "com.example": "repackaged.com.example",
        // Remap all classes in the package "com.example" to "repackaged.com.example"
        // Example: "com.example.MyClass" -> "repackaged.com.example.MyClass", "com.examplepkg.MyOtherClass" -> "com.examplepkg.MyOtherClass"
        "com.example.": "repackaged.com.example.",
    ]

    // Whether to remap classes in the jar file
    // Can be set to false if you only want to remap strings, services or the manifest
    remapClasses = true // Default: true

    // Whether to move files in the jar file to the new package structure
    // Should probably only be set to false if remapClasses is also false
    // Could be useful to remap selected strings after another repackage transformer already moved classes
    moveFiles = true // Default: true

    // Whether to remap strings inside classes
    // This works with strings in dot notation (e.g. "com.example.MyClass") and in slash notation (e.g. "com/example/MyClass")
    // Disabled by default, because it could easily break things
    remapStrings = false // Default: false

    // Whether to remap services in the jar file
    remapServices = true // Default: true

    // Whether to remap the manifest in the jar file
    remapManifest = true // Default: true

    // Whether to remove empty directories in the jar file
    // After remapping classes, some directories might be empty
    // Removing them is not required, but can help to reduce the size of the jar file and make it cleaner
    // This also removes empty directories which are not part of the relocations
    removeEmptyDirs = false // Default: false

    // Whether to remap log4j plugin classes
    // This is only required when repackaging log4j itself
    remapLog4jPlugins = true; // Default: true
}

ExcludeTransformer

This transformer can be used to exclude files from the jar file.

exclude {
    // The files to exclude from the jar file
    // A file is removed if its path starts with one of the specified paths
    // The paths are relative to the root of the jar file
    excludes = [
        // Exclude all files in the "META-INF" directory
        "META-INF",
        // Exclude all files in the "com/example" package
        "com/example",
    ]

    // A list of regex patterns to match files against
    // The entire path of the file needs to match the regex pattern
    regexExcludes = [
        // Exclude all files containing "Test" in their name
        ".*Test.*",
        // Exclude all files ending with ".class"
        ".*\\.class$",
    ]

    // Reverse the exclusion logic
    // If set to true, only files that do not match the excludes or regexExcludes will be kept
    reversed = false // Default: false
}

AccessTransformer

This transformer can be used to make classes, fields and methods accessible/mutable.

access {
    // The classes, fields and methods to make public
    accessible = [
        // Make the class "org.example.MyClass" public
        "org/example/MyClass",
        "Lorg/example/MyClass;",
        // Make the field "org.example.MyClass#myField" public
        "org/example/MyClass.myField:I",
        "Lorg/example/MyClass;myField:I",
        // Make the method "org.example.MyClass#myMethod()" public
        "org/example/MyClass.myMethod()V",
        "Lorg/example/MyClass;myMethod()V",
    ]

    // The classes, fields and methods to make mutable (non-final)
    mutable = [
        // Make the class "org.example.MyClass" mutable
        "org/example/MyClass",
        "Lorg/example/MyClass;",
        // Make the field "org.example.MyClass#myField" mutable
        "org/example/MyClass.myField:I",
        "Lorg/example/MyClass;myField:I",
        // Make the method "org.example.MyClass#myMethod()" mutable
        "org/example/MyClass.myMethod()V",
        "Lorg/example/MyClass;myMethod()V",
    ]

    // The classes, fields and methods to make public and mutable
    full = [
        // Make the class "org.example.MyClass" public and mutable
        "org/example/MyClass",
        "Lorg/example/MyClass;",
        // Make the field "org.example.MyClass#myField" public and mutable
        "org/example/MyClass.myField:I",
        "Lorg/example/MyClass;myField:I",
        // Make the method "org.example.MyClass#myMethod()" public and mutable
        "org/example/MyClass.myMethod()V",
        "Lorg/example/MyClass;myMethod()V",
    ]
}

StringReplaceTransformer

This transformer can be used to replace strings in the jar file.

stringReplace {
    // File extensions to apply the string replacements to
    fileExtensions = [
        ".class", // Default value
        ".yml", // Replace strings in yaml files
        ".json" // Replace strings in json files
    ]

    // The placeholders to replace in the jar file
    // Strings are replaced by iterating over the replacements map and replacing each key with its value
    // The order of the replacements is not guaranteed, so do not rely on it
    replacements = [
        // Replace the placeholder "${version}" with the project version
        "${version}": project.version
    ]

    // Regex replacements can be used to replace strings matching a regex pattern
    regexReplacements = [
        // Strip all leading and trailing whitespace from strings
        "^[ \\t]+": "",
        "[ \\t]+$": "",
    ]
}

ClassTransformTransformer

This transformer can be used to transform classes in the jar using my ClassTransform library.
When this transformer is used, a transformers source set is automatically created in the project. You need to put all your class transform classes in the src/transformers/java directory.
You can only put transformer classes in that directory, other classes will throw an exception when the transformer is loaded.

By default, the ClassTransform library and the target jar/dependency being transformed are automatically added to the compilation classpath. You can add more dependencies (like other projects or external libraries) by using the classpath property of the transformer.

Warning

At the moment, the ClassTransform transformer does not support dependency transformations!

classTransform {
    // The package or single class to load as a transformer
    // Use "*" to load all transformers in the specified package, but not in subpackages
    // Use "**" to load all transformers in the specified package and all subpackages
    // Example: "test.*" will load "test.MyTransformer" but not "test.sub.MyOtherTransformer"
    // Example: "test.**" will load "test.MyTransformer" and "test.sub.MyOtherTransformer"
    // Example: "test.MyTransformer" will load only that class
    included = "**" // Default: "**"

    // Add dependencies to the compilation classpath of ClassTransform transformers
    // This allows transformers to reference classes and methods from these dependencies
    classpath.from(configurations.runtimeClasspath)
}

About

Gradle plugin for transforming libraries and (output) jar files.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages