掌握Maven(Q&A)

1、报错:No plugin descriptor found at META-INF/maven/plugin.xml

在执行Maven指令mvn clean test 时,遇到上述报错信息。原因是:项目中包含多个模块,当前编译的模块依赖的其他某个模块没有被install到本地仓库。因此对于依赖的模块要先执行mvn install指令。

参看:https://tieba.baidu.com/p/3274758816

2、发现部分Maven项目中的POM文件包含Import-Package标签,例如:

<instructions>
	<!-- transitive dependencies are not being set as optional,see https://issues.apache.org/jira/browse/FELIX-3877 -->
	<Import-Package>
		com.sleepycat*;resolution:=optional,org.apache.maven*;resolution:=optional,org.codehaus.plexus*;resolution:=optional,org.logicalcobwebs.proxool*;resolution:=optional,org.dom4j*;resolution:=optional,*
	</Import-Package>
	<Export-Package>quickfix.*,org.quickfixj.*</Export-Package>
	<Private-Package>quickfix.*,org.quickfixj.*</Private-Package>
</instructions>

 

Import-Package是什么意思?

      Import-Package是用来简化由maven-bundle-plugin插件自动生成的jar包的导入。大多数情况下,不需要为Import-Package指定任何东西,因为默认是*, 它表示任何在项目字节码中被引用的外部包都会被导入。

      这个依赖段提供大量的bundle供生成字节码的编译器和改善包导入的bnd(插件)使用。例如,它能够根据该依赖段中提供的一个bundle的某个版本添加一个版本范围。

参看:https://stackoverflow.com/questions/12445933/how-can-maven-bundle-plugin-find-packages-in-import-package

 

3、maven的java web项目启动找不到Spring ContextLoaderListener问题

报错信息:

 

严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
严重: Error configuring application listener of class org.springframework.web.util.Log4jConfigListener
java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener

 

解决方法:

 

1. 右键单击工程项目 ->点击 properties
2. 选择 Deployment Assembly
3. 点击 Add -> Java Build Path Entries -> Next
4. 选择 Maven Dependencies -> Finish -> Apply -> OK
5. Clean project and server. 重启server

 

参看:http://www.codingyun.com/article/92.html

 

4、在Eclipse中执行maven test命令,报错:

 

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project XXXX-webservice: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test failed: Plugin org.apache.maven.plugins:maven-surefire-plugin:2.12.4 or one of its dependencies could not be resolved: Failure to find org.apache.maven.surefire:surefire-api:jar:2.12.4 in http://192.168.01:8081/repository/maven-public/ was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced -> [Help 1]

 

解决方法:

     在pom.xml中配置依赖的插件,如:

 

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.7</version>
</plugin>

 

5、将Maven项目首次导入Eclipse中时,启动server后,maven项目没有执行编译。

解决方法:项目->右键->属性->Project Facets , 去掉JAX-RS、Dynamic Web Module单选按钮的勾选,点击“Apply”按钮,然后在勾选“Dynamic Web Module”单选按钮,再次点击“Apply”按钮。 OK

 

6、在Eclipse中创建的Maven项目,提示找不到自己创建的类。

 

解决方法:项目右键->Maven -> Update project

 

7、在Eclipse中创建的Maven项目,提示“错误: 找不到或无法加载主类”。

我遇到的情况是项目的target目录下的class文件没有生成,这时如果执行某个含有main方法类的"Debug as"时,就会出现这样的错误,当这些class文件存在是不会出现。

解决方法:项目右键->Maven -> Update project(这样会在target下重新生成class文件)

 

8、在Eclipse中创建的Maven项目,程序中使用了配置文件,程序运行时总提示配置文件找不到

我遇到的情况如该博客所示:参看:http://www.cnblogs.com/dyllove98/archive/2013/08/06/3241103.html



9、将Jar安装到本地仓库和Jar上传到私服

参看:http://blog.csdn.net/we_shell/article/details/49819221

注意:nexus私服的相应仓库需要开启Allow redeploy,另外还需注意权限问题,参看:http://www.javatang.com/archives/2010/01/23/4518375.html

 

10、nexus私服,创建一个可发布构件到远程仓库的角色时需要注意的事情:比如涉及的远程仓库为maven-releases

     该角色需要包含nx-repository-view-maven2-maven-releases-edit权限,并且需要包含nx-anonymous角色。

原因解释:https://issues.sonatype.org/browse/NEXUS-14523

     总体来讲,admin权限是用来管理repository本身的,比如创建repository、编辑repository、删除repository,而view权限是用来管理repository的使用的,比如上传component(组件)、删除component、更新component等。

 

11、eclipse中maven项目关联jar包源码

       在Maven项目中,我们可能需要关联查看依赖的jar包的源码,可以通过以下方式获取到源码:

参看:https://www.cnblogs.com/lizhonghua34/p/6178035.html

(1)、Eclipse中项目(maven项目)导航窗口:项目名称右键 -> Maven -> Download Sources .

       这种方式可以下载该项目的所有依赖的jar的源码。

(2)、Eclipse中项目(maven项目)导航窗口:Maven Dependencies目录下 ->选择jar包上右键 -> Maven -> Download Sources

      这种方式可以下载某个依赖的jar包的源码

附:如果本地已经下载好了源码,需要将jar与源码关联的话,可以通过以下方式:

     Eclipse中项目(maven项目)导航窗口:Maven Dependencies目录下 ->选择jar包上右键 -> Properties -> Java Source Attachment -> External location.

选择指定源文件的path, 设置Encoding,即可。

注:上述通过Maven Download Sources的方法已经默认设置了源码的关联。

 

12、maven 安装本地jar包到本地仓库中时,发生错误

The goal you specified requires a project to execute but there is no POM in this directory (C:\Users\xxx). Please verify you invoked Maven from the correct directory. -> [Help 1]

原因:指定的jar包路径中包含空格。 例如:

mvn install:install-file -Dfile=D:\Documents and Settings\xxx\Desktop\cglib-2.2.jar -DgroupId=cglib -DartifactId=cglib -Dversion=2.2 -Dpackaging=jar  -DgeneratePom=true -DcreateChecksum=true

其中,-Dfile后面的jar路径“D:\Documents and Settings\xxx\Desktop\cglib-2.2.jar”中的“Documents and Settings”存在空格。

解决方法1:修改jar包路径,移除路径中的空格;

解决方法2:将jar包路径用双引号括起来,例如-Dfile="D:\Documents and Settings\xxx\Desktop\cglib-2.2.jar"。

参看:https://blog.csdn.net/dulildx/article/details/43227017

13、Maven项目中的pom.xml报错:Missing artifact xx.xxx.xxx:jar:x.x.x 

这种错误是因为该jar包没有安装到本地仓库,可以尝试使用Maven Update命令更新项目,如果还是不行(有可能是网络原因导致的jar包下载失败),可以找一份其他人的maven仓库中的对应jar的对应版本的文件夹复制一份到自己的仓库。

也可能是maven setting配置的原因,可以在浏览器中访问一下程序日志中显示的仓库地址,如果仓库地址不能访问,就是配置问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值