基础服务
【未分类】
备忘的一些链接
图床列表 - imgtu
【目录】生活&其它
游戏修改器-nes游戏修改记录
尝试操作系统-Community20.3
哈哈哈
警世恒言/人生谎言
使用视频采集卡让笔记本当nuc的屏幕
【已废弃】获取微信好友-itchat
获取微信好友-pc hook
易混词语(尽量避免使用)
有用的链接(工具)
zy服务版本记录
注意安全、生命、健康
日文输入法
安卓模拟器中使用微信充值微信豆
备忘小技巧
excel
网站生成
静态网站生成工具-vuepress
静态网站生成工具-hugo
【目录】代码片段
动态显示select的option列表
同时显示汉字和拼音
常用java代码
性能
jmeter
jmeter-性能
ops
kubernetes
jenkins
drone
服务升级记录
mrdoc升级记录
mysql升级记录
方案
跨语言的web服务间接口调用安全问题
【其它编程语言】
编程-javascript
编程-python
前端框架-react
编程-golang
c语言
.NET(dotnet)
【前端】
Lua简明教程
【缓存】redis
redis
服务搭建
搭建mrdoc
主机服务及端口列表
安装centos7虚拟机
docker
虚拟机-vagrant和virtualbox
安装AlpineLinux虚拟机
docker machine
docker-swarm
配置https域名
centos7服务器清理磁盘空间
vscode
multipass
ubuntu 使用
【数据库】mysql
mysql命令
mysql
HashDatabaseAndTableTest.java
mysql-快速导入100w条记录
mysql高级命令
用于排查问题的一些常见命令
基础信息
mysql5.7升级到8
maven相关
maven命令行汇总
自动升级maven工程中的项目版本
maven基础及高级
windows系统使用
windows客户端软件使用
windows 宿主机 + virtualbox虚拟机共同使用
windows常见问题记录
新的开发环境
windows11配置
windows系统下载
命令行安装软件 & 配置软件
折腾谷歌浏览器
浏览器插件列表
windows音频视频下载工具
云服务
服务-tao-道
【目录】技术分享
技术分享-todo
【目录】shell
windows常用命令
shell脚本汇总
shell命令汇总
windows常用脚本
命令行操作录制工具 asciinema-player
windows软件包管理器scoop
网络相关
服务使用
nexus
jenkins
apifox
notify (bark-server)
开发人员相关
IDEA相关配置
压力测试工具-jmeter
【目录】git集合
git常用操作
gitlab替代品:gitea搭建及简单使用
【git神技】git配置多个提交账户
【git神技】git别名使用
【git神技】一个本地仓库同时提交到github/gitlab/gitee等多个代码平台
gitlab使用记录
git问题汇总
git-for-windows配置
用过的jdk版本
内网穿透-通过公网ip访问本地web服务
常见问题的排查及工具
本文档使用 MrDoc 发布
-
+
首页
maven基础及高级
## maven ## alias 配置别名 ```bash echo "alias mcp='mvn clean package'" >> ~/.bashrc echo "alias mci='mvn clean install'" >> ~/.bashrc source ~/.bashrc ``` #### 阿里云maven [settings.xml] ```xml <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> ``` #### 阿里云maven [pom.xml] ```xml <repository> <id>maven-aliyun</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository> ``` #### mirror 与 repository - repository是仓库,说明要直连这个仓库获取jar。可被配置在pom.xml中 - mirror是镜像,会拦截对远程仓库的请求到配置的地址上 #### 反应堆 - https://juvenshun.iteye.com/blog/565240 - https://www.jianshu.com/p/29bb16755883 - http://maven.apache.org/guides/mini/guide-multiple-modules.html - 说明 -pl (project list),为要操作的模块,建议使用`groupId:artifactId`,以英文逗号分隔,-am(also make)参数把要操作的模块的依赖模块也进行处理 - 不建议在-pl的参数中使用pom的g:a而要使用它的子模块的g:a `mvn clean package -pl groupId1:artifactId1,groupId2:artifactId2 -am` #### 一些命令 - 运行java类 `mvn exec:java -Dexec.mainClass=com.examples.Main` - 显示依赖树 `mvn -X compile dependency:tree -Dverbose >a.log` - 下载源代码jar -DdownloadSources=true - 下载javadoc包 -DdownloadJavadocs=true - 跳过测试 -Dmaven.test.skip=true 或 -DskipTests - 有时为了跳过测试,会使用参数-DskipTests和-Dmaven.test.skip=true,这两个参数的主要区别是: * -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。 * -Dmaven.test.skip=true,不执行测试用例,也不编译测试用例类。 - 确认未使用的依赖项 `mvn dependency:analyze` #### 常用插件 ```xml <!-- compile source --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>utf8</encoding> </configuration> </plugin> <!-- deploy source jar--> <plugin> <artifactId>maven-source-plugin</artifactId> <version>2.1</version> <configuration> <attach>true</attach> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <!-- copy dependencies --> <!-- mvn org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies mvn org.apache.maven.plugins:maven-dependency-plugin:copy-dependencies -DoutputDirectory=${project.build.directory}/dependency_dir --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>install</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>lib</outputDirectory> </configuration> </execution> </executions> </plugin> <!-- skip tests --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> <!-- 解决spring3注解方式时,不能扫描jar包里面的Bean类 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> </configuration> </plugin> <!-- 打包时排除某个类 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <excludes> <exclude>com/project/MainApp*</exclude> </excludes> </configuration> </plugin> <!-- 将属性回显到控制台 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.1</version> <executions> <execution> <phase>validate</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo> settings.localRepository = ${settings.localRepository} </echo> <echo> project.build.directory = ${project.build.directory} </echo> </tasks> </configuration> </execution> </executions> </plugin> <!-- deploy排除某些包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <skip>true</skip> </configuration> </plugin> ``` versions-maven-plugin https://maven.apache.org/maven-release/maven-release-plugin/index.html maven-release-plugin #### pom.xml中使用属性 maven定义了很多变量属性,参考[这里](http://docs.codehaus.org/display/MAVENUSER/MavenPropertiesGuide)。 - 内置属性 ``` ${basedir} represents the directory containing pom.xml ${version} equivalent to ${project.version} or ${pom.version} ``` - Pom/Project properties ``` 所有pom中的元素都可以用 'project.' 前缀进行引用,如${project.build.directory},${project.build.finalName} ``` - 本地用户设定 ``` 所有用的的 settings.xml 中的设定都可以通过 'settings.' 前缀进行引用,如${settings.localRepository} refers to the path of the user's local repository. ``` - 环境变量 ``` 系统的环境变量通过 'env.' 前缀引用,如${env.M2_HOME} ``` - java系统属性 ``` 直接使用如${java.home}, ${java.version}, ${os.name}, ${file.separator}, ${path.separator}, ${line.separator} ``` - 用户在pom中定义的自定义属性(直接使用,此最常见) ``` <properties> <java.version>1.8</java.version> </properties> ``` - 上级工程的变量 ``` 上级工程的pom中的变量用前缀 ${'project.parent.'} 引用. ``` #### 多环境配置 - 方法一 使用配置文件 ``` 常见的就是配置一个src/main/filters目录,里面有filter-dev.properties\filter-test.properties\filter-prod.properties等配置文件,每个文件里的key是相同的,只有Value不同 ``` - 方法二 使用配置目录 这种方式也常见,配置多个类似的目录,在打包时指定使用哪一个,如下配置片段 ``` <resources> <resource> <directory>src/main/conf/${profiles.active}</directory> </resource> </resources> <profiles> <!-- 默认激活 dev 开发环境 --> <!-- production使用 mvn xxx -Pproduction --> <profile> <!-- 本地开发环境 --> <id>development</id> <properties> <profiles.active>dev</profiles.active> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <!-- 生产环境 --> <id>production</id> <properties> <profiles.active>production</profiles.active> </properties> </profile> </profiles> ``` #### 本地仓库无用文件清理 1. 使用cmd删除所有以.lastUpdate结尾的文件 切换到maven的本地仓库,在当前目录打开cmd命令行, 执行命令 `for /r %i in (*.lastUpdated) do del "%i"` 2. 清理空文件夹 for /f "delims=" %a in ('dir /ad /b /s E:\maven-repo\^|sort /r') do ( rd "%a">nul 2>nul && echo 空目录"%a"成功删除!) #### 插件开发 #### using archetype `mvn archetype:generate -DgroupId=com.billy.bigdata -DartifactId=bigdata-hadoop -DpackageName=com.billy.bigdata.hadoop -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0-SNAPSHOT -DinteractiveMode=false` #### maven wrapper 使用 `mvn wrapper:wrapper`可以生成.mvn/wrapper/maven-wrapper.properties文件,大致内容如下(使用国内源): ```text #distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip distributionUrl=https://mirrors.huaweicloud.com/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip #distributionUrl=http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip #wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar wrapperUrl=https://mirrors.huaweicloud.com/repository/maven/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar ``` ```bash # maven wrapper可以使用如下(并未测试,因为使用的是settings.xml) export MVNW_REPOURL=https://maven.aliyun.com/nexus/content/groups/public # 指定.m2目录,默认是~/.m2 export MAVEN_USER_HOME=~/.m2_another_dir ``` ## 并发编译/打包 `mvn clean package -DskipTests -T4` ### 私服 - nexus3 - reposilite #### reposilite <https://reposilite.com/> ##### what? Lightweight repository manager for Maven artifacts. ##### why? - Reduce usage of your resources to even 8MB of RAM - 95%+ test coverage - Easy to use ##### disadvantage against nexus3 - only support java, not support npm/docker ##### have a try ##### install - download a jar named 'reposilite-VERSION.jar' from <https://github.com/dzikoysk/reposilite/releases> - run `java -jar reposilite-VERSION.jar` - visit <http://localhost:80/> ##### upload artifact - generate token `keygen / user0 m` - add alias and token into ~/.m2/settings.xml or $M2_HOME/conf/settings.xml ```xml <server> <id>local-repo-release</id> <username>user0</username> <password>paste_token_here</password> </server> <server> <id>local-repo-snapshot</id> <username>user0</username> <password>paste_token_here</password> </server> ``` - add below into pom.xml ```xml <distributionManagement> <repository> <id>local-repo-release</id> <url>http://localhost:80/releases</url> </repository> <snapshotRepository> <id>local-repo-snapshot</id> <url>http://localhost:80/snapshots</url> </snapshotRepository> </distributionManagement> ``` - `mvn clean deploy -DskipTests` - code is attached. [【附件】maven-reposilite-usage.zip](/media/attachment/2023/07/maven-reposilite-usage.zip) ##### conclusion only use to publish jar to other users, can not be consider as private-server. ## mvnd [mvn daemon](https://downloads.apache.org/maven/mvnd/) ## maven源码分析 - 版本3.6.3 可以修改$M2_HOME/conf/logging/simplelogger.properties 使日志内容更多(日志中显示时间/线程名/logger名 ``` org.slf4j.simpleLogger.showDateTime=true # 如下一行原simplelogger.properties中没有,看源码org.slf4j.impl.SimpleLoggerConfiguration#init才知道 org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss,SSS org.slf4j.simpleLogger.showThreadName=true org.slf4j.simpleLogger.showLogName=true ``` 命令 ``` # 打印info/warn/error日志 mvn clean package # 打印debug/info/warn/error日志 mvn clean package -X ``` - 文件和类的对应 * pom.xml --> ? - Project --> ? - Parent --> ? - Properties --> ? - 对应一次请求:即一次mvn xxx命令,生命周期,Session MavenCli * 还有哪些类?
我是张三
2025年1月23日 20:21
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
eblog
Markdown文件
分享
链接
类型
密码
更新密码