Maven: exec-maven-plugin 设置 classpathScope
因为项目的pom文件中依赖定义scope为provided,只能在compile与test阶段引入,如下。
<dependency> <groupId>${project.groupId}</groupId> <artifactId>atser-common</artifactId> <version>${project.parent.version}</version> <scope>provided</scope> </dependency>
通过maven exec-maven-plugin 执行调用java执行main函数如下
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <executions> <execution> <id>pythoncodegenerator</id> <phase>compile</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>com.hhasdf.PythonCodeGenerator</mainClass> <arguments> <argument>D:\\testcode\\</argument> </arguments> </configuration> </execution> </executions> </plugin>
程序会报java.lang.NoClassDefFoundError错误,这是因为在plugin的java阶段,classpath找不到依赖范围是provided级别的jar包。
这里解决方法为修改该task的classpath范围,修改为生命周期为compile阶段:<classpathScope>compile</classpathScope>
此时就不会找不到依赖啦~
PS:
Maven方面有问题可以查阅plugins的官方文档地址:https://www.mojohaus.org/exec-maven-plugin/index.html,讲的蛮详细的。
Maven的插件不会用还可以通过maven的maven-help-plugin查询插件信息,具体命令如下:
mvn help:describe -Dplugin=org.codehaus.mojo:exec-maven-plugin:1.6.0 -Ddetail,有如下结果:
classpathScope (Default: runtime) User property: exec.classpathScope Defines the scope of the classpath passed to the plugin. Set to compile,test,runtime or system depending on your needs. Since 1.1.2, the default value is 'runtime' instead of 'compile'.
说明现在默认的classpath使用runtime的,而不是compile。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?