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。

  

posted @   寻Meng启示  阅读(1715)  评论(0编辑  收藏  举报
编辑推荐:
· .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 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示