调试ClassNotFoundException在Mojo中使用@requiresDependencyResolution时抛出我的Maven Mojo

时间:2012-06-11 17:23:09

标签: java maven

在我开发的插件中抛出了ClassNotFoundException。无法成为founf的类肯定存在,并且其关联的项目作为依赖项包含在执行项目的pom.xml文件中,如下所示:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>project-one</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

我的插件包含在执行的pom.xml中,如下所示:

<build>
<plugins>
 <plugin>
   <groupId>com.example</groupId>
                <artifactId>project-two-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <configuration>
                           <customSettingOne>
                               setting
                           </customSettingOne>
                        </configuration>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>some-task</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

我的插件类如下:

/**
 *  @goal some-task
 *
 *  @requiresDependencyResolution compile
 */

public class MyPluginMojo extends AbstractMojo {

    /**
     * Directory to save the CSV files to.
     *
     * @parameter alias="customSettingOne"
     * @required
     */
    private File customSettingOne;

}

我已尝试使用以下代码:

Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)

以及Eclipse m2e使用的嵌入式版本

Embedded (3.0.2/1.0.200.20111228-1245

当我的插件代码试图从ProjectOne加载类时,我得到一个ClassNotFoundException。

任何人都有任何想法,我怎么能到底?是否可以检查或转储插件中使用的类路径?

1 个答案:

答案 0 :(得分:5)

我先在这里查看:

Guide to Maven Classloading

如果这没有帮助,可能会有一些诊断代码,如下所示:

package stackoverflow;

import java.net.URL;
import java.net.URLClassLoader;

public class PrintClassLoader {
    public static void main(String[] args) {
        PrintClassLoader pcl = new PrintClassLoader();
        pcl.printClassLoader(pcl.getClass().getClassLoader());
    }

    public void printClassLoader(ClassLoader classLoader) {
        if (null == classLoader) {
            return;
        }
        System.out.println("--------------------");
        System.out.println(classLoader);
        if (classLoader instanceof URLClassLoader) {
            URLClassLoader ucl = (URLClassLoader) classLoader;
            int i = 0;
            for (URL url : ucl.getURLs()) {
                System.out.println("url[" + (i++) + "]=" + url);
            }
        }
        printClassLoader(classLoader.getParent());
    }
}

例如,将打印如下内容:

--------------------
sun.misc.Launcher$AppClassLoader@35ce36
url[0]=file:/D:/dev/workspaces/3.6/all/Z_temp/target/classes/
url[1]=file:/D:/dev/.m2/repository/javax/mail/mail/1.4/mail-1.4.jar
url[2]=file:/D:/dev/.m2/repository/javax/activation/activation/1.1/activation-1.1.jar
url[3]=file:/D:/dev/.m2/repository/commons-io/commons-io/2.1/commons-io-2.1.jar
--------------------
sun.misc.Launcher$ExtClassLoader@757aef
url[0]=file:/C:/java/jdk/jdk1.6.0_31/jre/lib/ext/dnsns.jar
url[1]=file:/C:/java/jdk/jdk1.6.0_31/jre/lib/ext/localedata.jar
url[2]=file:/C:/java/jdk/jdk1.6.0_31/jre/lib/ext/sunjce_provider.jar
url[3]=file:/C:/java/jdk/jdk1.6.0_31/jre/lib/ext/sunmscapi.jar
url[4]=file:/C:/java/jdk/jdk1.6.0_31/jre/lib/ext/sunpkcs11.jar