Java 9模块下Kotlin项目同模块符号访问报错的非导出包解决办法
我有一个极简Kotlin演示项目,完整代码如下:
package com.example.demo import io.swagger.annotations.Api @Api(tags = [API_NAME]) class DemoClass const val API_NAME = "demo" fun main() = println("Hello World!")
原本运行正常,添加Java 9模块系统后,在src/main/kotlin下创建了module-info.java,内容如下:
module com.example.demo { requires swagger.annotations; requires kotlin.stdlib; }
出现编译错误:
E:\demo\src\main\kotlin\com\example\demo\DemoClass.kt:[5,18] Symbol is declared in module 'com.example.demo' which does not export package 'com.example.demo'
问题出在@Api注解中使用的API_NAME常量。虽然在module-info.java中导出com.example.demo包可以解决问题,但不想这么做,求其他解决办法。
使用Maven作为构建工具,pom.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> <kotlin.version>1.7.10</kotlin.version> </properties> <dependencies> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.6.6</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk8</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-test</artifactId> <version>${kotlin.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <sourceDirectory>src/main/kotlin</sourceDirectory> <testSourceDirectory>src/test/kotlin</testSourceDirectory> <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> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> <configuration> <args> <arg>-Xjsr305=strict</arg> </args> <compilerPlugins> <plugin>spring</plugin> </compilerPlugins> <jvmTarget>1.8</jvmTarget> </configuration> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-allopen</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
mvn clean install -e的完整输出:
[INFO] Error stacktraces are turned on. [INFO] Scanning for projects... [INFO] [INFO] --------------------------< com.example:demo >-------------------------- [INFO] Building demo 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ demo --- [INFO] Deleting E:\demo\target [INFO] [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Using 'UTF-8' encoding to copy filtered properties files. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ demo --- [INFO] Required filename-based automodules detected: [swagger-annotations-1.6.6.jar]. Please don't publish this project to a public artifact repository! [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to E:\demo\target\classes [INFO] [INFO] --- kotlin-maven-plugin:1.7.10:compile (compile) @ demo --- [INFO] Applied plugin: 'spring' [ERROR] E:\demo\src\main\kotlin\com\example\demo\DemoClass.kt: (5, 18) Symbol is declared in module 'com.example.demo' which does not export package 'com.example.demo' [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.825 s [INFO] Finished at: 2022-07-16T01:49:05+02:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.7.10:compile (compile) on project demo: Compilation failure [ERROR] E:\demo\src\main\kotlin\com\example\demo\DemoClass.kt:[5,18] Symbol is declared in module 'com.example.demo' which does not export package 'com.example.demo' [ERROR] [ERROR] -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.7.10:compile (compile) on project demo: Compilation failure E:\demo\src\main\kotlin\com\example\demo\DemoClass.kt:[5,18] Symbol is declared in module 'com.example.demo' which does not export package 'com.example.demo' at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:972) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293) at org.apache.maven.cli.MavenCli.main (MavenCli.java:196) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:566) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347) Caused by: org.jetbrains.kotlin.maven.KotlinCompilationFailureException: Compilation failure E:\demo\src\main\kotlin\com\example\demo\DemoClass.kt:[5,18] Symbol is declared in module 'com.example.demo' which does not export package 'com.example.demo' at org.jetbrains.kotlin.maven.MavenPluginLogMessageCollector.throwKotlinCompilerException (MavenPluginLogMessageCollector.java:111) at org.jetbrains.kotlin.maven.KotlinCompileMojoBase.execute (KotlinCompileMojoBase.java:212) at org.jetbrains.kotlin.maven.K2JVMCompileMojo.execute (K2JVMCompileMojo.java:222) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:972) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293) at org.apache.maven.cli.MavenCli.main (MavenCli.java:196) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:566) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347) [ERROR] [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
有几种不需要导出整个包的解决办法:
1. 使用字符串字面量替代常量
直接在@Api注解中写字符串字面量,绕过模块访问检查:
@Api(tags = ["demo"]) class DemoClass
这种方法最简单,适合常量值不需要复用的场景。
2. 将常量移至未命名包(默认包)
把API_NAME常量放到默认包中,默认包的内容对模块内部是可见的:
// 去掉package声明,直接定义常量 const val API_NAME = "demo"
然后在DemoClass.kt中引用:
package com.example.demo import io.swagger.annotations.Api import API_NAME @Api(tags = [API_NAME]) class DemoClass fun main() = println("Hello World!")
注意:默认包在模块化系统中不推荐长期使用,但能快速解决问题。
3. 使用opens指令而非exports
如果需要保留常量在原包中,但不想导出整个包供外部模块调用,可以使用opens指令开放包给反射访问——因为Swagger注解在运行时需要反射读取值,opens只允许反射访问,不会让包内的类/常量被其他模块直接引用:
module com.example.demo { requires swagger.annotations; requires kotlin.stdlib; opens com.example.demo to swagger.annotations; }
这种方式更符合模块化设计原则,既满足Swagger的反射需求,又不会把包暴露给所有模块。
4. 把常量放在专门的开放包中
创建一个新的包,比如com.example.demo.constants,在module-info.java中导出或开放这个包,把API_NAME移到该包下:
// com/example/demo/constants/ApiConstants.kt package com.example.demo.constants const val API_NAME = "demo"
修改module-info.java:
module com.example.demo { requires swagger.annotations; requires kotlin.stdlib; opens com.example.demo.constants to swagger.annotations; // 或者如果不需要反射限制,直接导出:exports com.example.demo.constants; }
然后在DemoClass中引用:
package com.example.demo import io.swagger.annotations.Api import com.example.demo.constants.API_NAME @Api(tags = [API_NAME]) class DemoClass fun main() = println("Hello World!")
这种方式更规整,适合有多个需要对外暴露的常量的场景。
内容的提问来源于stack exchange,提问作者findusl

