工程化之MAVEN

Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Dependency Management System),和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。当你使用Maven的时候,你用一个明确定义的项目对象模型来描述你的项目,然后Maven可以应用横切的逻辑,这些逻辑来自一组共享的(或者自定义的)插件。
1.    优势
a)    约定优于配置
b)    简单
c)    测试支持
d)    构建简单
e)    CI
f)     插件丰富
2.    下载 
a)    https://maven.apache.org/download.cgi
b)    安装
c)    maven-model-builder-3.3.9.jar/org/apache/maven/model 
d)    配置  MVM_HOME
       Windows  path
       Linux  .bash_profile

配置setting.xml

maven加载setting.xml的顺序是从用户目录中的~/.m2/setting.xml再到maven的安装目录~/conf/setting.xml加载的。

maven仓库的默认地址是${user.home}/.m2/repository,我们也可以自己配置一个本地仓库 

从maven默认的远程仓库下载jar包的速度有点慢,我们可以配置国内的一些maven仓库

<mirror>  
    <id>alimaven</id>  
    <name>aliyun maven</name>  
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
    <mirrorOf>central</mirrorOf>          
    </mirror> 
    <mirror>
    <id>ui</id>
    <mirrorOf>central</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>http://uk.maven.org/maven2/</url>
    </mirror>
    <mirror>
    <id>osc</id>
    <mirrorOf>central</mirrorOf>
    <url>http://maven.oschina.net/content/groups/public/</url>
    </mirror>
    <mirror>
    <id>osc_thirdparty</id>
    <mirrorOf>thirdparty</mirrorOf>
    <url>http://maven.oschina.net/content/repositories/thirdparty/</url>
</mirror>

3.搭建一个单模块的maven项目
a)    pom.xml
i.    groupId   com.xxx.xxx
ii.    artfactId 功能命名
iii.    version 版本号
iv.    packaging  打包方式 默认是jar
v.    dependencyManagement(在多模块中使用)
       只能出现在父pom
       统一版本号
       声明 (子POM里用到再引)

vi.    Dependency
       Type  默认jar
       scope
             compile  编译   例如spring-core
             test 测试
             provided编译  例如 servlet
            runtime运行时 例如JDBC驱动实现
            system 本地一些jar  例如短信jar
       依赖传递
       第一列表示直接依赖的scope,第一行表示间接依赖的scope
 

 compile    testprovided    runtime    
compile    compile    --runtime    
testtest--test
provided    provided    -provided    provided    
runtime    runtime    --runtime    

g)    依赖仲裁
i.    最短路径原则
ii.    加载先后原则
h)    exclusions
i.    排除包
 
vii.    生命周期 lifecycle/phase/goal
       A Build Lifecycle is Made Up of Phases
       A Build Phase is Made Up of Plugin Goals

4.版本管理
a)    1.0-SNAPSHOT(SNAPSHOT代表不稳定版本)
i.    repository 删除
ii.    mvn clean package -U (强制拉一次)
b)    主版本号.次版本号.增量版本号-<里程碑版本>
1.0.0-RELAESE(RELAESE代表稳定版本)

5.常用命令
a)    compile
b)    clean    删除target/
c)    test       test case junit/testNG
d)    package 打包
e)    install    把项目install到local repo
f)     deploy    发本地jar发布到remote

6.插件
      常用插件
i.    https://maven.apache.org/plugins/ 
ii.    http://www.mojohaus.org/plugins.html 
iii.    findbugs 静态代码检查
iv.    versions 统一升级版本号
1.    mvn versions:set -DnewVersion=1.1
v.    source 打包源代码
vi.    assembly 打包zip、war
vii.   tomcat7

       自定义插件 https://maven.apache.org/guides/plugin/guide-java-plugin-development.html
a)    <packaging>maven-plugin</packaging>
b)    extends AbstractMojo 

7.    Profile
a)    使用场景 dev/test/pro(切换环境)  mvn install -P test 这样就能切换到test环境的配置

    <build>
        <resources>
            <resource><directory>src/main/resources/${profiles.active</directory</resource>
        </resources>
    </build>
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <project.active>dev</project.active>
            </properties>
            <activation><activeByDefault>true</activeByDefault></activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <project.active>test</project.active>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <project.active>pro</project.active>
            </properties>
        </profile>
      </profiles>


b)    setting.xml 家和公司两套(配置两个profile)这样修改setting.xml就可以切换环境了


8.    仓库(私服)
a)    下载
b)    安装 解压
c)    使用http://books.sonatype.com/nexus-book/reference3/index.html 
i.    http://192.168.1.6:8081/nexus
ii.    admin/admin123
d)    发布
i.    pom.xml 配置 

setting.xml配置


e)    下载jar配置
i.    配置mirror
ii.    Profile
9.    archetype  模版化(生成一个脚手架用于快速创建项目)
a)    生成一个archetype
i.    mvn archetype:create-from-project
ii.    cd /target/generated-sources/archetype
iii.    mvn install
b)    从archetype创建项目 mvn archetype:generate -DarchetypeCatalog=local

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值