您可以在支援 Gradle 的 IDE 中開啟此範例。

此範例示範如何利用 產出物轉換,透過在對應的 Jar 檔案中新增額外資訊,將傳統 Java 程式庫轉換為 Java 模組。為此,在 buildSrc 資料夾中定義了一個名為 extra-java-module-info 的外掛程式。此外掛程式可以複製到另一個專案中,並根據需要進行調整,以解決希望將每個相依性都視為 Java 模組的使用案例。

此範例定義了一個應用程式,該應用程式依賴 Maven Central 的程式庫,其中某些程式庫無法作為模組使用。它使用 commons-cli (非模組) 來解析命令列引數 (可能包含 JSON 字串),並使用 gson (正式模組) 來解析 JSON 字串。它還使用了 commons-lang3 (自動模組) 和 commons-beanutils (非模組),這兩者引入了一些額外的相依性,這些相依性也不是模組。

透過配置我們自己的 extra-java-module-info 外掛程式,我們新增資訊以將舊版程式庫轉換為模組。

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')
}

您可以這樣執行範例應用程式

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