本文的前提条件:
- windows7-64bit
- jdk1.8.0
- Maven-3.5.0
1- 更新Eclipse中Maven配置
1.1- 修改Eclipse根目录下eclipse.ini文件
D:DownLoadFileseclipse-java-oxygen-R-win32-x86_64eclipseeclipse.ini
在“-vm”选项下填写“C:Program FilesJavajdk1.8.0_101injavaw.exe”
-startup plugins/org.eclipse.equinox.launcher_1.4.0.v20161219-1356.jar --launcher.library plugins/org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.500.v20170531-1133 -product org.eclipse.epp.package.java.product -showsplash org.eclipse.epp.package.common --launcher.defaultAction openFile -vm C:Program FilesJavajdk1.8.0_101injavaw.exe --launcher.defaultAction openFile --launcher.appendVmargs -vmargs -Dosgi.requiredJavaVersion=1.8 -Dosgi.instance.area.default=@user.home/eclipse-workspace -XX:+UseG1GC -XX:+UseStringDeduplication -Dosgi.requiredJavaVersion=1.8 -Xms256m -Xmx1024m
1.2- 修改Eclipse的默认JRE
Windows---》Preferences---》Java ---》Installed JREs ---》Add---》Standard VM,Next---》Directory,选中JDK目录,例如C:Program FilesJavajdk1.8.0_101目录,点击Finish
在Installed JREs界面勾选新加入的选项
在Installed JREs---》Execution Environments界面勾选新加入的选项
1.3- 修改Eclipse中Maven默认配置
更改为本机的Maven
Windows---》Preferences---》Maven---》Installations---》Add---》Directory,选中D:DownLoadFilesapache-maven-3.5.0目录,点击Finish。
在Installations勾选新加入的选项,保存并关闭。
更改Maven的默认Settings
Windows---》Preferences---》Maven---》User Settings,根据需要选择使用的Settings文件。一般将Settings文件放置在对应的repo下。
更改Maven的默认仓库位置
2- 创建Maven项目
File ---> New ---> Maven Project
---> 默认勾选"Use default Workspace location",点击Next
--->选择maven-archetype-quickstart,点击Next
---> 设置必要的项目参数, 然后点击Finish创建Maven工程
- Group Id : 项目组织的唯一标识符,实际对应JAVA的包结构,一般都是组织域名的反写形式
- Artifact Id : 项目的唯一标识符,实际对应项目的名称,就是项目根目录的名称
- Version : 项目的版本
- Pacaking : 项目的打包方式,默认为jar
3- 编写pom.xml文件
根据实际需求编写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>testMaven.demo</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project>
4- 编译与安装
注意:需要引入的依赖jar包必须在仓库存在,否则会提示报错。
4.1- 编译
选中pom.xml文件,鼠标右键Run AS--->Maven build---》在弹出的界面,填写Goals内容:compile,然后Run
如果需要再次运行,可以在填写Goals内容:clean compile,然后Run
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building demo 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:Anliven-RunningenEclipseProjectsdemosrcmain esources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:Anliven-RunningenEclipseProjectsdemo argetclasses [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.201 s [INFO] Finished at: 2017-10-24T13:20:33+08:00 [INFO] Final Memory: 13M/208M [INFO] ------------------------------------------------------------------------
4.2- 安装
选中pom.xml文件,鼠标右键Run AS--->Maven install
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building demo 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:Anliven-RunningenEclipseProjectsdemosrcmain esources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:Anliven-RunningenEclipseProjectsdemosrc est esources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:Anliven-RunningenEclipseProjectsdemo arget est-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo --- [INFO] Surefire report directory: D:Anliven-RunningenEclipseProjectsdemo argetsurefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running testMaven.demo.demo.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ demo --- [INFO] Building jar: D:Anliven-RunningenEclipseProjectsdemo argetdemo-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ demo --- [INFO] Installing D:Anliven-RunningenEclipseProjectsdemo argetdemo-0.0.1-SNAPSHOT.jar to D:DownLoadFilesapache-maven-repo estMavendemodemo .0.1-SNAPSHOTdemo-0.0.1-SNAPSHOT.jar [INFO] Installing D:Anliven-RunningenEclipseProjectsdemopom.xml to D:DownLoadFilesapache-maven-repo estMavendemodemo .0.1-SNAPSHOTdemo-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.321 s [INFO] Finished at: 2017-10-24T13:21:24+08:00 [INFO] Final Memory: 17M/265M [INFO] ------------------------------------------------------------------------
4.3- 查看本地仓库
本地仓库包含下载的所有依赖包,默认位于用户目录(例如windows系统:C:Users<username>.m2
epository),可通过setting.xml文件设置。
示例:D:DownLoadFilesapache-maven-repo
<localRepository>D:DownLoadFilesapache-maven-repo</localRepository>
也可在Eclipse界面Windows---》Preferences---》Maven---》User Settings---》Local Repository,可以查看到本地仓库的位置信息。
guowli@5CG450158J MINGW64 /d/DownLoadFiles/apache-maven-repo/testMaven $ pwd /d/DownLoadFiles/apache-maven-repo/testMaven guowli@5CG450158J MINGW64 /d/DownLoadFiles/apache-maven-repo/testMaven $ ls -lR .: total 0 drwxr-xr-x 1 guowli 1049089 0 Oct 24 13:21 demo/ ./demo: total 0 drwxr-xr-x 1 guowli 1049089 0 Oct 24 13:21 demo/ ./demo/demo: total 1 drwxr-xr-x 1 guowli 1049089 0 Oct 24 13:21 0.0.1-SNAPSHOT/ -rw-r--r-- 1 guowli 1049089 278 Oct 24 13:21 maven-metadata-local.xml ./demo/demo/0.0.1-SNAPSHOT: total 13 -rw-r--r-- 1 guowli 1049089 189 Oct 24 13:21 _remote.repositories -rw-r--r-- 1 guowli 1049089 2309 Oct 24 13:21 demo-0.0.1-SNAPSHOT.jar -rw-r--r-- 1 guowli 1049089 770 Oct 24 13:17 demo-0.0.1-SNAPSHOT.pom -rw-r--r-- 1 guowli 1049089 704 Oct 24 13:21 maven-metadata-local.xml guowli@5CG450158J MINGW64 /d/DownLoadFiles/apache-maven-repo/testMaven