Maven--java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/util/scan/InclusionScanException
Posted GW_Cheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven--java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/util/scan/InclusionScanException相关的知识,希望对你有一定的参考价值。
maven报错 java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/util/scan/InclusionScanException
问题的解决方案会在文章的中间给出,请一步一步阅读。
Maven是一个非常强大的构建工具,能够帮助我们自动化构建过程,从清理、编译、测试到生成报告,再到打包和部署,Maven都可以。
Maven对于项目目录结构、测试用例命名方法等内容有既定的规则,只要遵循这些规则,用户在项目间切换的时候就免去了额外学习的成本,也就是约定优于配置(Convention Over Configuration)。
第一个Maven项目
创建
采用IDEA
File -> New -> Project
选择Maven(不要勾选Create from archetype)
填写Maven的三坐标(GroupId,ArtifactId、Version)
SNAPSHOT是快照的意思,说明该项目处于开发中,是不稳定的版本,随着项目的发展,Version会不断更新,如升级为1.0,1.1-SNAPSHOT,1.1,2.0等。
填写Project name和项目保存的路径
完成之后生成的项目骨架
Maven的约定
pom.xml在项目的根目录下
假设该目录为${proj-dir},那么Maven有以下假设:
${proj-dir}/src/main/java —— 存放项目的.java文件。
${proj-dir}/src/main/resources —— 存放项目资源文件,如spring, hibernate,log4j等配置文件。
${proj-dir}/src/test/jave —— 存放所有测试.java文件,如JUnit测试类。以*Test.java命名
${proj-dir}/src/test/resources —— 测试资源文件。
${proj-dir}/target —— 项目输出位置。
项目的路径
编写简单的helloworld
该Hello类所在的报名要与GroupId、ArtifactId相吻合。
引入Junit依赖,在pom.xml中
编写测试用例
采用Maven来构建项目
用Maven来运行测试用例
运行会报错
C:\\Java\\jdk1.7.0_21\\bin\\java -Dmaven.multiModuleProjectDirectory=C:\\Users\\GWCheng\\IdeaProjects\\maven-hello -Dmaven.home=D:\\servers\\apache-maven-3.3.9 -Dclassworlds.conf=D:\\servers\\apache-maven-3.3.9\\bin\\m2.conf -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA 15.0.4\\bin" -Dfile.encoding=UTF-8 -classpath "D:\\servers\\apache-maven-3.3.9\\boot\\plexus-classworlds-2.5.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA 15.0.4\\lib\\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=15.0.4 -s E:\\JavaLibrary\\.m2\\settings.xml test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-hello 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for org.apache.maven.plugins:maven-compiler-plugin:jar:3.1 is missing, no dependency information available
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-hello ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-hello ---
[WARNING] The POM for org.apache.maven.plugins:maven-compiler-plugin:jar:3.1 is missing, no dependency information available
[WARNING] Error injecting: org.apache.maven.plugin.compiler.CompilerMojo
java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/util/scan/InclusionScanException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2413)
at java.lang.Class.getDeclaredConstructors(Class.java:1855)
at com.google.inject.spi.InjectionPoint.forConstructorOf(InjectionPoint.java:245)
at com.google.inject.internal.ConstructorBindingImpl.create(ConstructorBindingImpl.java:99)
at com.google.inject.internal.InjectorImpl.createUninitializedBinding(InjectorImpl.java:658)
at com.google.inject.internal.InjectorImpl.createJustInTimeBinding(InjectorImpl.java:882)
at com.google.inject.internal.InjectorImpl.createJustInTimeBindingRecursive(InjectorImpl.java:805)
at com.google.inject.internal.InjectorImpl.getJustInTimeBinding(InjectorImpl.java:282)
at com.google.inject.internal.InjectorImpl.getBindingOrThrow(InjectorImpl.java:214)
at com.google.inject.internal.InjectorImpl.getProviderOrThrow(InjectorImpl.java:1006)
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1038)
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1001)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051)
at org.eclipse.sisu.space.AbstractDeferredClass.get(AbstractDeferredClass.java:48)
at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
at com.google.inject.internal.InternalFactoryToInitializableAdapter.provision(InternalFactoryToInitializableAdapter.java:53)
at com.google.inject.internal.ProviderInternalFactory$1.call(ProviderInternalFactory.java:65)
at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:115)
at org.eclipse.sisu.bean.BeanScheduler$Activator.onProvision(BeanScheduler.java:176)
at com.google.inject.internal.ProvisionListenerStackCallback$Provision.provision(ProvisionListenerStackCallback.java:126)
at com.google.inject.internal.ProvisionListenerStackCallback.provision(ProvisionListenerStackCallback.java:68)
at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:63)
at com.google.inject.internal.InternalFactoryToInitializableAdapter.get(InternalFactoryToInitializableAdapter.java:45)
at com.google.inject.internal.InjectorImpl$2$1.call(InjectorImpl.java:1016)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1092)
at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1012)
at org.eclipse.sisu.inject.Guice4$1.get(Guice4.java:162)
at org.eclipse.sisu.inject.LazyBeanEntry.getValue(LazyBeanEntry.java:81)
at org.eclipse.sisu.plexus.LazyPlexusBean.getValue(LazyPlexusBean.java:51)
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:263)
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:255)
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getConfiguredMojo(DefaultMavenPluginManager.java:517)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:121)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: org.codehaus.plexus.compiler.util.scan.InclusionScanException
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
... 61 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.637 s
[INFO] Finished at: 2016-03-24T13:34:34+08:00
[INFO] Final Memory: 6M/90M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project maven-hello: Execution default-compile of goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile failed: A required class was missing while executing org.apache.maven.plugins:maven-compiler-plugin:3.1:compile: org/codehaus/plexus/compiler/util/scan/InclusionScanException
[ERROR] -----------------------------------------------------
[ERROR] realm = plugin>org.apache.maven.plugins:maven-compiler-plugin:3.1
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/E:/JavaLibrary/.m2/repository/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.jar
[ERROR] urls[1] = file:/E:/JavaLibrary/.m2/repository/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import from realm ClassRealm[maven.api, parent: null]]
[ERROR]
[ERROR] -----------------------------------------------------: org.codehaus.plexus.compiler.util.scan.InclusionScanException
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException
Process finished with exit code 1
定位到错误发生的地方
[WARNING] Error injecting: org.apache.maven.plugin.compiler.CompilerMojo
java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/util/scan/InclusionScanException
意思是说注入org.apache.maven.plugin.compiler.CompilerMojo的时候找不到org/codehaus/plexus/compiler/util/scan/InclusionScanException
我找了半天在InfoQ上找到了合理的解释
Maven官方有两个插件列表,第一个列表的GroupId为org.apache.maven.plugins,这里的插件最为成熟,具体地址为:http://maven.apache.org/plugins/index.html。第二个列表的GroupId为org.codehaus.mojo,这里的插件没有那么核心,但也有不少十分有用,其地址为:http://mojo.codehaus.org/plugins.html。
InfoQ的连接
http://www.infoq.com/cn/news/2011/05/xxb-maven-8-plugin/
从InfoQ的解释和报错信息来看,是我们的maven在编译时采用的插件没找到
去maven官网的编译插件瞅瞅
http://maven.apache.org/plugins/maven-compiler-plugin/
发现了一个重要信息
在Usage中我们去访问plugin’s wiki page 我们发现
这就是错误的根本原因,原来codehaus不可用了
顺着它给的思路去配置maven,这个感兴趣的读者可以试一下,我试了,不起作用,还有一点我用eclipse是可以执行了,可能eclipse默认采用org.apache.maven.plugins插件,而IDEA采用org.codehaus.mojo吧,我也想不明白?既然是maven的配置文件配置的,IDE工具只是负责加载,为什么会不一样呢?
不想那么多了,还是看看怎么解决吧
进入usage页面
http://maven.apache.org/plugins/maven-compiler-plugin/usage.html
发现了解决方案
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<!-- put your configurations here -->
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
按照各个配置一下pom.xml
我的配置如下,将下面的代码放在dependencies之后
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<!-- put your configurations here -->
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
设置maven编译器级别有一篇博客写的很清楚
http://blog.csdn.net/xyls12345/article/details/24990665
我们再次运行上面的test命令
结果如下
C:\\Java\\jdk1.7.0_21\\bin\\java -Dmaven.multiModuleProjectDirectory=C:\\Users\\GWCheng\\IdeaProjects\\maven-hello -Dmaven.home=D:\\servers\\apache-maven-3.3.9 -Dclassworlds.conf=D:\\servers\\apache-maven-3.3.9\\bin\\m2.conf -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA 15.0.4\\bin" -Dfile.encoding=UTF-8 -classpath "D:\\servers\\apache-maven-3.3.9\\boot\\plexus-classworlds-2.5.2.jar;C:\\Program Files (x86)\\JetBrains\\IntelliJ IDEA 15.0.4\\lib\\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=15.0.4 -s E:\\JavaLibrary\\.m2\\settings.xml test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-hello 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-hello ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ maven-hello ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\\Users\\GWCheng\\IdeaProjects\\maven-hello\\target\\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-hello ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\\Users\\GWCheng\\IdeaProjects\\maven-hello\\src\\test\\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ maven-hello ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\\Users\\GWCheng\\IdeaProjects\\maven-hello\\target\\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-hello ---
[INFO] Surefire report directory: C:\\Users\\GWCheng\\IdeaProjects\\maven-hello\\target\\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.gwc.mavenlearn.hello.HelloTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.063 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 36.692 s
[INFO] Finished at: 2016-03-24T15:28:58+08:00
[INFO] Final Memory: 14M/114M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
从输出可以看出命令执行完成,测试用例也顺利运行了,而且项目中还多了一个target文件夹
这就是maven为我们做的,它会用我们刚才配置的那个插件编译我们的代码
[INFO] Compiling 1 source file to C:\\Users\\GWCheng\\IdeaProjects\\maven-hello\\target\\test-classes
然后帮我们执行了测试用例
[INFO]
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ maven-hello ---
这就是Maven的强大之处,只要我们遵循Maven的约定,它就能自动地执行清理、编译、测试到生成报告,再到打包和部署。
我们package
Maven会帮我们把项目打包
命名规则是ArtifactId-Version.jar命名的。
参考文献
许晓斌. Maven实战[M]. 机械工业出版社, 2011.
以上是关于Maven--java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/util/scan/InclusionScanException的主要内容,如果未能解决你的问题,请参考以下文章