`
习惯在马桶上思考
  • 浏览: 112762 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

(转)MAVEN 使用帮助

阅读更多

虽然公司仍然使用ant,但自己还是尝试使用了下Maven2

网上资料虽然很多,还是总结出自己所需要的

一些好的文章

我的Maven之旅

http://www.blogjava.net/sitinspring/archive/2007/06/18/125006.html

http://zyl.iteye.com/blog/41754

等等

 

以下是我简单的总结

 

MAVEN 使用帮助

feigme

版本声明

JDK  5.0

Maven  2.0.9

OS  windows xp

安装

       Maven 下载地址  http://maven.apache.org/download.html

安装 Eclipse Maven2 插件  http://m2eclipse.sonatype.org/update/

       解压 Maven  D:\apache-maven-2.0.9

       设置环境变量  MAVEN_HOME = D:\apache-maven-2.0.9

       添加 Path  %MAVEN_HOME%\bin

       测试安装 

              C:\Documents and Settings\Administrator>mvn -version

Maven version: 2.0.9

Java version: 1.5.0_10

OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"

常用命令

       Maven2 的运行命令为  mvn 

常用命令为 

             mvn archetype:create :创建 Maven 项目

             mvn compile :编译源代码

             mvn test-compile :编译测试代码

             mvn test  运行应用程序中的单元测试

             mvn site  生成项目相关信息的网站

             mvn clean :清除目标目录中的生成结果

             mvn package  依据项目生成 jar 文件

             mvn install :在本地 Repository 中安装 jar

             mvn eclipse:eclipse :生成 Eclipse 项目文件

生成项目

             建一个 JAVA 项目  mvn archetype:create -DgroupId=com.demo -DartifactId=App

          建一个 web 项目  mvn archetype:create -DgroupId=com.demo -DartifactId=web-app -DarchetypeArtifactId=maven-archetype-webapp

 

生成 Eclipse 项目

普通 Eclipse 项目执行  mvn eclipse:eclipse

           Eclipse WTP 项目执行  mvn eclipse:eclipse –Dwtpversion=1.0

               wtp1.0 以上版本均可用)

 

        Eclipse 里面 file>import

       然后选择 General>Existing Projects into Workspace

       设置 maven 的路径变量 M2_REPO

Window -> Preferences -> Java -> Build Path -> Classpath Variables

New Name 命名  M2_REPO

路径 :本地的 maven 类库地址

这里为 C:/Documents and Settings/{ 你的系统登录用户名 }/.m2/repository

pom.xml 文件基本节点介绍

<project > :文件的根节点 .
<modelversion >  pom.xml 使用的对象模型版本 .
<groupId > :创建项目的组织或团体的唯一 Id.
<artifactId > :项目的唯一 Id, 可视为项目名 .
<packaging > :打包物的扩展名,一般有 JAR,WAR,EAR  
<version > :产品的版本号 .
<name > :项目的显示名,常用于 Maven 生成的文档。 
<url > :组织的站点,常用于 Maven 生成的文档。 
<description > :项目的描述,常用于 Maven 生成的文档。

 

 POM 4 中, <dependency> 中还引入了 <scope> ,它主要管理依赖的部署。

目前 <scope> 可以使用 5 个值:

compile :缺省值,适用于所有阶段,会随着项目一起发布。

provided :类似 compile ,期望 JDK 、容器或使用者会提供这个依赖。如 servlet.jar 

runtime :只在运行时使用,如 JDBC 驱动,适用运行和测试阶段。

test :只在测试时使用,用于编译和运行测试代码。不会随项目发布。

system :类似 provided ,需要显式提供包含依赖的 jar  Maven 不会在 Repository 中查找它。

 

定义自己的结构

新建一个 archetype 项目 

mvn archetype:create\ 
-DgroupId=cn.prof\
 
-DartifactId=prof-archetype\
 
-DarchetypeArtifactId=maven-archetype-archetype

       主要的模板文件  archetype-resources/pom.xml

       修改其中内容

       修改 META-INF/maven/archetype.xml 中定义了相关的 sources

       安装此项目  mvn install

执行下面的命令创建新目录的项目:

mvn archetype:create -DgroupId=com.mergere.mvnbook \ 
-DartifactId=proficio-example\
 
-DarchetypeGroupId=com.xxx.mvn\ 
-DarchetypeArtifactId=
 prof-archetype 
-DarchetypeVersion=1.0-SNAPSHOT

 

目录说明

Maven2  Directory

目录

二级目录

三级目录

四级目录

说明

pom.xml

 

Maven2 的项目设置文件

src/

 

源码目录

main/

 

项目主体目录根

java/

 

源代码目录

resources/

 

所需资源目录

filters/

 

资源过滤文件目录

assemby/

 

Assembly descriptors

config/

 

配置文件根目录

webapp/

 

web 应用目录

WEB-INF/

WEB-INF 目录

test/

 

项目测试目录根

java/

 

测试代码目录

resources/

 

测试所需资源目录

filters/

 

测试资源过滤文件目录

site/

 

site 相关的资源目录

target/

 

输出目录根

classes/

 

 

项目主体输出目录

test_classes/

 

项目测试输出目录

site/

 

项目site 输出目录

 

碰到的问题

1.        M2eclipse 插件装上之后,会报这个错

Eclipse is running in a JRE, but a JDK is required

Some Maven plugins may not work when importing projects or updating source folders.

解决办法 :查看 eclipse\readme\readme_eclipse.html 里面有详细说明,在 Selecting a workspace 这一节

创建一个快捷方式,设定目标位置为 D:\eclipse_jee\eclipse.exe -vm D:\Java\jdk1.5.0_10\bin\javaw

因为默认 vm  jdk\jre\bin\javaw ,直接改到 jdk 下,错误即可消除

 

2.       使用命令 mvn eclipse:eclipse –Dwtpversion=1.0 之后,导入的项目 Project Facet  java 还是 1.4 ,而我 Eclipse 上设置的是 5.0

解决办法 :在 pom.xml  plugins 加入说明就可以了

Xml代码 

<!--[if !supportLists]-->1.     <!--[endif]--><plugins>  

<!--[if !supportLists]-->2.     <!--[endif]-->        <plugin>  

<!--[if !supportLists]-->3.     <!--[endif]-->           <artifactId>maven-compiler-plugin</artifactId>  

<!--[if !supportLists]-->4.     <!--[endif]-->           <configuration>  

<!--[if !supportLists]-->5.     <!--[endif]-->               <source>1.5</source>  

<!--[if !supportLists]-->6.     <!--[endif]-->               <target>1.5</target>  

<!--[if !supportLists]-->7.     <!--[endif]-->           </configuration>  

<!--[if !supportLists]-->8.     <!--[endif]-->        </plugin>  

<!--[if !supportLists]-->9.     <!--[endif]--></plugins>  

 

3.   在使用命令mvn eclipse:eclipse 之后,importEclipse,然后在Eclipse项目上点右键,选择插件Maven>Enable Dependency Management后,会报重复引用依赖的错误
解决办法 :再执行mvn eclipse:eclipse然后刷新Eclipse的项目,取消掉maven插件的引用依赖,错误就会消除

 

4.   Failure executing javac, but could not parse the error:
编译器 (1.5.0_10) 中出现异常。 如果在 Bug Parade 中没有找到该错误,请在 Java Developer Connection (http://java.sun.com/webapps/bugreport )  对该错误进行归档。 请在报告中附上您的程序和以下诊断信息。谢谢您的合作。
java.nio.BufferOverflowException
解决办法 :(临时)在控制面板中将区域与语言选项中,把当地语言设为英语
(为什么我还会出现这个问题?请指教)

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics