maven - 如何将 "--add-exports"编译器指令添加到 Maven 编译中

标签 maven kotlin java-11 java-platform-module-system

使用 Java 11 编译时出现以下错误。

Symbol is declared in module 'java.xml' which does not export package 'com.sun.org.apache.xerces.internal.xni.parser'
Symbol is declared in module 'java.base' which does not export package 'sun.net.www.protocol.http'
Symbol is declared in module 'java.base' which does not export package 'sun.net.www.protocol.file'
Symbol is declared in module 'java.xml' which does not export package 'com.sun.org.apache.xerces.internal.util'
Symbol is declared in module 'java.xml' which does not export package 'com.sun.org.apache.xerces.internal.xni.parser'
Symbol is declared in module 'java.xml' which does not export package 'com.sun.org.apache.xerces.internal.xni.parser'
Symbol is declared in module 'java.xml' which does not export package 'com.sun.org.apache.xerces.internal.xni.parser'
[...]
Symbol is declared in module 'java.xml' which does not export package 'com.sun.org.apache.xerces.internal.impl.dtd'

我需要在 Java 编译中指定 --add-exports 指令。 我不知道如何将它添加到我的 Maven 构建编译 Kotlin 代码中。

此外,我不确定 --add-exports 的确切值。

这是我的pom.xml

    <!-- Kotlin compilation -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <source>src/main/java</source>
                                <source>src/main/kotlin</source>
                            </sourceDirs>
                        </configuration>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jvmTarget>1.8</jvmTarget>
                </configuration>
            </plugin>
            <!-- Bundle a standalone JAR -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <finalName>${project.artifactId}-${project.version}-all</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <archive>
                        <manifest>
                            <mainClass>DtdFinderKt</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <!-- Attempt to specify --add-export -->
    <profiles>
        <profile>
            <id>java11-compiler-java-with-kotlin</id>
            <activation>
                <file><exists>${basedir}/src/main/kotlin</exists></file>
                <jdk>[11,)</jdk>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <configuration>
                            <compilerArgs>
                                <arg>--add-exports</arg><arg>java.xml/com.sun.org.apache.xerces.internal.impl.dtd=ALL-UNNAMED</arg>
                                <arg>--add-exports</arg><arg>java.xml/com.sun.org.apache.xerces.internal.xni.parser=ALL-UNNAMED</arg>
                                <arg>--add-exports</arg><arg>java.base/sun.net.www.protocol.http=ALL-UNNAMED</arg>
                                <arg>--add-exports</arg><arg>jdk.unsupported/sun.misc=ALL-UNNAMED</arg>
                            </compilerArgs>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

最佳答案

解决方案是使用内部 API 在每个源文件的开头包含注释。

@file:Suppress("JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE")

发现此注释的原因是 this thread on Kotlin support forum .

关于maven - 如何将 "--add-exports"编译器指令添加到 Maven 编译中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63925065/

相关文章:

android - 如何在适配器的 Kotlin 中提供长点击监听器

android - jetpack compose 中重复使用具有重复代码的列

java - Eclipse jdk 11 问题 : The type java. lang.String 无法解决。它是从所需的 .class 文件间接引用的

mqtt - 为什么 HAProxy 在 TLS 握手结束时关闭与 HiveMQ MQTT 客户端的连接?

Spring Boot + Jetty & 热部署

java - 使用H2db实现springboot时出错

kotlin - 在对话框中检索 View

java - 如何打开在Docker容器上运行的应用程序的远程调试?

java - Maven:在 pom.xml 中编译外部依赖项

java - 如何在 Maven 项目中打包资源?