Maven 配置文件(pom.xml和setting.xml)

maven设置仓库地址可以在pom.xml文件中配置,也可以在conf/settings.xml中配置。

寻找jar的基本优先级顺序:
本地仓库 > settings.xml的profile的仓库 > pom.xml的profile的仓库 >pom.xml的仓库 > 中央仓库。

1.pom.xml

1.1 mvn常用的命令

  • mvn clean package:打包到本项目,一般在项目target目录下
    • 它执行了(7步): clean、resources、compile、testResources、testCompile、test、jar
  • mvn clean install:打包到本地仓库,如果没设置Maven本地仓库,一般在用户/.m2目录下。
    • 它执行了(8步):clean、resources、compile、testResources、testCompile、test、jar、install
  • mvn clean deploy:打包上传到远程仓库,如:私服nexus等,需要配置pom文件
    • 它执行了(9步):clean、resources、compile、testResources、testCompile、test、jar、install、deploy.
    • 例如:
      mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=C://fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

1.2  pom.xml设置仓库

        release和snapshot两者的区别。release是稳定版本,一经发布不再修改,想发布修改后的项目,只能升级项目版本再进行发布;snapshot是不稳定的,一个snapshot的版本可以不断改变。项目在开发期间一般会使用snapshot,更方便进行频繁的代码更新;一旦发布到外部,或者开发基本完成,代码迭代不再频繁,则推荐使用release。

<repositories>
    <repository>
        <!--远程仓库唯一标识符。用来匹配在settings.xml文件中配置的server.id -->
        <id>alimaven</id>
         <!--远程仓库名称 -->
        <name>aliyun maven</name>
        <url>https://maven.aliyun.com/nexus/content/repositories/central/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <!-- 用于定位和排序构件的仓库布局类型。可以是default或者legacy -->
        <layout>default</layout>
    </repository>
</repositories>
<!--发现插件的远程仓库列表,这些插件用于构建和报表 -->
<pluginRepositories>
    <pluginRepository>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>https://maven.aliyun.com/nexus/content/repositories/central/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

1.3  工程发布到远程仓库distributionManagement

 <!--在执行mvn deploy后表示要发布的位置。用于把项目发布到远程仓库 -->
    <distributionManagement>
        <!--部署项目产生的构件到远程仓库需要的信息 -->
        <repository>
            <!-- 是分配给快照一个唯一的版本号 -->
            <uniqueVersion/>
            <!--远程仓库唯一标识符。用来匹配在settings.xml文件中配置的server.id -->
            <id>nanxs-maven2</id>
            <name>nanxsmaven2</name>
            <url></url>
        </repository>
        <snapshotRepository>
            <uniqueVersion/>
            <id>nanxs-maven2</id>
            <name>Nanxs-maven2 Snapshot Repository</name>
            <url></url>
        </snapshotRepository>
    </distributionManagement>

1.4 dependency依赖设置

         pom文件中通过dependencyManagement来声明依赖,通过dependencies元素来管理依赖。dependencyManagement下的子元素只有一个直接的子元素dependencice,其配置和dependencies子元素是完全一致的;而dependencies下只有一类直接的子元素:dependency。一个dependency子元素表示一个依赖项目。

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.5</version>
            <!--默认是jar,可以是jar,war,ejb-client和test-jar-->
            <type>jar</type>
            <!--依赖的分类器。分类器可以区分属于同一个POM,但不同构建方式的构件。分类器名被附加到文件名的版本号后面,如果想将项目构建成两个单独的JAR,分别使用Java 4和6编译器,就可以使用分类器来生成两个单独的JAR构件-->
            <classifier></classifier>
            <!--控制依赖传递,排除该项目中的一些依赖,即本项目A依赖该dependency指示的项目B,但是不依赖项目B中依赖的这些依赖;-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-log4j2</artifactId>
                </exclusion>
            </exclusions>
            <!--可选依赖,用于阻断依赖的传递性,即本项目不会依赖父项目中optional设置为true的依赖。-->
            <optional>true</optional>
            <!-- 依赖范围:-->
            <!-- - compile:默认范围,用于编译;-->
            <!-- - provided:类似于编译,但支持jdk或者容器提供,类似于classpath-->
            <!-- - runtime: 在执行时需要使用;-->
            <!-- - systemPath: 仅用于范围为system,提供相应的路径-->
            <!-- - test: 用于test任务时使用;-->
            <!-- - system: 需要外在提供相应的元素,通过systemPath来取得-->
            <!-- - optional: 当项目自身被依赖时,标注依赖是否传递。用于连续依赖时使用-->
            <scope>compile</scope>
        </dependency>

1.5 build配置

        build配置下主要分为resources,pulgins,还有其他配置(类似finalName)。

<build>
        <!--        其他配置-->
        <!--当项目没有规定目标(Maven2 叫做阶段)时的默认值 -->
        <defaultGoal></defaultGoal>
        <!--构建产生的所有文件存放的目录 -->
        <directory></directory>
        <!--产生的构件的文件名,默认值是${artifactId}-${version}。 -->
        <finalName></finalName>
        <!--当filtering开关打开时,使用到的过滤器属性文件列表 -->
        <filters></filters>
        <!--项目源码目录,当构建项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。 -->
        <sourceDirectory></sourceDirectory>
        <!--该元素设置了项目单元测试使用的源码目录。该路径是相对于pom.xml的相对路径 -->
        <testSourceDirectory></testSourceDirectory>
        <!--被编译过的应用程序class文件存放的目录。 -->
        <outputDirectory></outputDirectory>
        <!--被编译过的测试class文件存放的目录。 -->
        <testOutputDirectory></testOutputDirectory>
        <!--项目脚本源码目录,该目录下的内容,会直接被拷贝到输出目录,因为脚本是被解释的,而不是被编译的 -->
        <scriptSourceDirectory></scriptSourceDirectory>
        <!--        resources配置-->
        <resources>
            <!--这个元素描述了项目相关或测试相关的所有资源路径 -->
            <resource>
                <!-- 描述了资源的目标输出路径。该路径是相对于target/classes的路径 -->
                <!-- 如果是想要把资源直接放在target/classes下,不需要配置该元素 -->
                <targetPath>./</targetPath>
                <!--是否使用参数值代替参数名。参数值取自文件里配置的属性,文件在filters元素里列出 -->
                <filtering/>
                <!--描述打包前的资源存放的目录,该路径相对POM路径 -->
                <directory>./</directory>
                <!--包含的模式列表,例如**/*.xml,只有符合条件的资源文件才会在打包的时候被放入到输出路径中 -->
                <includes>./*.html</includes>
                <!--排除的模式列表,例如**/*.xml,符合的资源文件不会在打包的时候会被过滤掉 -->
                <excludes>./*.xml</excludes>
            </resource>
        </resources>
        <!--        plugins配置-->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

默认的plugins:

pluginfuncationlife cycle phase
maven-clean-plugin清理上一次执行创建的目标文件clean
maven-resources-plugin处理源资源文件和测试资源文件resources;testResources
maven-compiler-plugin编译源文件和测试源文件compile,testCompile
maven-surefire-plugin执行测试文件test
maven-jar-plugin创建jarjar
maven-install-plugin安装jar,将创建生成的jar拷贝到.m2/repository下面install
maven-deploy-plugin发布jardeploy

 1.6 profiles配置

 profile是配置动态激活不同环境、依赖打包部署。

 <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <!--                设置默认激活:-->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <classname>SecurityProgram.class</classname>
            </properties>
            <build>
                <finalName>dev-jar</finalName>
                <plugins>
                    <plugin>
                        <!--                        设置jar包中只有SecurityProgram.class-->
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/${classname}</include>
                            </includes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>prod</id>
            <build>
                <finalName>prod-jar</finalName>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

命令行执行:

编译:mvn compile -P prod
打包:mvn package -P prod
部署(本地仓库):mvn install -P dev
发布(远程仓库):mvn deploy -P dev
查看当前激活的profile:mvn help:active-profiles
参数说明:
-P [parameter]:-P可以同时多个参数,如mvn deploy -P test,dev,test和dev都是上面自定义的profile的id值。 

2.setting文件

settings.xml文件

settings 主要由mirrors、servers 和profiles 三部分组成。

修改conf/setting.xml,增加一下配置

<?xml version="1.0" encoding="UTF-8"?>
<settings
    xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>/Users/Documents/repo</localRepository>
    <pluginGroups></pluginGroups>
    <proxies></proxies>
    <servers>
        <!--
    发布到仓库中的配置,id要和distributionrepository(pom.xml中)保持一致
    服务器要打包上传到私服时,设置私服的鉴权信息,否和报错 Return code is: 401, ReasonPhrase: Unauthorized
    -->
        <server>
            <id>release</id>
            <username>deployment</username>
            <password>123456</password>
        </server>
        <server>
            <id>snapshot</id>
            <username>deployment</username>
            <password>123456</password>
        </server>
    </servers>
    <mirrors>
        <!--  设置多个mirrors镜像,镜像只会执行第一个位置mirror。-->
        <!--  配置的多个mirror可以都放着不影响,选取一个镜像下载比较快的放在第一个就行。比如你设置使用自己公司的私有仓库-->
        <!--只有当前一个mirror无法连接的时候,才会去找后一个,类似于备份和容灾。所以当第一个mirror中不存在a.jar的时候,并不会去第二个mirror中查找,甚至于,maven根本不会去其他的mirror地址查询-->
        <mirror>
            <!--      当有id为B,A,C的顺序的mirror在mirrors节点中,maven会根据字母排序来指定第一个,所以不管怎么排列,一定会找到A这个mirror来进行查找,当A无法连接,出现意外的情况下,才会去B查询-->
            <id>aliyun</id>
            <name>aliyun Maven</name>
            <url>>http://maven.aliyun.com/nexus/content/groups/public</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>
    <profiles>
        <!-- 阿里云配置: 提高国内的jar包下载速度 -->
        <profile>
            <id>aliyun-Repository</id>
            <repositories>
                <repository>
                    <id>aliyun</id>
                    <url>>http://maven.aliyun.com/nexus/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>suwell-Repository</id>
            <repositories>
                <repository>
                    <id>first</id>
                    <name>Repository first</name>
                    <url></url>
                </repository>
            </repositories>
        </profile>
        <profile>
            <id>gomain-Repository</id>
            <repositories>
                <repository>
                    <id>second</id>
                    <name>Repository second</name>
                    <url></url>
                </repository>
            </repositories>
        </profile>
    </profiles>
	激活仓库配置,拉取依赖会在这些仓库中逐个去找
    <activeProfiles>
        <activeProfile>first-Repository</activeProfile>
        <activeProfile>aliyun-Repository</activeProfile>
        <activeProfile>second-Repository</activeProfile>
    </activeProfiles>
</settings>

2.1 mirrors

mirrors 主要作用是一个镜像代理,便于内外网厂库切换,或者单独配置内网使用。

如果pom中的repository的id能和mirrorOf的值关联上,那么url以mirror的为准,否则以repository中自己的url为准。

mirrorof:

  • *代表 所有仓库请求都走这个配置的镜像代理
  • central 默认是maven 的仓库

2.2 servers

        关联pom中distributionManagement下repository的id, 在推送依赖包的时候id进行认证和seting.xml的repository进行ID认证。

2.3 profiles

        根据环境参数或命令行参数激活某个构建处理。

参考:

maven:pom文件详解_pom directory_夜雨落花的博客-CSDN博客

Maven之pom.xml文件中的Build配置_pom build-CSDN博客 

【Maven】使用maven profile 动态激活不同环境、依赖打包部署_pom dependency 不同环境_子非Yu@Itfuture的博客-CSDN博客 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值