工程化-maven

本文详细介绍Maven的基础概念、优势及具体应用,包括安装配置、生命周期、版本管理等内容,并深入探讨依赖管理和自定义插件等高级特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.认识maven

2.maven的优势

3.下载安装

4.配置

5.pom介绍

6.传递依赖

7.依赖仲裁

8.生命周期

9.版本管理

10.常用命令

11.常用插件

12.自定义插件

13.profile使用

14.仓库nexus

15.archetype  模版化

16.其他


1.认识maven

仓库消除重复的构建,模板化消除重复的编码,profile使配置可选,更有丰富的版本控制与插件

 

2.maven的优势

a)约定优于配置

b)构建简单

c)插件丰富

 

3.下载安装

a)MAVEN_HOME环境变量设置 ~\apache-maven-3.5.3

a)测试是否安装成功 cmd->mvn -version

 

4.配置

全局setting.xml 

自定义setting.xml :加载顺序优于全局

localRepository:本地仓库路径

pluginGroups:自定义插件

proxies:代理,翻墙用

servers:私有仓库,需要密码的

mirrors:镜像配置,选国内

profiles:配置,开发,生产配置分开

 

1<mirror>  
2  <id>alimaven</id>  
3  <name>aliyun maven</name>  
4  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
5  <mirrorOf>central</mirrorOf>          
6</mirror>

 

5.pom介绍

超级pom.xml  :/apache-maven-3.3.3/lib/maven-model-3.3.3.jar

modelVersion:超级pom的版本

groupId:组名,一般为公司域名

artfactid:功能命名

version:版本号

packaging:jar,war,pom,maven-plugin

properties:变量配置

dependencyManagement:只能出现在父pom里面,用于做版本控制,子pom引用的时候不用写版本号

dependency

    type:默认jar

    scope 作用域,在什么阶段用,会不会打包

a)compile  编译使用   例如spring-core(用在编译,打包)

b)test 测试 例如spring-test(用在测试,不打包)

c)provided 已提供  例如 servlet(tomcat容器已提供,用在编译,不打包)

d)runtime 运行时 例如JDBC驱动实现(jdbc只实现了接口,编译期间不关心有没有实现类,用在运行时,打包)

e)system 本地一些jar,systemPath定义  例如短信jar(中央仓库并没有这个jar,但是你又想用,1install到本地 2发布到中央仓库 3 systemPath)

 

6.传递依赖

父/子 工程

compile

test

provided

runtime

compile

compile

-

-

runtime

test

test

-

-

test

provided

provided

-

provided

provided

runtime

runtime

-

-

runtime

 

7.依赖仲裁

 

当maven依赖同一个common-lib的时候出现的问题

a)最短路径原则

b)路径相同时,加载先后原则(书写顺序的先后,可能遇到api冲突问题)

c)exclusions,遇到冲突时使用排除法排除低版本的jar(例如jstl-api排除servlet-api)

    

8.生命周期

a)3个生命周期,每个生命周期多个阶段,每个阶段是由plugin goals组成的

b)A Build Lifecycle is Made Up of Phases

c)A Build Phase is Made Up of Plugin Goals

 

d)运行一个package命令,那么会按顺序从上到下依次运行validata,intialize等命令

 

 

 

9.版本管理

开发版可以重复更新,不管版本号,覆盖更新,使用开发版不安全;稳定版一个版本号只能更新一次,同个版本覆盖不了。

a)1.0-SNAPSHOT(开发版)

当遇到开发版的jar包有更新到中央仓库时,协作者需要更新到本地

方式一 把本地repository的这个包删除

方式二 运行的时候强制更新 mvn clean package -U (强制拉一次)

b)主版本好.次版本号.增量版本号-里程碑版本

1.0.0-RELEASE

 

10.常用命令

a)compile

b)clean    删除target/

c)test       test case junit/testNG

d)package 打包

e)install    把项目install到local repo

f)deploy    发本地jar发布到remote

  

11.常用插件

a)网址

https://maven.apache.org/plugins/ 

http://www.mojohaus.org/plugins.html 

b)findbugs 静态代码检查

mvn findbugs:check

c)versions 统一升级版本号

mvn versions:set -DnewVersion=1.1     

e)source 打包源代码

f)assembly 打包zip、war

g)tomcat7

                     

12.自定义插件

a)网址 https://maven.apache.org/guides/plugin/guide-java-plugin-development.html

b)<packaging>maven-plugin</packaging>

c)依赖

 1<dependency>
 2    <groupId>org.apache.maven</groupId>
 3    <artifactId>maven-plugin-api</artifactId>
 4    <version>3.5.0</version>
 5</dependency>
 6<dependency>
 7    <groupId>org.apache.maven.plugin-tools</groupId>
 8    <artifactId>maven-plugin-annotations</artifactId>
 9    <version>3.5</version>
10    <scope>provided</scope>
11</dependency>

d)新建类继承 AbstractMojo          

 

e)插件install到本地

f)新工程去执行插件

g)args参数传递

mvn -install -Dargs123

                      

13.profile使用

a)使用场景 dev/pro/test

 1配置两个profile
 2<profile>
 3    <id>dev</id>
 4    <properties>
 5        <profiles.avtive>dev</profiles.avtive>
 6    </properties>
 7    <activation>
 8        <activeByDefault>true</activeByDefault>
 9    </activation>
10</profile>
11<profile>
12    <id>pro</id>
13    <properties>
14        <profiles.avtive>pro</profiles.avtive>
15    </properties>
16</profile>
17定义规则
18<build>
19    <resources>
20        <resource>
21            <directory>${basedir}/src/main/resources</directory>
22            <excludes>
23                <exclude>conf/**</exclude>
24            </excludes>
25        </resource>
26        <resource>
27            <directory>src/main/resources/conf/${profiles.avtive}</directory>
28        </resource>
29    </resources>
30</build>

 

目录结构

 

执行 mvn clean install -P pro

 

b)setting.xml 的profile                 

公司的maven如果是内网连接,那么到家里就连不上了,这时候可定义两个profile连接不同的仓库 

 

14.仓库nexus

a)官网 https://help.sonatype.com/repomanager3

b)下载

c)安装  windwos  进入bin ./nexus.exe /run

d)访问地址 http://127.0.0.1:8081/nexus    admin/admin123

e)发布到仓库配置

 1//pom配置
 2<distributionManagement>
 3    <repository>
 4        <id>nexus-releases</id>
 5        <name>releases</name>
 6        <url>http://127.0.0.1:8081/repository/maven-releases/</url>
 7    </repository>
 8    <snapshotRepository>
 9        <id>nexus-snapshots</id>
10        <name>snapshots</name>
11        <url>http://127.0.0.1:8081/repository/maven-snapshots/</url>
12    </snapshotRepository>
13</distributionManagement>
14//setting配置(id与pom的id要对应)
15<server>  
16    <id>nexus-releases</id>  
17    <username>admin</username>  
18    <password>admin123</password>  
19</server>
20<server>  
21    <id>nexus-snapshots</id>  
22    <username>admin</username>  
23    <password>admin123</password>  
24</server>
1    

 

f)从仓库下载配置

配置mirror

配置profile

 

g)用户角色权限

使用场景,如果两个部门的人不同工程使用了同一个artifactId,那么会被覆盖,这时候需要配置一下,不同角色传到不同仓库

 

h)定时任务 Tasks配置:如配置定时更新索引

 

15.archetype  模版化

a)应用场景:可以是module的模板,生成快速开发脚手架

b)生成一个archetype

到你要生成的工程下执行命令

i.mvn archetype:create-from-project 生成一个模板

ii.cd /target/generated-sources/archetype 生成模板的位置

iii.mvn install 把模板映射到本地仓库

 

c)从archetype创建项目

从本地仓库选择模板生成 mvn archetype:generate -DarchetypeCatalog=local

 

16.其他

1)mvn dependency:tree >d.txt 树日志

2)mvn help:system 系统变量

3)传到maven仓库(server-interface)run as->bulider ->deploy -e(skip test)

4)mvn映射jar包到本地

mvn install:install-file -Dfile=I:\maven-repository\jar\taobao-sdk-java.jar -DgroupId=com.tbk.api -DartifactId=tbkapi -Dversion=0.0.1 -Dpackaging=jar

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值