在 JDK 11 中访问 `sun.security.x509` 没有模块?

Access `sun.security.x509` in JDK 11 without modules?

(tl,最后是博士) 我们有一个生成自签名 SSL 证书的小方法,它显然依赖于 sun.security.x509。因此,目前我们仍在使用 JDK8 构建它,即使代码库的其余部分(它只是一个小的、单一的库)是使用 JDK11 和 运行 与 JVM11 构建的。

不幸的是,根据(CertificateFactory 与生成证书几乎没有任何关系,这与它的 javadoc 状态相反......),主要 JDK 中没有替换......):

一个选择是使用 BouncyCastle,但那是我们真正不需要的额外 4MB,特别是对于这么小的任务,所以我在考虑如何访问它,同时

据我所见,包和所需的 类 仍然是包,相关的 类 仍然存在(参见 sun.security.x509 on github 但是在构建它时(使用 maven)我得到错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project: Compilation failure: Compilation failure:
[ERROR] OldSelfSignedCertificateGenerator.java:[20,25] package sun.security.x509 does not exist
[ERROR] OldSelfSignedCertificateGenerator.java:[71,45] cannot find symbol
[ERROR]   symbol:   class X509CertInfo
[ERROR]   location: class OldSelfSignedCertificateGenerator

我搜索了一下并添加:

<arg>--add-exports</arg><arg>java.base/sun.security.x509=ALL-UNNAMED</arg>

maven-compiler-plugin 并且它有点工作 - 我只得到 WARNING 而不是关于 sun.security.x509 包:

[WARNING] OldSelfSignedCertificateGenerator.java:[20,25] sun.security.x509.AlgorithmId is internal proprietary API and may be removed in a future release

但是!现在看来我进入了(不情愿!)模块系统并且它抱怨访问其他基本 Java 类 (还有一个我们的依赖项):

[ERROR] CertificateUtil.java:[35,17] package java.util.logging is not visible
  (package java.util.logging is declared in module java.logging, but module java.base does not read it)

我尝试以相同的方式将 java.logging 模块添加到导出中,但没有成功。似乎我还必须将这个库及其对模块系统的依赖转换为模块系统,这并不是真正需要的。

问题与

有些相关

tl,博士; 有没有办法在没有模块系统的情况下使用 JDK 11 下的 sun.security.x509 包编译库?一些简单的开关?

事实证明,这可能与以下事实有关:由较新的 JDK (9+) 版本生成的构建在 JDK8:

下无法执行
<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>9</source>
        <target>9</target>
        <release combine.self="override"></release>
        <compilerArgs>
            <arg>--add-exports</arg><arg>java.base/sun.security.x509=ALL-UNNAMED</arg>
        </compilerArgs>
    </configuration>
</plugin>

要在 gradle 中包含 sun.security.[somePackage] 类,您可以添加:

tasks.withType(AbstractCompile) {
    options.compilerArgs += ["--add-exports", "java.base/sun.security.util=ALL-UNNAMED"]
    options.compilerArgs += ["--add-exports", "java.base/sun.security.pkcs=ALL-UNNAMED"]
}