SpringBoot系列: Eclipse+Maven环境准备

这个链接比我写得更全面, http://tengj.top/2018/01/01/maven/

 

=============================
20190115补充: maven 的一些插件
=============================
maven-checkstyle-plugin 检查代码规范的插件
jacoco-maven-plugin 测试覆盖率
maven-surefire-report-plugin 生成单元测试html报告
spotbugs-maven-plugin maven中集成Spotbugs(FindBugs)工具
dependency-check-maven 它是 org.owasp 开发的用来检查依赖库中是否存在已公开的漏洞.


=============================
apache maven 配置
=============================
apache maven 官网下载并解压至 D:\my_program\apache-maven-3.5.3\

新建环境变量 M2_HOME 和 MAVEN_HOME, 都指向 D:\my_program\apache-maven-3.5.3\ , 其中 M2_HOME 是 Maven V2 以上版本使用的环境变量, 而 MAVEN_HOME 是 MAVEN V1的环境变量, 一般情况下设置M2_HOME就够了.

将下面路径加到 Path 环境变量中.
D:\my_program\apache-maven-3.5.3\bin


-----------------------
$M2_HOME/conf/settings.xml 配置文件
-----------------------

默认情况下 maven会在 ${user.home}/.m2/repository 目录中存放 本地的 jar 文件, 该目录会占用较大的空间, 如果要修改jar存放的地址, 可以修改 $M2_HOME/conf/settings.xml 文件的 localRepository tag, 比如修改为:
<localRepository>d:/.m2/repository</localRepository>

maven官方的repository 访问很慢, 可以使用阿里云镜像, 修改 $M2_HOME/conf/settings.xml 文件, 修改 mirror tag 为:

<mirror>
    <!--This sends everything else to /public -->
    <id>nexus-aliyun</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror> 

 

=============================
Eclipse 上的配置
=============================
我使用的 spring tool suite版本的eclipse, 已经包含了 Maven Integration for Eclipse 插件.

Eclipse 上配置如下:

Eclipse 的菜单 Window -> Preference -> Maven
在Maven 配置Installation tab 页中新增一个maven 安装, 将 $M2_HOME 的实际值配进去. 本例为 D:\my_program\apache-maven-3.5.3 路径名.
在Maven 配置 User settings tab 页将 $M2_HOME/conf/settings.xml 的实际值配进去, 本例为 D:\my_program\apache-maven-3.5.3\conf\settings.xml 文件路径.


=============================
Eclipse 创建 maven 项目
=============================
创建 maven 项目, 如果要创建一个最简的 maven 项目, 在新建maven 向导中, 选择 quickstart 这个 archetype, 这个 java 项目会自动生成一个 pom.xml 文件, 该文件即是 maven 管理的入口文件.


接下来用eclipse editor打开pom.xml 文件, 然后点击 Run as菜单/ maven build... , 即可使用maven管理项目, 在 maven build 的界面上, 可以输入指定的 goals, 完成特定任务. 比如:

在 Goals 输入: clean package , 先清空编译文件, 然后打包项目成jar
在 Goals 输入 compile , 进行 compile.
下面是一些常用的 maven 命令, 这些命令可以单独或组合作为maven build的goals, 组合的时候两个命令之间用空格隔开即可.

mvn clean package -Dmaven.test.skip=true 命令, 排除测试代码然后打包.
mvn clean 命令, 将 mavendemo\target 目录的编译文件清空.
mvn validate 命令, 验证工程是否正确,所有需要的资源是否可用.
mvn compile 命令, 编译 mavendemo 工程.
mvn package 命令, 将 mavendemo 工程打包成jar,比如生成 mavendemo-0.0.1-SNAPSHOT.jar
mvn install 命令, 将 mavendemo 工程打包成jar, 并发布到本地仓库中. install命令会自动先执行 validate/compile/package命令
mvn deploy 命令, 发布到远程的repository
mvn verify 命令, 验证打包的正确性
mvn test 命令, 使用单元测试工具运行测试用例.

mvn clean package exec:java 命令, 完成clean和package以及执行一条龙
mvn clean package -DskipTests 命令, 完成clean和package, 但不执行单元测试

mvn dependency:purge-local-repository 命令, 有时候本地repo的cache 的jar被损坏, 可用该命令修复本地repo的jar包(Eclipse中代码总是报 The type 某个类 cannot be resolved, 使用本命令修复本地repo的jar包一般即可解决)
mvn dependency:tree 命令, 列出项目的jar依赖关系

 

若 Maven 中心库中, 没有我们需要的 jar, 可使用 mvn 命令将本地的 jar 文件, 发布到 repository 中.
1. 发布到本机 repo 的命令为:
mvn install:install-file "-DgroupId=com.oracle" "-DartifactId=ojdbc14" "-Dversion=10.2.0.4.0" "-Dpackaging=jar" "-Dfile=D:\local_jars\ojdbc14.jar"
2. 发布到远端的 repo 命令为:
mvn deploy:deploy-file

 

=============================
pom.xml中的版本号和依赖scope的说明
=============================
不管是我们项目还是依赖项都可以指定版本信息, 比如下面:
<version>0.0.1-SNAPSHOT</version>
版本号采用三个数字标注, 后面往往还标注一个发布阶段, 一般有几种发布阶段, 他们是:
SNAPSHOT, 表示开发中的版本, 会修改bug和添加新功能.
M1或M2等表示是一个里程碑, 即将发布.
RC(Release candidate)发布候选版.
GA(General availability)基本可用版.
RELEASE, 表示是一个正式的发布版.
发布的完整阶段是: SNAPSHOT -> M1 -> M2 ... -> RC -> GA -> Release

 

pom.xml 文件中可以定义很多个依赖项, 其中有个 scope tag, 代表本类库和目标项目之间的关系, 如下面的junit依赖项,

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

scope tag 有几种取值, 含义分别是:
compile, 默认取值, 也即是说编译和打包需要该依赖库.
test, 表示仅仅在单元测试时需要该类库.
provided, 表示在编译阶段需要该类库, 但打包时不需要, 因为这个类库在目标环境中已经提供了. 比如 servlet-api jar(tomcat等都提供).
runtime, 表示在编译和打包的时候都不需要, 但在运行的时候需要, 比如某个jdbc驱动类库.


=============================
spring-boot-maven-plugin build 插件
=============================
pom.xml 文件添加spring-boot-maven-plugin build 插件, 该插件将为maven增加几个goals, 它们是:
spring-boot:run runs your Spring Boot application.
spring-boot:repackage repackages your jar/war to be executable.
spring-boot:start and spring-boot:stop to manage the lifecycle of your Spring Boot application (i.e. for integration tests).
spring-boot:build-info generates build information that can be used by the Actuator.
另外, 一旦使用了该插件, mvn package 会将项目打包为 fatjar 形式, 简化了项目部署.

pom.xml增加该插件的代码示例, 需要增加 starter-parent 作为 parent. 

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

 


=============================
参考
=============================
使用教程: http://www.vogella.com/tutorials/EclipseMaven/article.html
关于Maven依赖详解, 参考https://www.cnblogs.com/AlanLee/p/6187843.html

 

posted @ 2018-05-29 22:43  harrychinese  阅读(764)  评论(0编辑  收藏  举报