Maven操作

Maven操作

1.Maven使用deploy上传jar包到远程库

配置远程库认证

需要在./conf/setting.xml中配置需要远程上传库的地址,用户以及密码(如果需要认证):

<servers>
    <server>
      <id>roy_privrepository_snapshots</id>
      <username>blaaair</username>
      <password>111111</password>
    </server>
</servers>

上面设置仓库的用户名密码 比如在nexus设置的仓库凭证

使用deploy命令上传

mvn install是将jar包安装到本地库,mvn deploy是将jar包上传到远程server,install和deploy都会先自行bulid编译检查,如果确认jar包没有问题,可以使用-Dmaven.test.skip=true参数跳过编译和测试。

mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile=D:\MvnProject\service-mvn-1.0.0.jar -DgroupId=pri.roy.mvn.test -DartifactId=mvn-api -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar -DrepositoryId=roy_privrepository_snapshots -Durl=http://10.4.71.144:9090/repository/roy_privrepository_snapshots/

参数意义:

-Dmaven.test.skip=true                              //跳过编译、测试
-Dfile=D:\MvnProject\service-mvn-1.0.0.jar          //jar包文件地址,绝对路径
-DgroupId=pri.roy.mvn.test                          //gruopId--pom坐标,自定义
-DartifactId=mvn-api                                //artifactId--pom坐标,自定义
-Dversion                                           //版本号
-Dpackaging                                         //打包方式
-DrepositoryId                                      //远程库服务器ID
-Durl                                               //远程库服务器地址

出现success提示即为上传成功

2. 配置私库

在maven的setting配置可以参考官方 : http://maven.apache.org/settings.html#Servers.

3. Jenkins使用maven的坑

如果你需要使用maven作为Jenkins构建项目,拉取snapshots或者release的jar包,明明发布出去了,却始终不能更新最新的jar.

需要配置两处:

1.updatePolicy为always,不然maven会默认每天更新一次

完整setting :

<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
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <profiles>
    <profile>
      ...
      <repositories>
        <repository>
          <id>codehausSnapshots</id>
          <name>Codehaus Snapshots</name>
          <releases>
            <enabled>false</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
            <checksumPolicy>fail</checksumPolicy>
          </snapshots>
          <url>http://snapshots.maven.codehaus.org/maven2</url>
          <layout>default</layout>
        </repository>
      </repositories>
      <pluginRepositories>
        ...
      </pluginRepositories>
      ...
    </profile>
  </profiles>
  ...
</settings>
  • 版本快照:这些是每种工件,版本或快照的策略。通过这两个集合,POM可以在单个存储库中独立于另一个类型更改每种类型的策略。例如,可以决定仅启用快照下载,可能用于开发目的。
  • enabled:对于是否为相应类型(版本或快照)启用此存储库,为true还是false。
  • updatePolicy:此元素指定更新应尝试发生的频率。Maven会将本地POM的时间戳(存储在存储库的maven-metadata文件中)与远程数据进行比较。选项包括:always,daily(默认),interval:X(其中X是以分钟为单位的整数)或从不。
  • checksumPolicy:当Maven将文件部署到存储库时,它还会部署相应的校验和文件。您可以选择忽略,失败或警告缺少或不正确的校验和。
  • layout:在上面的存储库描述中,有人提到它们都遵循一个共同的布局。这大多是正确的。Maven 2具有其存储库的默认布局; 但是,Maven 1.x有不同的布局。使用此元素指定默认或遗留的元素。

2. mvn命令加上 -U 参数

e.g.

mvn install xxx -U

附加 : Maven常用参数及其说明

-h,--help                              Display help information
-am,--also-make                        构建指定模块,同时构建指定模块依赖的其他模块;
-amd,--also-make-dependents            构建指定模块,同时构建依赖于指定模块的其他模块;
-B,--batch-mode                        以批处理(batch)模式运行;
-C,--strict-checksums                  检查不通过,则构建失败;(严格检查)
-c,--lax-checksums                     检查不通过,则警告;(宽松检查)
-D,--define <arg>                      Define a system property
-e,--errors                            显示详细错误信息
-emp,--encrypt-master-password <arg>   Encrypt master security password
-ep,--encrypt-password <arg>           Encrypt server password
-f,--file <arg>                        使用指定的POM文件替换当前POM文件
-fae,--fail-at-end                     最后失败模式:Maven会在构建最后失败(停止)。如果Maven refactor中一个失败了,Maven会继续构建其它项目,并在构建最后报告失败。
-ff,--fail-fast                        最快失败模式: 多模块构建时,遇到第一个失败的构建时停止。
-fn,--fail-never                       从不失败模式:Maven从来不会为一个失败停止,也不会报告失败。
-gs,--global-settings <arg>            替换全局级别settings.xml文件(Alternate path for the global settings file)
-l,--log-file <arg>                    指定输出日志文件
-N,--non-recursive                     仅构建当前模块,而不构建子模块(即关闭Reactor功能)。
-nsu,--no-snapshot-updates             强制不更新SNAPSHOT(Suppress SNAPSHOT updates)
-U,--update-snapshots                  强制更新releases、snapshots类型的插件或依赖库(否则maven一天只会更新一次snapshot依赖)
-o,--offline                           运行offline模式,不联网进行依赖更新
-P,--activate-profiles <arg>           激活指定的profile文件列表(用逗号[,]隔开)
-pl,--projects <arg>                   手动选择需要构建的项目,项目间以逗号分隔;A project can be specified by [groupId]:artifactId or by its relative path.
-q,--quiet                             安静模式,只输出ERROR
-rf,--resume-from <arg>                从指定的项目(或模块)开始继续构建
-s,--settings <arg>                    替换用户级别settings.xml文件(Alternate path for the user settings file)
-T,--threads <arg>                     Thread count, for instance 2.0C where C is core multiplied
-t,--toolchains <arg>                  Alternate path for the user toolchains file
-V,--show-version                      Display version information WITHOUT stopping build
-v,--version                           Display version information
-X,--debug                             输出详细信息,debug模式。
-cpu,--check-plugin-updates            【废弃】,仅为了向后兼容
-npr,--no-plugin-registry              【废弃】,仅为了向后兼容
-npu,--no-plugin-updates               【废弃】,仅为了向后兼容
-up,--update-plugins                   【废弃】,仅为了向后兼容

Maven操作
https://www.blaaair.com/archives/maven-cao-zuo
作者
Glo6f
发布于
2024年07月19日
许可协议