系统版本:Mac OSX 10.11.2
Eclipse:Kepler Service Release 2
Maven:3.2.1
1. maven安装
- 官网下载maven压缩包apache-maven-3.2.1-bin.tar.gz,并解压
- 配置系统环境变量
终端
执行
vim ~/.bashrc
结尾处键入
export MAVEN_HOME=/User/dzgdp888/apache-maven-3.2.1
export PATH=${PATH}:${MAVEN_HOME}/bin
:wq保存并退出,并使环境变量立即生效
source ~/.bashrc
- 查看maven是否安装成功
~ dzgdp888$ mvn -v
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-15T01:37:52+08:00)
Maven home: /Users/dzgdp888/dev/apache-maven-3.2.1
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: zh_CN, platform encoding: EUC_CN
OS name: "mac os x", version: "10.11.2", arch: "x86_64", family: "mac"
证明maven安装成功
2. Eclipse的maven插件安装
- 打开Eclipse,选择Help->Install New SoftWare
- 点击
Add...
- 点击OK,等待安装完成
安装验证:
重启eclipse, Help --> About Eclipse --> Installation Details
在Installed Software标签中检查刚才选择的模块是否在这个列表中
检查eclipse是否已经支持创建Maven项目:
File --> New --> Other ,找到Maven一项,如果展开一切正常,说明m2eclipse已经正确安装了。
3. maven工程导入
-
Eclipse中Import选择导入已经存在的maven工程,如图
- 选择需要导入的工程,点击Finish即可
4.使用打jar包
- 不包含依赖包
终端
进入工程根目录,执行mvn打包命令
dzgdp888:~ dzgdp888$ cd Desktop/storm
dzgdp888:storm dzgdp888$ ls
README.md src target
pom.xml storm-example.iml 项目说明.txt
dzgdp888:storm dzgdp888$ mvn package
执行结果如下:
Running com.blogchong.storm.helloworld.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ storm-example ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.667 s
[INFO] Finished at: 2015-12-30T00:53:12+08:00
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------
dzgdp888:storm dzgdp888$
BUILD SUCCESS即为打包成功,打包成功后jar包路径为工程target目录下的storm-example-0.0.1-SNAPSHOT.jar
dzgdp888:storm dzgdp888$ ls target/
archive-tmp
classes
maven-archiver
storm-example-0.0.1-SNAPSHOT.jar
surefire-reports
test-classes
dzgdp888:storm dzgdp888$
- 包含依赖包
通过maven可以很方便的将依赖包一同打到jar包中,操作方法和之前的基本一致,只是mvn命令更换为
mvn assembly:assembly
打好的jar包同样存放在target文件夹下,本例中名字为storm-example-0.0.1-SNAPSHOT-jar-with-dependencies.jar
从上图可以看出,这两种方式打包后大小还是相差很大的。