JAVA-第九部分-Maven

192 阅读2分钟

Maven

  • Maven仓库
  • 快速查找jar包,形成依赖xml语言
  • 项目管理工具,依赖管理,构建生命周期/阶段
  • POM project Object Model,项目对象模型
  • 仓库,存储资源和各种jar包
  • 坐标,Maven中的仓库位置
  • groupId定义当前Maven项目隶属的组织名称
  • artifactId定义当前Maven项目名称
  • version定义当前项目版本号
  • pom.xml文件依赖示例
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>
  • war类型为webapp,jar类型为默认java
  • 项目构建生命周期 compile->test-compile->test->package->install

依赖

  • 路径优先,层级越深,优先级越低
  • 声明优先,相同层级,配置顺序靠前的覆盖顺序靠后的
  • 特殊优先,相同资源的不同版本,后配置的覆盖先配置的
  • 可选依赖,隐藏依赖
<optional>true</optional>
  • 排除依赖,主动断开
<exclusions>
  <exclusion>
    <groupId>juint</groupId>
    <artifactId>junit</artifactId>
  </exclusion>
</exclusions>
  • 依赖范围
<scope>test</scope>
scope主代码测试代码打包
compile(默认)YYY
testY
providedYY
runtimeY

Maven生命周期

  • clean 清理工作
  • default 核心工作,例如编译,测试,打包,部署
  • site 产生报告,发布站点
  • 插件,与生命周期内的阶段绑定,在执行到对应生命周期时,执行对应的插件功能
<!--构建-->
<build>
    <!--插件-->
    <plugins>
        <!--具体插件-->
        <plugin>
            <!--打包插件-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    //目标打包文件
                    <goals>
                        <goal>jar</goal>
                        <goal>test-jar</goal>
                    </goals>
                    //运行时期
                    <phase>generate-test-resources</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

构建管理

  • 打包形式为pom <packaging>pom</packaging>
  • 用专门的模块,管理其他模块的更新
  • 调用顺序根据依赖关系执行
<modules>
<module>模块路径<modeule>
</modules>

依赖继承

  • 声明依赖管理,所有依赖全部放在这里面,管理其他模块的依赖版本
<dependencyManagement></dependencyManagement>
  • 插件管理
<pluginManagement></pluginManagement>
  • 被管理的工程定义父工程
<parent>
    <>文件定位<>
    <relativePath>父工程的pom文件</relativePath>
</parent>

属性

  • 统一版本,管理配置文件的内容
<properties>
    <propertiy>
        <!--自定义属性-->
        <spring.version></spring.version>
    </propertiy>
</properties>
//使用
${junit.version}
  • build里添加配置文件
<resources>
    <resource>
        <directory>资源文件夹路径</directory>
        <filtering>true</filtering>
    </resource>
</resources>

多环境配置

//环境
<profiles>
    //一个环境
    <profile>
        //环境名
        <id>pro_env</id>
        //环境配置
        <properties>
            <junit.version>4.12</junit.version>
        </properties>
        //默认启动
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>
//调用
install -P 环境名

跳过测试

  • install -D skipTests
  • maven窗口的Toogle闪电按钮
  • pom文件
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>
  • 指定测试
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <includes>
            <include>**/测试类名</include>
        </includes>
        <excludes>
            <exclude></exclude>
        </excludes>
    </configuration>
</plugin>

Nexus私服

  • 下载链接
  • idea通过本地仓库与私服建立连接
  • 发布到私服上
<distributionManagement>
    <repository>
        <id>仓库</id>
        <url>地址</url>
    </repository>
</distributionManagement>