【问题标题】:How add "--add-exports" compiler directive to Maven compilation如何在 Maven 编译中添加“--add-exports”编译器指令
【发布时间】:2020-09-16 17:21:09
【问题描述】:

使用 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 指令。 我不知道如何将它添加到我编译 Kotlin 代码的 Maven 构建中。

另外,我不确定--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>

【问题讨论】:

  • &lt;arg&gt;--add-exports java.xml/com.sun.org.apache.xerces.internal.impl.dtd=ALL-UNNAMED&lt;/arg&gt; 的格式怎么样?
  • @Naman 没用。同样的错误。
  • 您标记为Attempt to specify --add-export 的配置将命令行参数添加到maven-compiler-plugin,但这只会编译Java 源文件。你确定是他们依赖内部代码而不是你的 Kotlin 文件吗?此外,使用这些内部 API 的可能是编译器插件本身,而不是正在编译的代码。在这种情况下,您需要将--add-exports 添加到编译过程中。对于 Maven 进程,.mavenrc 可以做到这一点(我认为)——如果编译过程是分叉的,不知道该怎么做。
  • @Nicolai 是的,API 用于 Kotlin 代码。该项目仅在 Kotlin 中。
  • 基本上我需要为 kotlin-compiler 找到与 --add-export 等效的东西。

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


【解决方案1】:

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

@file:Suppress("JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE")

这个注解是因为this thread on Kotlin support forum被发现的。

【讨论】:

    猜你喜欢
    • 2021-03-04
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多