maven

alias 配置别名

echo "alias mcp='mvn clean package'" >> ~/.bashrc
echo "alias mci='mvn clean install'" >> ~/.bashrc
source ~/.bashrc

阿里云maven [settings.xml]

<mirror>
    <id>nexus-aliyun</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus aliyun</name>
    <url>https://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

阿里云maven [pom.xml]

<repository>
    <id>maven-aliyun</id>
    <url>https://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是镜像,会拦截对远程仓库的请求到配置的地址上

反应堆

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

常用插件

    <!--  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>
    
                <!-- 用于加载application.yml信息,在pom文件可读取到application.yml中信息 -->
<plugin>
    <groupId>it.ozimov</groupId>
    <artifactId>yaml-properties-maven-plugin</artifactId>
    <version>1.1.3</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <files>
                    <file>src/main/resources/application.yml</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin>
versions-maven-plugin
https://maven.apache.org/maven-release/maven-release-plugin/index.html
maven-release-plugin

pom.xml中使用属性

maven定义了很多变量属性,参考这里

  • 内置属性
${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"

  1. 清理空文件夹 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文件,大致内容如下(使用国内源):

#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
# 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

在容器中打包

docker run --rm -v "$PWD":/usr/src/mymaven -v "$HOME/.m2":/root/.m2 -w /usr/src/mymaven maven:3.9.6-eclipse-temurin-21 mvn clean package

私服

  • 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
upload artifact
  • generate token keygen / user0 m

  • add alias and token into ~/.m2/settings.xml or $M2_HOME/conf/settings.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

<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>
conclusion

only use to publish jar to other users, can not be consider as private-server.

mvnd

mvn daemon

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
    • 还有哪些类?
作者:admin  创建时间:2023-07-12 21:24
最后编辑:admin  更新时间:2025-09-19 10:08