原文:http://www.cnzhujie.cn/?p=368


最近项目中使用到maven,通过一段时间的使用,对maven的基本使用差不多熟悉了,这边做个总结

1、下载并配置环境变量

下载地址http://maven.apache.org/download.html,之后解压到本地文件夹,比方说解压到D:Programapache-maven-3.0.4

设置环境变量:新增MAVEN_HOME,值为刚刚解压的路径:D:Programapache-maven-3.0.4。修改PATH变量,在末尾增加;%MAVEN_HOME%bin;

打开cmd,输入mvn -v查看安装是否成功(需要提前安装jdk)。

2、修改repository位置

repository目录中存放着被编译的项目所使用的jar包,此文件夹默认位置为用户目录下的.m2目录下。

可以通过修改maven目录下的conf/setting.xml修改存放位置:在<setings …></setting>内添加如下代码

<localRepository>你想存放的路径</localRepository>

3、新建maven项目

可以通过myeclipse新建maven项目。

本人使用的是myeclipse10,内置maven4myeclipse,其中内嵌了maven3.0.2。可以通过下面步骤将1中的安装的maven配置到myeclipse中:

  • windows –> preferences

  • 弹出的对话框左侧选择Myeclipse下的Maven4Myeclipse

  • 在installations中选择add,选择1中maven解压的路径:D:Programapache-maven-3.0.4

  • 在User settings中选择setting.xml位置,D:Programapache-maven-3.0.4confsetting.xml

  • 点击ok保存配置

新建普通maven项目:

  • File->New->Project…

  • 选择Maven Project

  • 不要勾选“create a simple project(…)”

  • 如果新建j2ee项目,在中间的表格中选中maven-archetype-webapp,如果中间表格没有此选择,请切换上方的Catalog试试看

  • 如果新建普通项目,选择Maven-archetype-quickstart即可

  • 至此过程建造完毕



4、编译项目

4.1在myeclispe中编译:run as选择maven bulid…,参数输入clean package表示先清除之前生成的,再重新生成

4.2在资源浏览器中编译:cmd进入到工程目录,输入mvn clean,然后再输入mvn package

5、编码问题

maven编译过程默认使用的是gbk编码,如果项目不是gbk编码,并且源码中存在中文字符(注释也算),那么编译就会产生错误,应该如何配置呢?在pom.xml的<bulide>…</bulide>内添加如下代码:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
            <encoding>UTF-8</encoding>
    </configuration>
 </plugin>

这样弄完之后会发现编译没错,但还是有warnning:

[WARNING] Usingplatformencoding(GBKactually)tocopyfilteredresources, i.e. build is platform dependent!

在pom.xml的<bulide>…</bulide>内添加如下代码即可:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <encoding>UTF-8</encoding>
    </configuration>
 </plugin>

6、icu2.6.2和tomcat7不兼容问题

工程中使用到pmd,而pmd中使用到icu4j,pmd版本是5.0,icu4j版本是2.6.1

maven编译等都没有错误,将打包好的war放入tomcat7的webapps目录,启动tomcat7的时候发现抛出一个异常(但服务器启动正常):

严重: Unable to process Jar entry [com/ibm/icu/impl/data/LocaleElements_zh__PINY

IN.class] from Jar [jar:file:/D:/Program/tomcat7/webapps/jcpa/WEB-INF/lib/icu4j-

2.6.1.jar!/] for annotations

org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in

constant pool: 60

在Google上搜索“Invalid byte tag in constant pool: 60”找到这篇文章:http://stackoverflow.com/questions/6751920/tomcat-7-servlet-3-0-invalid-byte-tag-in-constant-pool

文章中的评论说:

It may not be your issue, but mine was the same as this one — an old version of com.ibm.icu:icu4j. I solved the problem by changing by build configuration to exclude the older transitive dependencies and explicitly depending upon the latest version (4.8).

意思是说icu4j版本太低,不兼容tomcat7,只要在pom.xml中添加下面代码

<dependency>
    <groupId>com.ibm.icu</groupId>
    <artifactId>icu4j</artifactId>
    <version>3.4.4</version>
 </dependency>

这样配置之后,项目发布之后发现lib包中只有3.4.4版本,没有2.6.1版本,说明maven编译会进行处理,防止重复包含

7.生成jar包


有时候在项目中可能在原有的包基础上需要再抽取部分源码组成一个新的jar包,比如web项目中,最终打的是war包,但又可能需要部分代码生成jar包给到其他项目共享使用,此时我们就需要使用maven的jar插件了。

在pom文件中加入如下片段:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>passport-server-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>server-${project.version}</classifier>
<jarName>myjarname</jarName>
<includes>
<include>**/packagepath/**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>

使用maven-jar-plugin这个插件来进行打包,最重要的部分就是configuration段了,配置解释如下:

(1)、jarName:指定最终的jar包的名称,如果不指定这个则默认使用pom文件中定义的artifactId

(2)、classifier:分类名称,附加在jarName后面形成最终的jar文件名称

(3)、includes:指定需要打包的资源,可以指定多个。比方说如果想添加net.my.service包下的所有类添加到jar中,在include中可以这样写:**/net/my/service/*上面的写法不会包含子目录下的文件,如果需要包含可以下面这样写:**/net/my/service/**

   像上面的片段,最终的文件名称就是:myjarname-server-0.1.jar,如果pom文件中定义的版本是0.1的话。如果需要打包生成多个不同的jar包,则需要定义多个execution,将execution的id改成不同的id,并根据自己的需要进行配置