Maven入门


开发久了,总结一下吧。

一、Maven配置阿里云源

阿里云源文档地址:https://developer.aliyun.com/mvn/guide

编辑 ~/.m2/settings.xml 文件:


    
        aliyunmaven
        central,jcenter
        阿里云公共仓库
        https://maven.aliyun.com/repository/public
    


这个 url 是 central 仓和 jcenter 仓的聚合仓,所以只需要拦截这两个仓库即可,其它仓库的包还是去 maven 仓库中心下载。

这是配置全局的拦截器,mirrorOf 是配置拦截哪些镜像。

二、配置私服地址

首先配置私服的账号和密码,这个必须在 settings.xml 中配置:


    
        
            snailwu-demo-releases
            releases-1637456160565
            xxxxxxxxxx
        
        
            snailwu-demo-snapshots
            snapshots-1637456200873
            xxxxxxxxxx
        
    


1、在项目中配置


    
    
        
        
            aliyun-public
            aliyun-maven
            https://maven.aliyun.com/repository/public
            
                false
            
        
        
            snailwu-demo-releases
            releases
            https://snailwu-maven.pkg.coding.net/repository/demo/releases/
            
                false
            
        
        
            snailwu-demo-snapshots
            snapshots
            https://snailwu-maven.pkg.coding.net/repository/demo/snapshots/
            
                false
            
        
    


2、配置在 settings.xml

配置在这里需要先声明一个 profile:


    
        
            snailwu-demo
            
                
                
                    aliyun-public
                    aliyun-maven
                    https://maven.aliyun.com/repository/public
                    
                        false
                    
                
                
                    snailwu-demo-releases
                    releases
                    https://snailwu-maven.pkg.coding.net/repository/demo/releases/
                    
                        false
                    
                
                
                    snailwu-demo-snapshots
                    snapshots
                    https://snailwu-maven.pkg.coding.net/repository/demo/snapshots/
                    
                        false
                    
                
            
        
    

三、发布配置

在项目中的 pom.xml 文件中进行配置:


    
    
        
            
            snailwu-demo-releases
            releases
            https://snailwu-maven.pkg.coding.net/repository/demo/releases/
        
        
            
            snailwu-demo-snapshots
            snapshots
            https://snailwu-maven.pkg.coding.net/repository/demo/snapshots/
        
    

Maven 会自动根据包的名字是否带有 SNAPSHOT 来区分发布到哪个仓库。

四、生成 javadoc 和 sources jar包


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.apache.maven.plugins
                maven-source-plugin
                
                    
                        attach-sources
                        
                            jar
                        
                    
                
            
            
            
                org.apache.maven.plugins
                maven-javadoc-plugin
                
                    
                        attach-javadocs
                        
                            jar
                        
                    
                
            
        
    

五、命令

如果是 SpringBoot 项目,会自动给你生成 maven 的 jar 和命令,如图:

此时,你的机器不需要安装 maven,直接使用 mvnw 这个脚本即可运行,比如 ./mvnw clean compile install deploy

如何跳过测试?

  1. 在pom.xml 的 properties 下配置 true

  2. 在执行 ./mvnw 命令时,添加 -Dmaven.test.skip=true,实际就是执行时动态传一个变量,1中是静态配置