• Apache Maven系列【1】settings.xml文件配置和mvn命令大全


    1. settings.xml文件配置

    1.1 mirrors配置

    配置maven central的国内镜像地址

      <mirrors>
    	
    	<mirror>
          <id>alimaven</id>
          <name>aliyun maven</name>
          <!-- 拦截maven central仓库的包下载 -->
          <mirrorOf>central</mirrorOf>
          <url>https://maven.aliyun.com/repository/central</url>
        </mirror>
    
      </mirrors>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    默认会从maven的中央仓库https://repo1.maven.org/maven2/进行依赖包拉取,其id为central。这里配置mirrorOf表示拦截从id为central的仓库进行依赖包拉取,改为从https://maven.aliyun.com/repository/central进行依赖包拉取,id和name自定义

    可以配置多个mirrorOf指向central的mirror,只有第一个连接不上,才会使用第二个。如果第一个连接上了,但没有该依赖包,也不会使用第二个

    1.2 配置Maven工程的默认JDK版本

    如果默认的JDK不是我们想要的,可以参考如下方式进行配置

      <profiles>
      
        <profile>
          <id>jdk-11</id>
          <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>11</jdk>
          </activation>
          <properties>
            <maven.compiler.source>11</maven.compiler.source>
            <maven.compiler.target>11</maven.compiler.target>
            <maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
          </properties>
        </profile>
    	
      </profiles>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2. mvn命令大全

    2.1 mvn archetype:generate-通过模板创建项目

    方式一:通过maven的插件archetype进行交互式的generate

    C:\Users\dell\Desktop>mvn archetype:generate
    ......省略部分......
    Choose archetype:
    1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.)
    2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.)
    3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
    4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site.
          This archetype can be layered upon an existing Maven plugin project.)
    5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.)
    6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
    7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)
    8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates
          some of the supported document types like APT, XDoc, and FML and demonstrates how
          to i18n your site. This archetype can be layered upon an existing Maven project.)
    9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.)
    10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.)
    Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7: 7
    Define value for property 'groupId': com.hh
    Define value for property 'artifactId': maven-learn
    Define value for property 'version' 1.0-SNAPSHOT: : 0.1
    Define value for property 'package' com.hh: : com.hh
    Confirm properties configuration:
    groupId: com.hh
    artifactId: maven-learn
    version: 0.1
    package: com.hh
     Y: : y
    ......省略部分......
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  01:33 min
    [INFO] Finished at: 2022-06-23T16:35:15+08:00
    [INFO] ------------------------------------------------------------------------
    
    C:\Users\dell\Desktop>
    
    • 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
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    说明如下:

    • 这里选择了模板7,为maven-archetype-quickstart
    • pom.xml种junit版本为3.8.1,可以改成最新的4.13.2版本
    • 自动生成的App.java和AppTest.java可以删除

    pom(Project Object Model),即项目对象模型。自动生成的pom.xml解读

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <!-- 固定是4.0.0,代表当前pom.xml使用的标签结构 -->
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.hh</groupId>
      <artifactId>maven-learn</artifactId>
      <version>0.1</version>
      <!-- 打包方式 -->
      <!-- pom:表示这个是“管理其他module”的工程 -->
      <!-- jar:生成jar包,表示这是一个Java工程 -->
      <packaging>jar</packaging>
    
      <name>maven-learn</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <!-- 工程在构建过程中读取源码时使用的字符集 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.13.2</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
    
    
    • 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
    • 30
    • 31

    方式二:通过maven的插件archetype进行非交互式的generate

    C:\Users\dell\Desktop>mvn archetype:generate -DarchetypeGroupId=org.apache.flink -DarchetypeArtifactId=flink-quickstart-java -DarchetypeVersion=1.15.0 -DgroupId=com.hh -DartifactId=flink-test -Dversion=0.1 -Dpackage=my-flink -DinteractiveMode=false
    C:\Users\dell\Desktop>
    
    • 1
    • 2

    根据指导的模板,生成我们自己的项目

    2.2 mvn compile-编译项目

    默认使用3.1版本的maven-compiler-plugin插件

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.10.1</version>
            <configuration>
              <maven.compiler.source>11</maven.compiler.source>
              <maven.compiler.target>11</maven.compiler.target>
            </configuration>
          </plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    主程序编译命令

    C:\Users\dell\Desktop\maven-learn>mvn clean compile
    
    • 1

    编译后的文件位于target/classes目录下

    测试程序编译命令

    C:\Users\dell\Desktop\maven-learn>mvn clean test-compile
    
    • 1

    编译后的文件位于target/test-classes目录下

    2.3 mvn test-测试操作

    编写一个java测试程序。然后执行

    C:\Users\dell\Desktop\maven-learn> mvn clean test
    ......省略部分......
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.hh.Test
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.083 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  5.573 s
    [INFO] Finished at: 2022-06-23T17:42:16+08:00
    [INFO] ------------------------------------------------------------------------
    
    C:\Users\dell\Desktop\maven-learn>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    测试报告存放在target/surefire-reports目录下

    2.4 mvn install-安装操作

    会对项目进行打包,同时将jar包保存到本地的repository仓库中

    C:\Users\dell\Desktop\maven-learn>mvn clean install
    
    • 1

    2.5 mvn-dependency-查看依赖

    列表形式查看所有dependency,所有依赖没有层级关系

    C:\Users\dell\Desktop\maven-learn>mvn dependency:list
    .....省略部分......
    [INFO] The following files have been resolved:
    [INFO]    junit:junit:jar:4.13.2:test
    [INFO]    org.hamcrest:hamcrest-core:jar:1.3:test
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    .....省略部分......
    C:\Users\dell\Desktop\maven-learn>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    树形结构查看所有dependency,所有依赖有层级关系

    C:\Users\dell\Desktop\maven-learn>mvn dependency:tree
    .....省略部分......
    [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ maven-learn ---
    [INFO] com.hh:maven-learn:jar:0.1
    [INFO] \- junit:junit:jar:4.13.2:test
    [INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    .....省略部分......
    C:\Users\dell\Desktop\maven-learn>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    【JAVA学习笔记】67 - 坦克大战1.5 - 1.6,防止重叠,记录成绩,选择是否开新游戏或上局游戏,播放游戏音乐
    ThreadLocal(1):ThreadLocal介绍
    权限系统设计学习总结(5)—— 权限系统设计全面总结
    cartographer 编译遇到 abseil的问题
    uni-app攻略:如何对接驰腾打印机
    linux排查java进程占用CPU过高原因方法
    Jsonp跨域的坑,关于jsonp你真的了解吗
    MySQL和Oracle JDBC驱动包下载步骤
    vue中报 TypeError: Assignment to constant variable.
    【JULY-1】DAY 1 二分查找
  • 原文地址:https://blog.csdn.net/yy8623977/article/details/125429856