# Building Java Modules with Legacy Libraries Sample

version 9.0.0

- Groovy DSL

- Kotlin DSL

| You can open this sample in an IDE that supports Gradle.

This sample shows how artifact transforms can be utilised to turn traditional Java libraries into Java Modules by adding additional information to the corresponding Jars. For that, a plugin called `extra-java-module-info` is defined in the `buildSrc` folder. This plugin can be copied into another project and adjusted as needed to solve use cases where it is desired to treat every dependency as a Java Module.

The example defines an application that relies on libraries from Maven central where some of them are not available as modules. It uses `commons-cli` (not a module) to parse the command line arguments, which can contain a JSON String, and `gson` (a proper module) to parse the JSON string. It also utilises `commons-lang3` (an automatic module) and `commons-beanutils` (not a module) which brings in some additional dependencies that are also not modules.

By configuring our own `extra-java-module-info` plugin, we add information to turn the legacy libraries into modules.

application/build.gradle.kts

    extraJavaModuleInfo {
        // This does not have to be a complete description (e.g. here 'org.apache.commons.collections' does not export anything here).
        // It only needs to be good enough to work in the context of this application we are building.
        module("commons-beanutils-1.9.4.jar", "org.apache.commons.beanutils", "1.9.4") {
            exports("org.apache.commons.beanutils")

            requires("org.apache.commons.logging")
            requires("java.sql")
            requires("java.desktop")
        }
        module("commons-cli-1.4.jar", "org.apache.commons.cli", "3.2.2") {
            exports("org.apache.commons.cli")
        }
        module("commons-collections-3.2.2.jar", "org.apache.commons.collections", "3.2.2")
        automaticModule("commons-logging-1.2.jar", "org.apache.commons.logging")
    }

application/build.gradle

    extraJavaModuleInfo {
        // This does not have to be a complete description (e.g. here 'org.apache.commons.collections' does not export anything here).
        // It only needs to be good enough to work in the context of this application we are building.
        module('commons-beanutils-1.9.4.jar', 'org.apache.commons.beanutils', '1.9.4') {
            exports('org.apache.commons.beanutils')

            requires('org.apache.commons.logging')
            requires('java.sql')
            requires('java.desktop')
        }
        module('commons-cli-1.4.jar', 'org.apache.commons.cli', '3.2.2') {
            exports('org.apache.commons.cli')
        }
        module('commons-collections-3.2.2.jar', 'org.apache.commons.collections', '3.2.2')
        automaticModule('commons-logging-1.2.jar', 'org.apache.commons.logging')
    }

You can run the example application like this:

    run --args='-json {"message":"Hello","receivers":["Lisa","John"]} -debug'

For more information, see Java Module support in the Java Library Plugin and Java Module support in the Application Plugin.