Spring

Maven

2019-01-24  本文已影响0人  半数的年

还没有maven的时候,我们在项目添加依赖包的时候,需要自己下载添加到lib,但在项目中引入的依赖包其又依赖别的依赖包,并且可能存在版本冲突的问题。

一、认识maven

解决复杂依赖传递问题,打包项目

二、安装和配置

1、阿里镜像
<mirror>  
  <id>alimaven</id>  
  <name>aliyun maven</name>  
   <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
  <mirrorOf>central</mirrorOf>          
</mirror> 
2、profies可以配置开发、测试、生产不同环境时的配置
image.png

三、maven项目结构

image.png

1、pom.xml

(1)、groupId 域名反过来
(2)、artfactId 功能命名
(3)、version 版本号
(4)、packaging 打包方式 默认是jar
(5)、dependencyManagement

a)、只能出现在父pom
b)、统一版本号
c)、声明 (子POM里用到再引)
dependencies即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)
dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

(6)、dependency

四、生命周期

image.png

五、常用命令

六、实现plugin

七、搭建私服

<distributionManagement>
    <repository>
        <id>xiaoyuan-nexus-release</id>
        <name>Nexus Release Repository</name>
        <url>http://localhost:8081/repository/maven-releases/</url>
    </repository>
    <snapshotRepository>
        <id>xiaoyuan-nexus-snapshot</id>
        <name>Nexus Snapshot Repository</name>
        <url>http://localhost:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>
<servers>
    <server>
        <id>xiaoyuan-nexus-release</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <server>
        <id>xiaoyuan-nexus-snapshot</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
</servers>

八、自定义archetype(脚手架)

九、实现dev、test、pro不同环境下的配置

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <profiles.active>test</profiles.active>
        </properties>
    </profile>
    <profile>
        <id>pro</id>
        <properties>
            <profiles.active>pro</profiles.active>
        </properties>
    </profile>
</profiles>

<build>
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>conf/**</exclude>
                </excludes>
        </resource>
        <resource>
            <directory>src/main/resources/conf/${profiles.active}</directory>
        </resource>
    </resources>
</bulid>
上一篇下一篇

猜你喜欢

热点阅读