1. Centos maven安装配置、使用
1.1. maven的好处
依赖管理:如果多个项目会用到相同jar包,那么不用再每个项目中复制一份jar包了,节省空间

一键构建:一个mvn命令就可以搞定
可以跨平台
提高开发效率:应用于大型项目,提高开发效率
例如有3个模块用户管理、订单管理、支付管理。那么maven可以分模块开发
互联网项目分业务来开发
传统项目 按层次开发:底层-》中间层、服务层-》界面
1.2 maven仓库 (Repository)
优先级:
优先查找本地仓库
本地找不到,则从私服仓库上找,找到后下载到本地仓库
私服上找不到,则从中央仓库查找,找到后下载到私服仓库,最后下载到本地仓库
1.3. maven的安装
maven运行依赖jdk,首先依赖于jdk的安装,安装jdk1.8和maven3.5.3请参考我的简文:https://www.jianshu.com/p/052184a7bc35
1.4. maven配置
配置文件位置:/kluter/apache-maven-3.5.3/conf/settings.xml
1.4.1 本地仓库

手动修改到你想要的位置:/kluter/localRepository

2. maven常用命令
2.1 #mvn compile
编译maven项目,生成target目录树,存放maven编译生成的class文件
2.2 #mvn clean
清除target目录及目录下的所有内容。
2.3 #mvn test
运行test目录下的测试类
2.4 #mvn package
打jar包
2.5 #mvn install
maven工程的安装命令,执行install将maven打成jar包发不到本地仓库
3. linux eclipse安装并整合maven
3.1 下载安装、运行eclipse
http://www.eclipse.org/downloads/
#tar -zxf eclipse-jee-neon-1a-linux-gtk-x86_64.tar.gz -C /kluter
#cd /kluter/eclipse
#./eclipse
修改jvm内存最值
window->preferences->java->installed jres


如果上面这种方法没有生效,则需要更改eclipse安装目录下的eclipse.ini文件中的:
-Xms1024m
-Xmx4096m
并重启。
查看jvm的值的方法:
Windows -> Preferences -> 将show heap status勾上并确定退出
在eclipse界面的右下角就会出现jvm的heapsize了
3.2 eclipse中的maven相关配置


由于之前在settings.xml中配置了本地仓库,所以选择了user settings.xml后,本地仓库自动帮我们加载好了:

3.3 重建本地仓库索引
当本地仓库jar包太多时,为了提高maven的查询速度,可以重建jar包索引



4. 使用maven创建project
4.1 创建helloworld maven project



标准maven工程的JRE Library名字是J2SE-1.5,跟使用的JRE1.8版本不同,需要修改

4.2 手动修改JRE版本号(这一步可以不做,直接见4.3)



由于修改了JRE版本还没有生效,工程报错,只需要update project即可,这样也可以达到调整JRE版本的目的


4.3 修改JRE版本号的默认值
在4.2中介绍了手动修改jdk版本,但是每次新建maven工程都需要添加,这样工作就重复了,下面介绍一次性修改默认值更方便
# vi /kluter/apache-maven-3.5.3/conf/settings.xml
移动到文件末尾(shift+gg)找到</profiles>,在这一行之前添加一个profile如下:
<profile>
<id>jdk18</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
<maven.compiler.encoding>utf-8</maven.compiler.encoding>
</properties>
</profile>
然后再做一次maven project的update, jre版本就更新了
4.4 创建package和class


之后就是再package中创建源class

尝试编辑一段hdfs demo代码,由于没有手动导jar包导致编译报错,因此需要修改maven的pom.xml文件添加依赖:

我们可以从官网中知道hdfs使用的Configuration、FileSystem、Path类需要依赖common和hdfsjar包,修改pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gamebear</groupId>
<artifactId>hadoop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hadoopTest</name>
<description> </description>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
</project>
可以看出,在pom.xml中主要是填写</dependencies></dependencies>,而一个dependencies里面每个<dependency></dependency>去apache的maven官网查询:http://mvnrepository.com
或者从maven官网查询:https://search.maven.org/
将注释掉的三行代码打开:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
至此,linux下的eclipse+maven3.5全部介绍完毕,有没有发现开发实际上很简单!
网友评论