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。

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM