--仅在编译时添加模块

--add-modules only on compilation

我正在使用 Maven 和 构建我的项目。我在 pom.xml 文件中添加了:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${maven-compiler-plugin.version}</version>
    <configuration>
        <source>${java.version}</source>
        <target>${java.version}</target>
        <compilerArgs>
            <arg>--add-modules</arg>
            <arg>java.xml.bind</arg>
        </compilerArgs>
    </configuration>
</plugin>

但是,对于 运行 应用程序,我必须 运行 像这样:

java -jar --add-modules java.xml.bind my-app.jar

有没有办法从命令行构建应用程序 运行 而无需 --add-modules java.xml.bind 到 java 命令行参数?

我不久前做了 this answer,我回答这个作为使用 Maven 在 Java-9 中公开非 java.se 包的附加信息。

增加的部分特别关注使用单机版 java.xml.* API。为了适应它,您可能可以开始使用对 jaxb-api:2.3.0 的依赖,它可以作为模块加载,也可以从类路径执行。您需要做的更改是将以下内容添加到依赖项列表中:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

通过这种方式,您可以确保迁移到模块的独立 API 以及远离 deprecated piece of code.