软件世界网 购物 网址 三丰软件 | 小说 美女秀 图库大全 游戏 笑话 | 下载 开发知识库 新闻 开发 图片素材
多播视频美女直播
↓电视,电影,美女直播,迅雷资源↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
移动开发 架构设计 编程语言 Web前端 互联网
开发杂谈 系统运维 研发管理 数据库 云计算 Android开发资料
  软件世界网 -> 研发管理 -> Maven -> 正文阅读

[研发管理]Maven


maven报错 java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/util/scan/InclusionScanException
问题的解决方案会在文章的中间给出,请一步一步阅读。
Maven是一个非常强大的构建工具,能够帮助我们自动化构建过程,从清理、编译、测试到生成报告,再到打包和部署,Maven都可以。
Maven对于项目目录结构、测试用例命名方法等内容有既定的规则,只要遵循这些规则,用户在项目间切换的时候就免去了额外学习的成本,也就是约定优于配置(Convention Over Configuration)。

第一个Maven项目

创建


采用IDEA
File -> New -> Project
[img]http://img.blog.csdn.net/20160324101752132
选择Maven(不要勾选Create from archetype)
[img]http://img.blog.csdn.net/20160324102713152
填写Maven的三坐标(GroupId,ArtifactId、Version)
[img]http://img.blog.csdn.net/20160324115610149
SNAPSHOT是快照的意思,说明该项目处于开发中,是不稳定的版本,随着项目的发展,Version会不断更新,如升级为1.0,1.1-SNAPSHOT,1.1,2.0等。
填写Project name和项目保存的路径
[img]http://img.blog.csdn.net/20160324120553535
完成之后生成的项目骨架
[img]http://img.blog.csdn.net/20160324121456627
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 —— 项目输出位置。

项目的路径
[img]http://img.blog.csdn.net/20160324121559914

编写简单的helloworld


[img]http://img.blog.csdn.net/20160324131656063
该Hello类所在的报名要与GroupId、ArtifactId相吻合。
引入Junit依赖,在pom.xml中
[img]http://img.blog.csdn.net/20160324132511311
编写测试用例
[img]http://img.blog.csdn.net/20160324132959958

采用Maven来构建项目


用Maven来运行测试用例
[img]http://img.blog.csdn.net/20160324133343194
运行会报错
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/
发现了一个重要信息
[img]http://img.blog.csdn.net/20160324142700386
在Usage中我们去访问plugin’s wiki page 我们发现
[img]http://img.blog.csdn.net/20160324142820668
这就是错误的根本原因,原来codehaus不可用了
[img]http://img.blog.csdn.net/20160324143644044
顺着它给的思路去配置maven,这个感兴趣的读者可以试一下,我试了,不起作用,还有一点我用eclipse是可以执行了,可能eclipse默认采用org.apache.maven.plugins插件,而IDEA采用org.codehaus.mojo吧,我也想不明白?既然是maven的配置文件配置的,IDE工具只是负责加载,为什么会不一样呢?
不想那么多了,还是看看怎么解决吧
进入usage页面
[img]http://img.blog.csdn.net/20160324144019268
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文件夹
[img]http://img.blog.csdn.net/20160324153216849
这就是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
[img]http://img.blog.csdn.net/20160324154256197
Maven会帮我们把项目打包
[img]http://img.blog.csdn.net/20160324154534790
命名规则是ArtifactId-Version.jar命名的。

参考文献


许晓斌. Maven实战[M]. 机械工业出版社, 2011.
InfoQ的文章
Maven官网
一篇博客

......显示全文...
    点击查看全文


上一篇文章      下一篇文章      查看所有文章
2016-03-26 16:29:39  
研发管理 最新文章
拉格朗日乘数
maven之可视化项目依赖(Visualizingdepend
mac效率工具
Atitit.css规范bem项目中CSS的组织和管理
git入门
Asimplemodelfordescribingbasicsourcesofp
Linux进程管理浅析
我的openwrt学习笔记(十九):linux便捷开
2、微控制器选择
Git使用手册:为Git仓库创建Submodule
360图书馆 软件开发资料 文字转语音 购物精选 软件下载 美食菜谱 新闻资讯 电影视频 小游戏 Chinese Culture 股票 租车
生肖星座 三丰软件 视频 开发 短信 中国文化 网文精选 搜图网 美图 阅读网 多播 租车 短信 看图 日历 万年历 2018年1日历
2018-1-17 14:52:18
多播视频美女直播
↓电视,电影,美女直播,迅雷资源↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  软件世界网 --