2

在我开发的插件中抛出了 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。

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

4

1 回答 1

6

我会先在这里检查:

Maven 类加载指南

如果这没有帮助,也许是一些诊断代码,如下所示:

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
于 2012-06-11T18:39:02.983 回答