Maven exec插件ClassNotFoundException

Bar*_*rry 7 java maven

我正在使用Maven exec插件从命令行运行java应用程序,命令为mvn exec:java.我已经在pom.xml中指定了主类以及相关的依赖项.

<groupId>com.example.MyApp</groupId>
<artifactId>MyApp</artifactId>
<version>1.0.0</version>
<build>
  <plugins>
     <plugin>
       <groupId>org.codehaus.mojo</groupId>
       <artifactId>exec-maven-plugin</artifactId>
       <version>1.2.1</version>
       <executions>
         <execution>
           <goals>
             <goal>java</goal>
          </goals>
         </execution>
       </executions>
       <configuration>
          <mainClass>com.example.myclass</mainClass>
          <arguments>
            <argument>configFile</argument>
            <argument>properties</argument>
          </arguments>
       </configuration>
     </plugin>
Run Code Online (Sandbox Code Playgroud)

我还指定了一些依赖...

<dependencies>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>com.example.MyLibrary</groupId>
    <artifactId>MyLibrary</artifactId>
    <version>1.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

MyApp程序读取一个配置文件,该文件作为命令行参数传入.配置文件包含位于的类的名称MyLibrary.所以这个类可能com.mypackage.driver.MyClass位于上面列出MyLibraryMyAppjar 的依赖项中.但是,当我尝试运行时,我得到一个ClassNotFoundException......

更新----我正在使用系统类加载器来加载在MyApp程序命令行中传入的类

ClassLoader loader = ClassLoader.getSystemClassLoader();
Run Code Online (Sandbox Code Playgroud)

我认为这是导致问题,因为它正在寻找默认类路径上不包含依赖项的类.

我在这里做错了什么提示?

小智 15

你还在寻找这个问题的答案吗?我有完全相同的问题,最后想出来了.

您需要在配置中添加includePluginDependencies,以使插件搜索主类的依赖项:

<configuration>
  <includePluginDependencies>true</includePluginDependencies>
  <mainClass>com.example.myclass</mainClass>
  <arguments>
    <argument>configFile</argument>
    <argument>properties</argument>
  </arguments>
</configuration>
Run Code Online (Sandbox Code Playgroud)

请参见此处:http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html#includePluginDependencies

  • 我有同样的问题,添加了 includePluginDependencies,仍然是 ClassNotFoundException。还有其他建议吗? (2认同)