在IDEA和maven中添加编译参数-parameters。解决Name for argument of type xxx not specified..问题
修复类似 Name for argument of type xxx not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag. 的报错
·
为项目添加全局的 -parameters 参数
你也可以为整个项目设置全局编译参数,这会应用于项目中的所有模块:
-
打开 IDE 设置:
- 从顶部菜单中选择
File
->Settings...
(在 macOS 上选择IntelliJ IDEA
->Preferences...
)。
- 从顶部菜单中选择
-
进入编译器设置:
- 在设置窗口左侧,选择
Build, Execution, Deployment
->Compiler
->Java Compiler
。
- 在设置窗口左侧,选择
-
添加编译器参数:
- 在右侧的
Additional command line parameters
框中,输入-parameters
。
- 在右侧的
-
保存设置:
- 点击
Apply
和OK
以保存更改。
- 点击
验证编译参数
可以通过以下步骤验证配置是否成功应用:
-
清理并重建项目:
- 从顶部菜单中选择
Build
->Rebuild Project
。
- 从顶部菜单中选择
-
检查编译输出:
- 在项目编译完成后,查看编译器输出窗口,以确保
-parameters
参数正确应用。
- 在项目编译完成后,查看编译器输出窗口,以确保
在构建工具中配置(Maven 或 Gradle)
如果你使用构建工具如 Maven 或 Gradle,可以直接在构建脚本中添加编译选项。
Maven
在 pom.xml
文件中添加以下内容:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
Gradle
在 build.gradle
文件中添加以下内容:
tasks.withType(JavaCompile) {
options.compilerArgs << '-parameters'
}
通过这种方式,你可以确保在不同的构建环境中使用相同的编译器选项。
结论
通过以上配置,你可以成功在 IntelliJ IDEA 以及构建工具中为你的项目添加 -parameters
参数,保留方法参数名称以便在运行时使用。例如,这在使用反射时尤为有用,如Spring或其他依赖参数名称的框架。
更多推荐
所有评论(0)