您可以使用 IntelliJ 的 Gradle 匯入Eclipse Buildship 在 IDE 中開啟此範例。

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

此範例定義了一個應用程式,它依賴於 Maven 中央的函式庫,其中一些函式庫不可用於模組。它使用 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'