maven 使用

    maven是时下比较流行的项目管理和构建自动化工具。项目中需要应用到其它jar包时,只需指定jar名称和版本,其依赖的类库会自动下载,非常方便,再也不用手动四处拷贝Jar包了。缺点是插件不稳定经常报错,而且访问国外网站非常缓慢,经常下载某些库出错。最好用Nexus搭建本地maven仓库。

 

一. 安装maven

下载 maven,解压后配置 $MAVEN/conf/settings.xml 指定 localReposity

eclipse 安装 maven 插件:

http://download.eclipse.org/technology/m2e/releases

 

二. 命令

maven clean 删除target下class文件

maven build     global指定compile,在target下面产生class文件

maven package 在target下生成jar文件

maven Install   在仓库下生成相应的pom和jar文件

 

创建Maven的普通java项目:

mvn archetype:generate -DgroupId=packageName -DartifactId=projectName      #3.0.5以上版本已取消对create的支持

   

创建Maven的Web项目:  

mvn archetype:generate -DgroupId=packageName -DartifactId=webappName -DarchetypeArtifactId=maven-archetype-webapp  

 

基础maven命令

mvn eclipse:eclipse 生成eclipse项目,普通导入会报NullPointerException异常

mvn jetty:run 运行jetty

mvn exec:java -Dexec.mainClass=com.dubbo.demo.consumer.ConsumerMain exec插件用于执行class

 

三. 配置

配置信息,分为三部分:groupId:artifactId:version

groupId   包名称

aritifactId  库名称

version 版本号

 

四. maven 报错信息

自从用了maven,麻烦不断 ...

 

2.1 eclipse中使用maven插件的时候,运行run as maven build的时候报错

-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.

直接的解决方法:使用低版本的maven。 

可以设一个环境变量M2_HOME指向你的maven安装目录

M2_HOME=D:\Apps\apache-maven-3.3.1

然后在Window->Preference->Java->Installed JREs->Edit

在Default VM arguments中设置 -Dmaven.multiModuleProjectDirectory=$M2_HOME

 

2.2 Could not transfer artifact org.codehaus.plexus:plexus-utils:jar:3.0 from/to central:SSL peer shut down incorrectly -> [Help 1]

国外网站访问超时,直接下载 plexus-utils-3.0.jar 放到本地仓库:%reposity%org\codehaus\plexus\plexus-utils\3.0

下载站点:http://mvnrepository.com/artifact/org.codehaus.plexus/plexus-utils

 

2.3 Failed to read artifact descriptor for xxx:jar:1.1: Could not transfer artifact xxx:pom:1.1 from/to central (https://repo.maven.apache.org/maven2): RSA premaster secret error: SunTlsRsaPremasterSecret KeyGenerator not available

国外网站访问超时,下载相应的*.pom文件

 

2.4 myeclipse生成web项目报错:Failed to create project。java.lang.NullPointerException

这明显是插件的bug,可通过创建一般Simpe Project,选择war,在项目上右键MyEclipse然后Add Web Project Capabilities.

 

2.5 Error assembling WAR: webxml attribute is required(or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

maven的web项目默认的webroot是在src\main\webapp。如果在此目录下找不到web.xml就抛出以上的异常,可通过以下配置指定web.xml路径 

<build>
  <plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-war-plugin</artifactId>
			<version>2.1.1</version>
			<configuration>
				<webXml>./WEB-INF/web.xml</webXml>
			</configuration>
		</plugin>
	</plugins>
</build>

   

2.7 Failure to find  xxx was cached in the local repository

如果工程依赖本地库,必须安装至远程仓库才能编译,非常不便。可通过强制指定本地库路径解决:

<scope>system</scope> 
<systemPath>${basedir}/src/main/webapp/WEB-INF/lib/xxx.jar</systemPath>

 

2.8 导入报错:

An internal error occurred during: "Importing Maven projects".

java.lang.NullPointerException

解决办法:add webapp capacities

 

三. maven 使用技巧

1.去版本号

maven build 的web项目默认会添加版本号,访问时非常不便,可添加以下配置可以去除版本号:

<build>
  <finalName>mav_web</finalName>
</build>

         

2. jar主类的指定:

默认情况下生成的jar没有指定Main-Class,无法直接运行。

可通过maven-shade-plugin实现:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<version>1.2.1</version>
	<executions>
	   <execution>
		  <phase>package</phase>
		  <goals>
				<goal>shade</goal>
		  </goals>
		  <configuration>
				<transformers>
				  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
				   <mainClass>com.mav_test.App</mainClass>
				 </transformer>
				</transformers>
		  </configuration>
		</execution>
	 </executions>
</plugin>

 

缺点:该插件会将所有依赖库均打包至jar文件中,非常笨重。

 

3.maven文件打包

assembly plugin实现自定义打包

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值