Maven的Dependency怎么找,maven设置

come from : http://www.javaeye.com/topic/240424

用了Maven,所需的JAR包就不能再像往常一样,自己找到并下载下来,用IDE导进去就完事了,Maven用了一个项目依赖 (Dependency)的概念,用俗话说,就是我的项目需要用你这个jar包,就称之为我的项目依赖你这个包,换句话说,你这个JAR包就是我这个项目的Dependency。

      于是很多人在刚用Maven的时候,就会在加Dependency时栽跟头,明明我手头上就有需要的JAR嘛,它偏不让我用,什么道理,跟着就是生闷气,少点耐性的,就会说Maven怎么怎么不爽,其实,如果你理解了Maven的一些机制,一些配置及管理的流程,就会觉得它其实很爽。

      我暂且不谈Maven的长篇概念(其实我也还很多很多不懂),我先给大家介绍一个简单的查找方法,让你很快地找到所需的Dependency,并配置到你的pom.xml里面去。

最快捷方法:

GOOGLE搜索:maven 你需的jar包名称 repository

比如我要做EJB,我要找jboss-j2ee.jar的Dependency

就在GOOGLE里输入

maven jboss-j2ee repository

在结果的第一条,进去你就可以在页面里找到下面这段

<dependency>
    <groupId>jboss</groupId>
    <artifactId>jboss-j2ee</artifactId>
    <version>4.0.2</version>
</dependency>

你把上面这段代码贴到你的Maven项目的pom适当的位置去,然后运行maven,Maven就会自动下载所需的jar及相关的pom信息,你不用管它,Maven会帮你下载,并放到适当的位置。

我再举几个查找实例,让大家熟悉一下怎么搜索

下面我要找 struts.jar

在GOOGLE里输入

maven struts repository

就很快可以找到下面这段代码

<dependency>
    <groupId>struts</groupId>
    <artifactId>struts</artifactId>
    <version>1.2.9</version>
</dependency>

我还要找hibernate

在GOOGLE输入

maven hibernate repository

在结果的第一条记录出现了很多个不同的hibernate链接(因为有很项目用了hibernate嘛)我们就选用org.hibernate的,看起来专业点嘛,呵呵

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>3.2.6.ga</version>
</dependency>

好了,SSH可不能少了中间那个S,Spring嘛

在GOOGLE里输入

maven spring repository

在第一个结果进去,我们这次选择org.springframework的,

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>2.5.5</version>
</dependency>

      其它的我就不一一举例了,大家自己试试,其实这个方法可以解决大多数常用包的Dependency查找,不用专门记那些repository下载站的网址嘛,不过始终这是非正规做法,其它要想更好地找到想要的Dependency,还是得了解maven repository是啥玩意,它的local repository库概念,以及怎样建立自己自己repository服务器(这个我还不太熟,这里就不讲了,大家自己去其它地方找相关资料,免得被我误导了)

      Maven的repository,说白了就是dependency的仓库,它按照一定的规则将dependency存放起来,以作缓存,如果本机的 repository找不到某个dependency,它就会自动去找到网上其它相关联的repository,找到的话将其下载至本地,那么下次它就不再去其它地方下载了,直接从本地获取。

本地的repository在哪可找到?

默认repository地址:当前用户的私人目录 + .m2

如果你设置了Maven目录下的conf/setting.xml的local repository属性,则不再是默认的repository地址,而使用你指定的地址。

找到下面这段,将它复制一份,放到注释外面,改成你自己的repository路径即可

<localRepository>c:/mvn repository/</localRepository>

Maven按什么规则去存放Dependency?

下面我们来拿hibernate来作例子,以介绍在Maven运行并下载到所需的Dependency后怎么缓存到本地的repository

下面这个是hibernate的Dependency配置

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>3.2.6.ga</version>
</dependency>

它被下载后将会被存放至

%repository%/org/hibernate/hibernate/3.2.6.ga/hibernate-3.2.6.ga.jar

其中%repository%就是你本地的repository目录!

不知大家看出来了没有,它是按你的Dependency的groupid + artifactid + version,然后以点号作分隔,来建立文件夹的,最后建立的文件夹如下

|--org

    |--hibernate

        |--hibernate

            |--3.2.6.ga

                |--hibernate-3.2.6.ga.jar

可能会有些人会问那个jar包的名字是怎么来的,细心的人或许早己观察出来了,没错,就是artifactId + "-" + version + ".jar"。

好了,下面我们按这个规则去找一下另外一个Dependency放在哪吧

<dependency>

    <groupId>com.google.gwt</groupId>

    <artifactId>gwt-servlet</artifactId>

    <version>1.4.61</version>

</dependency>

我们现在根据之前所讲的规则,先进入你的本地repository目录,然后进入com目录,再进入google目录,再进入gwt目录,再进入 gwt-servlet目录,再进入1.4.61目录,跟着你就可以发现一个名叫gwt-servlet-1.4.61.jar和其它一些文件了大家试试看吧!《注意:你在首次使用这个Dependency时,一定要运行Maven,让它去下载到需要的包,下载成功后你才能按上面的步骤找到,不然你找不到别乱骂我,你都没下载的话,肯定找不到了:(       》

      在你掌握这个技巧之后,有很多时候你可以根据所需包的package路径,自己摸它的Dependency,这个就不详谈了,总之你了解后,找Dependency不会成问题的,非常简单的事

      这个时候,就有另一些用户会提出疑问,我要用的jar包是我自己开发的,或是朋友,或是网友传来的,在网上根据找不到,怎么在Maven里面使用它们呢? 其实办法是肯定有的,而且也不难,也是常常要用到的,下面我来简单介绍一下几个方法:

前提,肯定是你先写到pom里面去,组织随你写,artifact 写你的jar名,或者随便写,version你也写上你想要的,如

<dependency>

    <groupId>com.mydomain.ooooo</groupId>

    <artifactId>my-jar</artifactId>

    <version>100.1.1</version>

</dependency>

一、用好你的IDE!

      现在大家用的IDE,无非就是Eclipse,或者netbeans,或是JBuilder,据我所知,现在大部份人都向前两者转移,这些IDE都提供了对Maven的支持,不过通常都需要你装插件才能打开并使用Maven的项目,怎么装maven插件我就不介绍了,大家自己上网找找别人写的文章吧。在你装好插件后,通常会有一个libraies目录,里面列出你项目要用到的jar包,你右键点击本地还没有添加进去的jar包,选择本地安装(Manually install artifact),选中你那个特殊的jar包,然后IDE会自动调用maven插件完成整个安装配置过程,下次你就不再这样子做了,因为你本地 repository己经有了。

二、手动将jar包放到本地repository里面去

根据先前介绍的maven repository存放规则,自己建好目录,改好jar的文件名,自己拷进去,如

进到你本地的repository目录

建一个com文件夹,点进去

再建一个mydomain文件夹,点进去

再建一个ooooo文件夹,点进去

再建一个my-jar文件夹,点进去

再建一个100.1.1文件夹,点进去

再把你的jar包,改名为my-jar-100.1.1.jar,放到100.1.1文件夹里面去,

       然后运行你的maven项目,看看它会不会build成功,事实上如果你之前做的文件夹没错,大小写没问题,或没其它小失误,你的项目就可以正常用到你自己的jar包了

三、直接到http://repo1.maven.org/maven2/ 查找

      不过这也需要你知道maven repository的存放规则才知道怎么在网站上找,找到你还得自己写dependency的配置代码,本人不常来这里找。不过各有所好,大家选择合适自己的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Book Description Manage your Java and JEE project dependencies with ease with this hands-on guide to Maven Overview Improve your productivity by efficiently managing dependencies. Learn how to detect and fix dependency conflicts Learn how to share transitive relations and to visualize your dependencies In Detail Managing dependencies in a multi-module project is difficult. In a multi-module project, libraries need to share transitive relations with each other. Maven eliminates this need by reading the project files of dependencies to figure out their inter-relations and other related information. Gaining an understanding of project dependencies will allow you to fully utilize Maven and use it to your advantage. Aiming to give you a clear understanding of Maven’s functionality, this book focuses on specific case studies that shed light on highly useful Maven features which are often disregarded. The content of this book will help you to replace homebrew processes with more automated solutions. This practical guide focuses on the variety of problems and issues which occur during the conception and development phase, with the aim of making dependency management as effortless and painless as possible. Throughout the course of this book, you will learn how to migrate from non-Maven projects to Maven, learn Maven best practices, and how to simplify the management of multiple projects. The book emphasizes the importance of projects as well as identifying and fixing potential conflicts before they become issues. The later sections of the book introduce you to the methods that you can use to increase your team’s productivity. This book is the perfect guide to help make you into a proud software craftsman. What you will learn from this book Learn how to use profiles, POM, parent POM, and modules Increase build-speed and decrease archive size Set, rationalize, and exclude transitive dependencies Optimize your POM and its dependencies Migrate projects to Maven including projects with exotic dependencies Approach An easy-to-follow, tutorial-based guide with chapters progressing from basic to advanced dependency management. Who this book is written for If you are working with Java or Java EE projects and you want to take advantage of Maven dependency management, then this book is ideal for you. This book is also particularly useful if you are a developer or an architect. You should be well versed with Maven and its basic functionalities if you wish to get the most out of this book. Table of Contents Chapter 1: Basic Dependency Management Chapter 2: Dependency Mechanism and Scopes Chapter 3: Dependency Designation (advanced) Chapter 4: Migration of Dependencies to Apache Maven Chapter 5: Tools within Your IDE Chapter 6: Release and Distribute Appendix: Useful Public Repositories Book Details Title: Apache Maven Dependency Management Author: Jonathan Lalou Length: 158 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2013-10-25 ISBN-10: 1783283017 ISBN-13: 9781783283019

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值