• Maven


    Maven项目构建工具

    • POM(项目对象模型):把项目当做一个对象

    • 核心内容是依赖管理,主要过程是项目构建,通过各种插件来产出文件

    局部setting配置文件就是在maven文件夹下(与repositroy同级),创建一个setting配置文件,到时会先读取到这一个文件.

    断言,junit包下的类

    //预期是4,如果num不是4就报错
    Assert.assertEquals(4,num);
    
    • 1
    • 2
    
    <modelVersion>4.0.0modelVersion>
    
    • 1
    • 2

    依赖管理

    依赖传递

    • 注意被引用的项目的scope,设置时不要是test模式,不然不能引用

    引用其它maven项目

    
    <scope>testscope>
    
    <dependency>
      <groupId>org.examplegroupId>
      <artifactId>MavenartifactId>
      <version>1.0-SNAPSHOTversion>
      
      
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    传递优先级

    • 路径优先
    • 声明优先:层级相同时,看父级谁优先
    • 特殊优先:后面的覆盖前面的

    排除依赖:不要你的依赖

      <version>1.0-SNAPSHOTversion>
      <exclusions>
        <exclusion>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
        exclusion>
      exclusions>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    可选依赖:不给别人用

    <version>4.13.2version>
    <optional>trueoptional>
    
    • 1
    • 2

    依赖范围

    <scope>testscope>
    
    • 1
    作用范围
    • 主程序:main包下
    • 测试程序:test包下
    • 参与打包:package范围内
    1. 默认compile全作用范围

    2. test只对测试程序有效

    3. provider:不参与打包,如servlet-api依赖,如果参与打包,会和tomcat自带的servlet-api依赖冲突

    runtime只参与打包:如jdbc的Dervier类,在主程序中从来没有用过,用的都是jdbc的驱动

    依赖范围传递性

    只有compile和runtime的直接依赖才能传递.间接依赖的作用范围为结果

    在这里插入图片描述


    生命周期

    Maven的生命周期分为三大部分clean,default(核心部分),site(报告期)

    //会在target/surefire-reports下生成一个Test.xml的测试报告
    mvn test
    
    • 1
    • 2
    • install会将项目打包安装到本地仓库去,路径在maven控制台里有个install开头的标明

    插件用法

    这个就是site期:项目构建过程中使用插件产出文件的过程

    阿帕奇·马文·贾瓦多克插件 – 用法 (apache.org)

    <plugin>
      <groupId>org.apache.maven.pluginsgroupId>
      <artifactId>maven-javadoc-pluginartifactId>
      <version>3.4.1version>
      <executions>
        <execution>
          
          <goals>
            <goal>test-jargoal>
          goals>
          
          <phase>testphase>
        execution>
      executions>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    分模块开发

    • 本质就是将把每个包分成项目,引用包资源就是引用这个项目的依赖

    聚合:项目统一构建

    
    <packaging>pompackaging>
    
    <modules>
      <module>untitledmodule>
      <module>untitled1module>
    modules>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    继承

    父项目规定好依赖的信息,子项目继承父项目,引用依赖时就不用配置如version这种信息了.

    父项目配置

    
    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.13.2version>
        dependency>
      dependencies>
    dependencyManagement>
    
    
    <build>
      
      <pluginManagement>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-javadoc-pluginartifactId>
            <version>3.4.1version>
            <executions>
              <execution>
                <goals>
                  <goal>test-jargoal>
                goals>
                <phase>testphase>
              execution>
            executions>
          plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    子项目配置

    子项目会继承父项目的插件配置

    
    <parent>
      <groupId>org.examplegroupId>
      <artifactId>MavenartifactId>
      <version>1.0-SNAPSHOTversion>
      
      <relativePath>../pom.xmlrelativePath>
    parent>
    
    
    <artifactId>untitledartifactId>
    
    
    
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <scope>testscope>
    dependency>
    
    
    <plugin>
      <groupId>org.apache.maven.pluginsgroupId>
      <artifactId>maven-javadoc-pluginartifactId>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    属性

    自定义属性

    <properties>
      
      <junit.version>4.13junit.version>
    properties>
    
    
    <version>${junit.version}version>
    
    
    <version>${version}version>
    
    
    <version>${settings.offline}version>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    读取电脑系统属性:

    控制台属性:PS F:\JAVA\Maven> mvn help:system

    会在控制台打印出系统变量和环境变量

    
    <version>${user.home}version>
    
    
    <version>${HOMEPATH}version>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    版本管理

    自定义版本号:Release是发布版,Snapshot是快照版

    <version>2.0-RELEASEversion>
    
    • 1

    资源配置

    统一管理项目需要用到的各种资源

    
    <jdbc.url>1234567jdbc.url>
    properties>
    
    
    <build>
      <resources>
        <resource>
          
          <directory>${project.basedir}/src/main/resourcesdirectory>
          
          <filtering>truefiltering>
        resource>
      resources>
    
      <testResources>
        <testResource>
          <directory>${project.basedir}/src/test/resourcesdirectory>
          <filtering>truefiltering>
        testResource>
      testResources>
    
    
      
    url=${jdbc.url}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    测试

    Properties properties = new Properties();
    try {
      properties.load(Test.class.getClassLoader().getResourceAsStream("jdbc.properties"));
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    System.out.println(properties.getProperty("url"));
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    多环境配置

    
    <profiles>
      <profile>
        
        <id>test-mvnid>
        <properties>
          <jdbc.url>098765jdbc.url>
        properties>
    
        
        <activation>
          <activeByDefault>trueactiveByDefault>
        activation>
      profile>
    profiles>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    测试时携带参数:打包项目为test-mvn

    install -P test-mvn
    
    • 1

    跳过测试

    install -D skipTests
    
    • 1

    这个是在子项目(要跳过测试的项目中)测试的

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.pluginsgroupId>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.2version>
          <configuration>
            
            
    
            
            <includes>
              <include>**/T*st.javainclude>
            includes>
            
            <excludes>
              <exclude>**/T*st.javaexclude>
            excludes>
          configuration>
        plugin>
      plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    私服

    • nexue的配置文件etc下的nexus-default.properties,默认端口号是8081,账号密码是adminadmin

    • bin目录下控制台启动: ./nexus /run nexus

    创建
    1. 在nexus的设置>repositroy中创建maven2私服.group是组, hosted是宿主仓库,proxy是代理仓库
    2. 内容栏中可以搜索,浏览和更新仓库.一般在浏览中找到自己的私服进行更新.
    3. 记得把自己的私服加到maven-public组中去

    本地连接私服

    1. 在maven的setting.xml中配置服务和镜像.也就是本地访问私服的权限
    <servers>
      >
      <server>
        <id>maven-hostedid>
        <username>adminusername>
        <password>adminpassword>
      server>
    servers>
    
    
      <mirror>
        <id>nexusid>
        <mirrorOf>*mirrorOf>
        
        
        
        
        
        <url>http://localhost:8081/repository/maven-public/url>
      mirror>
    mirrors>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    将项目发布到私服

    
    <distributionManagement>
      
      <repository>
        <id>maven-hostedid>
        <url>http://localhost:8081/repository/maven-hosted/url>
      repository>
      <snapshotRepository>
        <id>maven-hostedid>
        <url>http://localhost:8081/repository/maven-hosted/url>
      snapshotRepository>
    
    distributionManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    记得版本号要发布成上线版本

    <artifactId>untitledartifactId>
    <version>2.0-RELEASEversion>
    
    • 1
    • 2

    发布到私服

    
    <distributionManagement>
      
      <repository>
        <id>maven-hostedid>
        <url>http://localhost:8081/repository/maven-hosted/url>
      repository>
      <snapshotRepository>
        <id>maven-hostedid>
        <url>http://localhost:8081/repository/maven-hosted/url>
      snapshotRepository>
    
    distributionManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    记得版本号要发布成上线版本

    <artifactId>untitledartifactId>
    <version>2.0-RELEASEversion>
    
    • 1
    • 2
  • 相关阅读:
    java并发编程
    超简单集成华为 HMS MLKit 机器学习服务:银行卡识别 SDK,一键实现银行卡绑定
    Redis --- 安装教程
    如何全面的理解APS自动排产系统?
    (附源码)ssm失物招领系统 毕业设计 182317
    数据结构-顺序表详解(含完整代码)
    如何将普通索引转化为日期索引
    【C++】二叉搜索树
    集线器、交换机、路由器是如何转发包的
    吴恩达老师机器学习课程笔记 06 逻辑回归
  • 原文地址:https://blog.csdn.net/lm12032015/article/details/126851916