一、maven 声明周期
声明周期的概念与意义
在项目构建时通常会包含清理、编译、测试、打包、验证、部署、文档生成等步骤,maven 统一对其进行了整理,抽象成三个生命周期(lifecycle)及各自对应的阶段(phase)。这么做的意义是:
在执行项目构建阶段时可以采用 jar 方式构建也可以采用 war 包方式构建,提高了灵活性。我们可以通过命令 mvn ${phase name}直接触发指定阶段的执行
如:
#执行清理 phase
mvn clean
#执行编译 phase
mvn compile
#同时执行清理、编译
mvn clean compile
maven 三大声明周期与其对应的阶段(phase)
maven 总共包含三大生生命周期
clean Lifecycle 清理生命周期,用于于清理项目
default Lifecycle 默认生命周期,用于编译、打包、测试、部署等
site Lifecycle 站点文档生成,用于构建站点文档
生命周期与对应的阶段详细表格可参考我的百家号文章
https://baijiahao.baidu.com/builder/preview/s?id=1704047895433370912
三大生命周期其相互独立执行,也可以合在一起执行。但 lifecycle 中的 phase 是有严格执行顺序的,比如必须是先执行完 compile 才能执行 package 动作,此外 phase 还有包含逻辑,即当你执行一个 phase 是,其前面的 phase 会自动执行。
#执行编译
mvn compile
#执行打包就包含了编译的执行
mvn package
声明周期与插件的关系
声明周期的 phase 组成了项目构建的完成过程,但这些过程具体由谁来实现呢?
这就是插件,maven 的核心部分代码量其实很少,其大部分实现都是由插件来完成的。
比如:test 阶段就是由 maven-surefire-plugin 实现。在 pom.xml 中我们可以指定插件目标(goal)与 phase 绑定,也就是插件在什么阶段执行,当项目构建到达指定 phase 时就会触发这些插件 goal 的执行。
一个插件有时会实现多个 phase 比如:maven-compiler-plugin 插件分别实现了 compile 和 testCompile。
总结:
生命周期的阶段可以绑定具体的插件及目标
不同配置下同一个阶段可以对应多个插件和目标
phase->plugin->goal
声明周期与插件的默认绑定
在我们的项目中并没有配置 maven-compiler-plugin 插件,但当我们执行 compile 命令时一样能够执行编译操作,原因是 maven 默认为指定阶段绑定了插件实现。
以下两个操作在一定程度上是等价的:
mvn compile
#直接执行 compile 插件目标
mvn org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
clean Lifecycle 默认绑定
<phases><phase>pre-clean</phase><phase>clean</phase><phase>post-clean</phase></phases><default-phases><clean>org.apache.maven.plugins:maven-clean-plugin:2.5:clean</clean></default-phases>
复制代码
site Lifecycle 默认绑定
<phases><phase>pre-site</phase><phase>site</phase><phase>post-site</phase><phase>site-deploy</phase></phases><default-phases><site>org.apache.maven.plugins:maven-site-plugin:3.3:site</site><site-deploy>org.apache.maven.plugins:maven-site-plugin:3.3:deploy</site-deploy></default-phases>
复制代码
Default Lifecycle JAR 默认绑定
注:不同的项目类型 其默认绑定是不同的,这里只指列举了 packaging 为 jar 的默认绑定
全部的默认绑定参见:https://maven.apache.org/ref/3.5.4/maven-core/default-bindings.html#
<phases><process-resources>org.apache.maven.plugins:maven-resources-plugin:2.6:resources</process-resources><compile>org.apache.maven.plugins:maven-compiler-plugin:3.1:compile</compile><process-test-resources>org.apache.maven.plugins:maven-resources-plugin:2.6:testResources</process-test-resources><test-compile>org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile</test-compile><test>org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test</test><package>org.apache.maven.plugins:maven-jar-plugin:2.4:jar</package><install>org.apache.maven.plugins:maven-install-plugin:2.4:install</install><deploy>org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy</deploy></phases>
复制代码
二、maven 自定义插件开发
maven 插件相关概念
插件坐标定位:
插件与普通 jar 包一样包含组件坐标定位属性即:
groupId、artifactId、version,当使用该插件时会从本地仓库中搜索,如果没有即从远程仓库下载。
<!-- 唯一定位到dependency 插件 --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>2.10</version>
复制代码
插件执行 execution:
execution 配置一组指示插件如何执行的属性:
id 执行器命名
phase 什么阶段执行
goals 执行一组什么目标或功能
configuration 执行目标所需的配置文件
# 将插件依赖拷贝到指定目录<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> <excludeTransitive>true</excludeTransitive> </configuration> </execution> </executions></plugin>
复制代码
常用插件的使用
除了通过配置的方式使用插件外,maven 也提供了通过命令直接调用插件目标。
其命令格式如下:
mvn groupId:artifactId:version:goal -D{参数名}
#展示 pom 的依赖关系树
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:tree
#也可以使用简化版的命令,但前提必须是 maven 官方插件
mvn dependency:tree
其他常用插件:
#查看 pom 文件的最终配置
mvn help:effective-pom
#原型项目生成
archetype:generate
#快速创建一个 web 程序
mvn archetype:generate -DgroupId=demo -DartifactId=simple-webbapp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
#快速创建一个 java 项目
mvn archetype:generate -DgroupId=demo -DartifactId=simple-java -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
开发一个自定义插件
实现步骤:
插件 pom 配置:
<?xml version="1.0" encoding="UTF-8"?><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>bianchengjieji</groupId> <version>1.0.SNAPSHOT</version> <artifactId>bianchengjieji-maven-plugin</artifactId> <packaging>maven-plugin</packaging> <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>org.apache.maven.plugin-tools</groupId> <artifactId>maven-plugin-annotations</artifactId> <version>3.4</version> </dependency> </dependencies></project>
复制代码
插件实现类:
package com.bianchengjieji.maven;
import javafx.beans.DefaultProperty;import org.apache.maven.plugin.AbstractMojo;import org.apache.maven.plugin.MojoExecutionException;import org.apache.maven.plugin.MojoFailureException;import org.apache.maven.plugins.annotations.LifecyclePhase;import org.apache.maven.plugins.annotations.Mojo;import org.apache.maven.plugins.annotations.Parameter;
/** * @author Tommy * Created by Tommy on 2018/8/8 **/@Mojo(name = "bianchengjieji")public class BianchengjiejiPlugin extends AbstractMojo { @Parameter String sex;
@Parameter String describe;
public void execute() throws MojoExecutionException, MojoFailureException { getLog().info(String.format("小明 sex=%s describe=%s",sex,describe)); }}
复制代码
三、nexus 私服搭建与核心功能
私服使用场景
私服使用场景如下:
公司不能连接公网,可以用一个私服务来统一连接
公司内部 jar 组件的共享
nexus 下载安装
nexus 下载地址:
https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.5-02-bundle.tar.gz
解压并设置环境变量
#解压
shell>tar -zxvf nexus-2.14.5-02-bundle.tar.gz
#在环境变量当中设置启动用户
shell> vim /etc/profile
#添加 profile 文件。安全起见不建议使用 root 用户,如果使用其它用户需要加相应权限
export RUN_AS_USER=root
#加载配置文件
shell>source /etc/profile
配置启动参数
cd nexus-2.14.9-01/
cd conf/
vim nexus.properties
增加 application-port=9999
#启动
cd ${nexusBase}/bin/
./nexus start
#停止
./nexus stop
登录 nexus 界面
地址:http://{ip}:9999/nexus/
用户名:admin
密码:admin123
nexus 仓库介绍
点击Repositories
3rd party:第三方仓库
Apache Snapshots:apache 快照仓库
Central: maven 中央仓库
Releases:私有发布版本仓库
Snapshots:私有 快照版本仓库
本地远程仓库配置
在 pom.xml 中配置远程仓库
<repositories> <repository> <id>nexus-public</id> <name>my nexus repository</name><url>http://{ip}:9999/nexus/content/groups/public/</url> </repository></repositories>
复制代码
或者在 settings.xml 文件中配置远程仓库镜像 效果一样,但作用范围广了
<mirror><id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name><url>http://192.168.0.147:9999/nexus/content/groups/public/</url></mirror>
复制代码
发布项目至 nexus 远程仓库
在 pom.xml 中配置仓库地址
<distributionManagement> <repository> <id>nexus-release</id> <name>nexus release</name> <url>http://192.168.0.147:9999/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>nexus-snapshot</id> <name>nexus snapshot</name> <url>http://192.168.0.147:9999/nexus/content/repositories/snapshots/</url> </snapshotRepository></distributionManagement>
复制代码
在 setting.xml 中设置 server
<server><id>nexus-snapshot</id><username>deployment</username><password>deployment123</password></server><server><id>nexus-release</id><username>deployment</username><password>deployment123</password></server>
复制代码
#执行 deploy 命令
mvn deploy
评论