maven配置详解

2023-05-16

 下载地址:Maven – Download Apache Maven,

添加环境变量:MAVEN_HOME

一、配置文件

maven的配置文件主要有 settings.xml 和pom.xml 两个文件。

1.其中在maven安装目录,例如apache-maven-3.8.1\conf目录下的settings.xml 文件是全局配置文件

2.用户目录的.m2子目录下面的settings.xml的配置只是针对当前用户的配置

3.项目根路径下的pom.xml主要是对当前项目的配置。

局部配置优先于全局配置。 配置优先级从高到低:pom.xml> user settings > global settings

二、settings.xml 配置详解

1.LocalRepository 本地仓库配置:


<localRepository>D:\repository</localRepository>  

2.InteractiveMode 用户输入配置:


<interactiveMode>true</interactiveMode>  

3.离线模式


<offline>false</offline>  

3.插件组,当我们使用某个插件,并且没有在命令行为其提供组织Id(groupId)的时候,Maven就会使用该列表。默认情况下该列表包含了org.apache.maven.pluginsorg.codehaus.mojo


<pluginGroups>
    <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>  

4.私服服务器配置,配置私服的用户名和密码。配置的私服服务器可以用来发布jar包,与pom.xml 中 发布标签distributionManagement 中配置的仓库ID相互对应。


<servers>
    <server>
        <id>maven-releases</id>
        <username>developer</username>
        <password>123456</password>
        <!--文件被创建时的权限。 -->
        <filePermissions>664</filePermissions>
        <!--目录被创建时的权限。 -->
        <directoryPermissions>775</directoryPermissions>
    </server>
</servers>  

 5.镜像配置


<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>https://maven.aliyun.com/repository/central</url>
    <!-- 被镜像的服务器的id -->
    <mirrorOf>*</mirrorOf>
</mirror>  

6.Profiles配置。

settings.xml中的profile元素是pom.xml中profile元素的子集。只包含了id、activation、repositories、pluginRepositories和 properties元素。
如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile。


<profiles>
    <profile>
        <id>nexus</id>
        <!-- 设置默认激活 -->
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <repositories>
            <!-- 配置依赖仓库,可以配置多个仓库,maven会按照顺序进行依赖的加载 -->
            <repository>
                <id>nexus</id>
                <name>gwm nexus</name>
                <url>http://nexus.maven.cn/repository/maven-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <!-- 设置插件仓库 -->
        <pluginRepositories>
            <pluginRepository>
                <id>nexus</id>
                <name>gwm nexus</name>
                <url>http://nexus.maven.cn/repository/maven-public/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
    <profile>
        <id>sonar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <!-- 配置Sonarquebe 代码扫描插件的全局属性-->
        <properties>
            <sonar.host.url>
                http://localhost:9000
            </sonar.host.url>
            <sonar.login>admin</sonar.login>
            <sonar.password>admin</sonar.password>
        </properties>
    </profile>
</profiles>  

7. Activation配置,用来设置profile配置激活的条件逻辑。


<activation>
  <!--profile默认是否激活的标识 -->
  <activeByDefault>false</activeByDefault>
  <!--当匹配的jdk被检测到,profile被激活。例如,1.4激活JDK1.4,1.4.0_2,而!1.4激活所有版本不是以1.4开头的JDK。 -->
  <jdk>1.8</jdk>
  
  <!--如果Maven检测到某一个属性(其值可以在POM中通过${name}引用),其拥有对应的name = 值,Profile就会被激活。如果值字段是空的,那么存在属性名称字段就会激活profile,否则按区分大小写方式匹配属性值字段 -->
  <property>
    <!--激活profile的属性的名称 -->
    <name>mavenVersion</name>
    <!--激活profile的属性的值 -->
    <value>2.0.3</value>
  </property>
  <!--提供一个文件名,通过检测该文件的存在或不存在来激活profile。missing检查文件是否存在,如果不存在则激活profile。另一方面,exists则会检查文件是否存在,如果存在则激活profile。 -->
  <file>
    <!--如果指定的文件存在,则激活profile。 -->
    <exists>${basedir}/file2.properties</exists>
    <!--如果指定的文件不存在,则激活profile。 -->
    <missing>${basedir}/file1.properties</missing>
  </file>
</activation>  

8.properties 配置,对应profile的扩展属性和pom中properties的属性列表,这些值可以在pom.xml,setting.xml中使用标记${X}来使用,这里X是指属性的名称。


<!-- 
  1. env.X: 在一个变量前加上"env."的前缀,会返回一个shell环境变量。例如,"env.PATH"指代了$path环境变量(在Windows上是%PATH%)。 
  2. project.x:指代了POM中对应的元素值。例如: <project><version>1.0</version></project>通过${project.version}获得version的值。 
  3. settings.x: 指代了settings.xml中对应元素的值。例如:<settings><offline>false</offline></settings>通过 ${settings.offline}获得offline的值。 
  4. Java System Properties: 所有可通过java.lang.System.getProperties()访问的属性都能在POM中使用该形式访问,例如 ${java.home}。 
  5. x: 在<properties/>元素中,或者外部文件中设置,以${someVar}的形式使用。
 -->
<properties>  

  <project.version>1.0</project.version>  

</properties>  

9. Repositories 远程仓库配置,可以配置多个。可以配置在<settings>标签中,也可以配置在<profile>标签中(比较常见,配置在<profile>标签中可以根据profile的激活情况动态选择仓库)。配置形式参见《6.Profiles配置》。

10.插件仓库pluginRepositories 和repositories相同。

11. 激活profile配置 activeProfiles,用来激活配置的profile。和 activation 配置相比 activeProfiles 配置比较简单,也比较常用。


<activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>  

 三、POM.xml配置文件:


<parent>
    <!--父项目的构件标识符 -->
    <artifactId />
    <!--父项目的唯一标识符 -->
    <groupId />
    <!--父项目的版本 -->
    <version />
    <!-- 父项目的pom.xml文件的相对路径。
    默认值是../pom.xml。
    Maven首先在构建当前项目的地方寻找父项目的pom,其次在文件系统的这个位置(relativePath位置),然后在本地仓库,最后在远程仓库寻找父项目的pom。 
    注意:如果在父项目中通过<modules>指定了子模块,且子模块在父项目目录下,则不需要指定此配置。如果子项目不在父项目的目录下,应该指定此配置。
    -->
    <relativePath>../pom.xml</relativePath>
</parent>

<!-- 模型版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 公司或者组织的唯一标志-->
<groupId>com.companyname.project-group</groupId>
<!-- 项目的唯一ID->
<artifactId>project</artifactId>
<!-- 版本号 -->
<version>1.0</version>

<!--项目产生的构件类型,例如jar、war、ear、pom -->
<packaging>jar</packaging>

<!-- 属性配置 -->
<properties>
    <!-- 编译时的编码 -->
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<!-- 依赖配置 -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>${spring-boot.version}</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
<!-- 依赖声明,不会真正引入包。一般在父pom中进行声明,在子pom中真正引入 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-core</artifactId>
            <version>${hutool.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>     

<!-- 编译构建相关配置 -->
<build>
    <!-- 插件申明,一般在父pom中声明,在子pom中真正引入 -->
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <!-- 插件引入,在父pom中引入以后,所有子pom中都会引入 -->
    <plugins>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>3.6.0.1398</version>
        </plugin>
    </plugins>
</build>       

<!-- 针对当前项目的远程仓库配置 -->
<repositories>
    <repository>
        <id>aliyun-public</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>
<!-- 针对当前项目的远程插件仓库配置 -->
<pluginRepositories>
    <pluginRepository>
        <id>aliyun-public</id>
        <url>https://maven.aliyun.com/repository/public</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

<!--jar包发布私服配置-->
<distributionManagement>
    <repository>
        <!-- 此ID和setting.xml 中server中配置的服务器进行对应 -->
        <id>maven-releases</id>
        <name>releases</name>
        <url>http://nexus.maven.cn/repository/maven-releases/</url>
        <uniqueVersion>true</uniqueVersion>
    </repository>
    <snapshotRepository>
        <id>maven-snapshots</id>
        <name>snapshots</name>
        <url>http://nexus.maven.cn/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

<!--动态构建配置,通过设置活动的profile,profile中的配置会作用于当前的项目编译构建 -->
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <spring.profiles.active>prod</spring.profiles.active>
        </properties>
    </profile>
</profiles>  

四、远程仓库的加载

maven仓库依赖下载顺序:

1,在settings.xml文件中配置的本地仓库中寻找依赖,没找到则进入第2步。

2,在settings.xml文件中配置的全局远程仓库中寻找,没找到则进入第3步。

3,在当前项目的pom.xml中配置的远程仓库中寻找,如果没找到则进入第4步。

4,在中央仓库 https://repo.maven.apache.org/maven2 中寻找,如果没找到则抛出依赖无法加载异常。

镜像替换:

1,如果在找寻的过程中,如果发现该仓库有镜像匹配,则直接从镜像仓库中加载。

2,如果仓库的 id 设置成 <mirrorOf>central</mirrorOf>,则会覆盖 maven 的中央仓库配置。

3,如果镜像 ID 设置为 <mirrorOf>*</mirrorOf> 表示匹配所有的仓库,则所有依赖只从此镜像仓库中下载。

4,如果镜像ID 设置为 <mirrorOf>repo1,repo2</mirrorOf>,则匹配仓库repo1和repo2,使用逗号分隔多个远程仓库

5,如果镜像ID设置为 <mirrorOf>*,!repo1</miiroOf>匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除

 建议将镜像地址作为一个 普通仓库repository 进行配置,这样可以在其他 仓库下载不了的情况下查找到此仓库。如果配置了镜像仓库代替其他仓库容易出现在镜像中找不到依赖,导致项目无法编译的问题。

 原创文章,引用请注明出处,并联系本人征得本人同意后才可转载。

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

maven配置详解 的相关文章

随机推荐

  • 【面试必备】面向Android开发者的复习指南!最全的BAT大厂面试题整理

    近日一好友去阿里面试 xff0c 面试失败了 xff0c 分享了一个他最不擅长的算法面试题 题目是这样的 题目 xff1a 给定一个二叉搜索树 BST xff0c 找到树中第 K 小的节点 出题人 xff1a 阿里巴巴出题专家 xff1a
  • 文件选择器DocumentUI显示apk文件图标

    1 定位代码与调试 Hierarchy Viewer分析界面工具 xff0c uiautomatorviewer自动化查看器 xff0c 定位组件 添加调试信息 xff0c 调试定位代码 2 搜索获取apk文件图标 lt uses perm
  • Android adb 启动APP

    目录 启动命令一 常规命令 43 包名 activity二 常规命令 43 包名命令关闭App 获取包名和activity的路径代码获取1 命令获取 需要app运行在前台 xff0c 停留在启动界面 2命令获取 先执行命令 xff0c 再点
  • Android日志[基础篇]Android Log日志输出

    Android日志 基础篇 二 Android Studio修改LogCat日志的颜色 android util Log输出日志的常用方法如下 xff1a Log v String tag String msg Log d String t
  • Android WebView https白屏、Http和Https混合问题、证书配置和使用

    目录 前言启用https后白屏 xff08 证书错误 xff09 修改处理WebView中Http和Https混合问题处理办法Webview的几种内容加载模式 证书配置或处理https请求的证书okhttp进行请求 xff1a HttpsU
  • Java错误:找不到或无法加载主类

    目录 前言javac xxx java 编译需要相对物理路径java xxx 执行需要虚拟路径总结 前言 一般情况下 xff0c 我们都使用工具进行代码的编辑和调试 xff0c 例如eclipse Manven Android Studio
  • Edge 修改字符编码(详细图文)

    Microsoft Edge 版本 97 0 1072 62 官方内部版本 64 位 前言 如下图 xff0c 在访问页面时出现乱码 xff0c 而且一直返回的内容编码是UTF 8 xff0c 但Edge没找快捷的编码方式选择 方法一 In
  • Charles抓取HTTPS Windows Android iOS 图文详细

    文章目录 背景操作原理windows 安装CharlesCharles配置第一步 xff1a 配置HTTP代理 xff0c 这步与抓取HTTP请求是一样第二步 xff1a 配置SSL代理第三步 xff1a 为手机配置代理iPhone 代理配
  • Linux less 命令使用介绍

    文章目录 1 xff0e 命令格式2 xff0e 命令功能3 xff0e 命令参数4 xff0e 按键操作5 xff0e 示例1 查看文件内容2 ps查看进程信息并通过less分页显示3 查看命令历史使用记录并通过less分页显示5 浏览多
  • macOS/iOS WKWebview 下载文件

    WKWebview 下载文件需要通过JS注入的方式来下载 js下载的数据是base64编码的 xff0c 回到给原生后 xff0c 原生需要反编码后才是原始文件的数据 具体步骤 xff1a 配置WKWebview的js回调句柄 xff08
  • 【uniapp原生插件】招商银行一网通支付android&iOS

    招行支付插件说明 参考uni插件市场 wx gaogaoEagle 插件集成准备 从招行获得appid和appSchememanifest json中App原生插件配置 xff0c 云端插件选择试用或购买的插件 xff0c 并配置Andro
  • mongodb命令使用

    设置慢查询 db setProfilingLevel 1 200 查询副本集当前状况 需要将命令行切换到需要查询的副本集中 rs status 查询数据库当前情况 sh status 查询数据分片情况 db 集合名称 getShardDis
  • Duplicate class android.support.v4.app.INotificationSideChannel found in modules core-1.5.0-runtime

    冲突问题 androidx和support类冲突 xff0c 具体如下 xff1a Duplicate class android support v4 app INotificationSideChannel found span cla
  • 关于I帧/IDR、B帧、P帧、SPS、PPS

    在h264编解码中 xff0c 常常有I帧 IDR B帧 P帧 IDR NALU GOP xff0c 但往往没有关注细节 或者我们本身在实际应用中已使用过很多次 xff0c 但对相关的技术名词不清楚 在H264协议里定义了三种帧 xff0c
  • 如何保证数据库与缓存的数据一致性

    一 先删缓存 xff0c 再修改数据库 数据不一致的情况 一 线程A修改数据时 xff0c 需先执行删除缓存操作 二 其他线程只要在线程A删除缓存和执行update期间 xff0c 查询数据库得到了旧的数据 xff0c 此时就有极大的概率会
  • spring-jms/DefaultMessageListenerContainer配置

    一个DefaultMessageListenerContainer可以开启多个 concurrent AsyncMessageListenerInvoker并发 收消息 两种模式 模式一 xff1a 递增监听线程并调度 xff0c 监听线程
  • IDEA的Git操作——拉取、对比开发代码

    一 git原理 git是一个分布式的版本管理工具 xff0c 主要分为2个部分 xff1a 本地 xff1a 分为3个区 xff1a working space xff08 工作区 xff09 staging area xff08 暂存区
  • .sh文件无法执行

    sh文件无法执行 解决方法 xff1a chmod x xxx sh
  • html页面中查找元素 css

    1 根据某标签获取上层父标签 lt div id 61 34 div1 34 class 61 34 div1 34 gt lt a gt lt input type 61 34 text 34 gt lt img id 61 34 img
  • maven配置详解

    下载地址 xff1a Maven Download Apache Maven xff0c 添加环境变量 xff1a MAVEN HOME 一 配置文件 maven的配置文件主要有 settings xml 和pom xml 两个文件 1 其