因為項目的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。