Maven

Posted 架构成长之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven相关的知识,希望对你有一定的参考价值。

maven安装

下载安装包,maven官网

配置环境变量

MAVEN_HOME = D:\\hnisi\\local\\apache-maven-3.0.4

PATH=%PATH%;%MAVEN_HOME%\\bin

MAVEN_OPTS = -Xms128m -Xmx512m

M2_HOME = D:\\hnisi\\local\\apache-maven-3.0.4

maven依赖冲突

maven是最先声明原则来引用相应版本的jar,而gradle是最新版本原则。

首先要定位错误的jar包,在项目启动时加上vm参数-verbose:class,这时会打印类似如下信息:

[Loaded java.util.TaskQueue from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.TimerThread from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.Timer$1 from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded org.quartz.impl.StdScheduler from file:/Users/lilu/repository/org/quartz-scheduler/quartz/2.2.1/quartz-2.2.1.jar]
[Loaded java.util.Hashtable$KeySet from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded org.quartz.SchedulerMetaData from file:/Users/lilu/repository/org/quartz-scheduler/quartz/2.2.1/quartz-2.2.1.jar]
[Loaded java.util.TreeMap$NavigableSubMap from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.TreeMap$AscendingSubMap from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.TreeMap$NavigableSubMap$EntrySetView from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.TreeMap$AscendingSubMap$AscendingEntrySetView from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.TreeMap$NavigableSubMap$SubMapIterator from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.TreeMap$NavigableSubMap$SubMapEntryIterator from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded org.quartz.simpl.JobWrapper from file:/Users/lilu/repository/org/quartz-scheduler/quartz/2.2.1/quartz-2.2.1.jar]
[Loaded org.quartz.simpl.TriggerWrapper from file:/Users/lilu/repository/org/quartz-scheduler/quartz/2.2.1/quartz-2.2.1.jar]
[Loaded sun.util.locale.provider.TimeZoneNameUtility from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded sun.util.locale.provider.TimeZoneNameProviderImpl from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded sun.util.locale.provider.TimeZoneNameUtility$TimeZoneNameGetter from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded sun.util.resources.TimeZoneNamesBundle from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded sun.util.resources.TimeZoneNames from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded sun.util.resources.en.TimeZoneNames_en from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.LinkedHashMap$LinkedKeySet from /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar]
group_1.testJob_1 运行在: Sun Apr 30 08:41:57 CST 2017
[Loaded org.quartz.DisallowConcurrentExecution from file:/Users/lilu/repository/org/quartz-scheduler/quartz/2.2.1/quartz-2.2.1.jar]
[Loaded org.quartz.utils.ClassUtils f

maven的pom.xml会显示或隐式的依赖类包,如果不想依赖某个类包,可以通过用exclusion排除之,如下:

<dependency>  
        <groupId>org.unitils</groupId>  
        <artifactId>unitils-testng</artifactId>  
        <version>${unitils.version}</version>  
        <scope>test</scope>  
        <exclusions>  
            <exclusion>  
                <artifactId>junit</artifactId>  
                <groupId>junit</groupId>  
            </exclusion>  
        </exclusions>  
</dependency>  

maven仓库

maven仓库主要有两种:

  • remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问

  • local repository:存放在本地磁盘的一个文件夹,例如,windows上默认是C:\\Users\\{用户名}\\.m2\\repository目录

repository里存放的都是各种jar包和maven插件。当向仓库请求插件或依赖的时候,会先检查local repository,如果local repository有则直接返回,否则会向remote repository请求,并缓存到local repository。也可以把做的东西放到本地仓库,仅供本地使用;或上传到远程仓库,供大家使用。 

maven mirror:

mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。

通过以下图解:

没有配置mirror

配置mirror

此时,B Repository被称为A Repository的镜像。

如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像。换句话说,任何一个可以从仓库Y获得的构件,都能够从它的镜像中获取。

maven命令

mvn deploy:deploy-file -DgroupId=com.hnisi.syj -DartifactId=acegi-security -Dversion=1.0.5 -Dpackaging=jar 
-Dfile=D://hnisi/workspace/publicservice/WebRoot/WEB-INF/lib/acegi-security-1.0.5.jar
-Durl=http://xx/nexus/content/repositories/syj-releases/ -DrepositoryId=syj-rxx

 

以上是关于Maven的主要内容,如果未能解决你的问题,请参考以下文章

Spring+SpringMVC+MyBatis+Maven框架整合

关于mysql驱动版本报错解决,Cause: com.mysql.jdbc.exceptions.jdbc4Unknown system variable ‘query_cache_size(代码片段

如何在 Apache Felix maven-bundle-plugin 中设置 Provide-Capability 标头?

Maven FTP部署:无法创建目录

如何从詹金斯访问maven属性?

向小军 “小黑” 正式加入 Maven Skateboards