企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 简介 [https://mvnrepository.com/](https://mvnrepository.com/) bin下有mvn和mvnDebug boot下面的jar包是类加载器 conf下是配置文件目录(主要关注settings.xml) lib目录是maven运行依赖的jar包 ![](https://img.kancloud.cn/fc/e1/fce1e45c4e0f3b8675a0501d09540141_893x264.png) ~~~ mvn help:system //生成~/.m2文件夹 ~~~ ~~~ # 可选 MAVEN_OPTS = -Xms128m -Xmx512m ~~~ # maven参数 ~~~ -Xms256m -Xmx2048m -XX:MaxMetaspaceSize=200m -XX:PermSize=128m -XX:MaxPermSize=512M ~~~ # 配置java版本 ~~~ <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>9</maven.compiler.source> <maven.compiler.target>9</maven.compiler.target> </properties> ~~~ ~~~ <build> <plugins> <plugin> <!-- 指定maven编译的jdk版本,如果不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <!-- 一般而言,target与source是保持一致的,但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中不能使用低版本jdk中不支持的语法),会存在target不同于source的情况 --> <source>1.8</source> <!-- 源代码使用的JDK版本 --> <target>1.8</target> <!-- 需要生成的目标class文件的编译版本 --> <encoding>UTF-8</encoding><!-- 字符集编码 --> <skipTests>true</skipTests><!-- 跳过测试 --> <verbose>true</verbose> <showWarnings>true</showWarnings> <fork>true</fork><!-- 要使compilerVersion标签生效,还需要将fork设为true,用于明确表示编译版本配置的可用 --> <executable><!-- path-to-javac --></executable><!-- 使用指定的javac命令,例如:<executable>${JAVA_1_4_HOME}/bin/javac</executable> --> <compilerVersion>1.3</compilerVersion><!-- 指定插件将使用的编译器的版本 --> <meminitial>128m</meminitial><!-- 编译器使用的初始内存 --> <maxmem>512m</maxmem><!-- 编译器使用的最大内存 --> <compilerArgument>-verbose -bootclasspath ${java.home}\lib\rt.jar</compilerArgument><!-- 这个选项用来传递编译器自身不包含但是却支持的参数选项 --> </configuration> </plugin> </plugins> </build> ~~~ **scala配置** ~~~ <!-- the Maven Scala plugin will compile Scala source files --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.2</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> ~~~ # 并行 maven3推出了并行编译的功能,开启后在多核cpu下可以提高构建速度 * \-T,--threads Thread count, for instance 2.0C where C is core multiplied i5-7500 4核4线程, 因此设置为1C,表示一个核心开启一个线程,如果你的u支持超线程如4核8线程可以设置为2C ![](https://img.kancloud.cn/dd/1e/dd1e40c33658825d24d6aed64d1efc69_531x237.png) # 配置源 默认情况下配置多个mirror的情况下,只有第一个生效. 切换 ~~~ mvn help-effective-settings -Daliyun=central ~~~ ~~~ ~/.m2/setting.xml ~~~ ~~~ <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>central</id> <name>Maven Repository Switchboard</name> <url>http://repo1.maven.org/maven2/</url> <mirrorOf>central</mirrorOf> </mirror> <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> <mirror> <id>ibiblio</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url> </mirror> <mirror> <id>jboss-public-repository-group</id> <mirrorOf>central</mirrorOf> <name>JBoss Public Repository Group</name> <url>http://repository.jboss.org/nexus/content/groups/public</url> </mirror> <mirror> <id>google-maven-central</id> <name>Google Maven Central</name> <url>https://maven-central.storage.googleapis.com</url> <mirrorOf>central</mirrorOf> </mirror> <!-- 中央仓库在中国的镜像 --> <mirror> <id>maven.net.cn</id> <name>oneof the central mirrors in china</name> <url>http://maven.net.cn/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> ~~~ # SNAPSHOT版本和正式版本 在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个属性构成的,分别是groupId、artifactId以及version。这三个属性可以唯一确定一个组件(Jar包或者War包)。 假设有两个小组负责维护两个组件,example-service和example-ui,其中example-ui项目依赖于example-service。 而这两个项目每天都会构建多次,如果每次构建你都要升级example-service的版本,那么你会疯掉。这个时候SNAPSHOT版本就派上用场了。 每天日常构建时你可以构建example-service的快照版本,比如example-service-1.0-SNAPSHOT.jar,而example-ui依赖该快照版本。 每次example-ui构建时,会优先去远程仓库中查看是否有最新的example-service-1.0-SNAPSHOT.jar,如果有则下载下来使用。 即使本地仓库中已经有了example-service-1.0-SNAPSHOT.jar,它也会尝试去远程仓库中查看同名的jar是否是最新的。有的人可能会问,这样不就不能充分利用本地仓库的缓存机制了吗? 别着急,Maven比我们想象中的要聪明。 在配置Maven的Repository的时候中有个配置项,可以配置对于SNAPSHOT版本向远程仓库中查找的频率。 频率共有四种,分别是always、daily、interval、never。 当本地仓库中存在需要的依赖项目时,always是每次都去远程仓库查看是否有更新,daily是只在第一次的时候查看是否有更新,当天的其它时候则不会查看;interval允许设置一个分钟为单位的间隔时间,在这个间隔时间内只会去远程仓库中查找一次,never是不会去远程仓库中查找(这种就和正式版本的行为一样了)。 ~~~ clean :清除由项目编译创建的target, install:安装jar包到本地仓库。 -e详细异常,-U强制更新 ~~~ 强制拉一次 ~~~ mvn clean install -e -U ~~~ ~~~ mvn clean package -U ~~~ # 自动更新SNAPSHOT 修改setting.xml的发布部分如下设置 ~~~xml <profile> <id>dev</id> <repositories> <repository> <id>nexus</id> <url>http://ip:8081/nexus/content/groups/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>warn</checksumPolicy> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <url>http://ip:8081/nexus/content/groups/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>warn</checksumPolicy> </snapshots> </pluginRepository> </pluginRepositories> </profile> ~~~ --- enabled true或者false表示该仓库是否为某种类型构件(发布版或者快照版)开启。 updatePolicy 该元素指定更新发生的频率。Maven会比较本地POM和远程POM的时间戳。这里的 选项是:always(一直),daily(默认,每日),interval:X(这里X是以分 钟为单位的时间间隔),或者never(从不)。 checksumPolicy 当Maven将构件部署到仓库中时,它也会部署对应的校验和文件。当没有校验和 文件,或者该文件不正确时,你的选项有ignore(忽略),fail(失败),或者 warn(警告)。 --- enabled设置为true  updatePolicy更新snapshot包的频率,属性有四个值always(实时更新) daily(每天更新) interval:xxx(隔xxx分钟更新一次)  never(从不更新) 默认为daily  checksumPolicy为warn  如果是使用eclipse开发,集成了maven插件的,还需要做如下事情。  ~~~ Window>Preferences>Maven>User Settings>Update Settings  ~~~ 完成以上操作可完成无需更改版本发布,依赖方也不需要更改pom,只需执行一下`mvn clean install` 即可完成依赖更新 # idea更新 ![](https://img.kancloud.cn/17/9a/179a19d8d576cd17a0b9d0e3894617d6_633x295.png) 将Always update snapshots勾选上; 然后右击项目,reimport即可; ![](https://img.kancloud.cn/b9/29/b9297ccb835591d265e2cef78ff663e3_583x484.png)