如何在 IntelliJ IDEA 中管理本地类库

一般来说,如果项目是基于 Maven 管理工具的,我们会在 pom.xml 中添加 dependency 来管理依赖。但有时也会遇到要用的类库不在 Maven Repository 的情况。

比较常见的解决方法是把 JARs 安装到 local repository。这种方法的缺点是,只对本机有效,别人拿到你的代码后不能直接使用。这在团队协作中很不方便。

使用 IntelliJ IDEA 的项目设置可以很容易的解决上面的问题。

下面我给大家演示一下。

定义类库

IntelliJ IDEA 支持定义3种级别的类库:

  • global

    对所有项目有效

  • project

    对本项目下所有模块有效

  • module

    仅对本模块有效

我推荐定义 project 级别的类库,然后在 module 中引用。

  1. 在项目根目录下创建lib文件夹,并把jar包复制进去。

    ├─.idea
    ├─lib
    │ taobao-sdk-java-auto_1550480258080-20190218.jar
    └─src
  2. 打开 Project Structure 窗口,选中 Project Settings 下的 Libraries 标签,然后点击右侧的+,选择Java

  3. 在弹出的窗口中选择lib目录下的jar包。

  4. 然后 IntelliJ IDEA 会询问是否添加到现有的 module 中:

  5. 全部设置好后点击OK

添加依赖

如果是先定义的类库,后创建的 module ,那就需要我们手动添加依赖。

打开 Modules 窗口,选择一个 module 添加依赖:

打包设置

定义完类库,添加好依赖,这只能保证编译正常。要想让运行不出错,还需要把类库添加到生成的jar包中。

打开 Artifacts 窗口,在右侧的 Available Elements 列表中找到上面添加的类库。

根据自己的需要选择Put into Output Root/Extract Into Output Root

总结

我们把第三方 jar 包存放在项目中,并通过定义 project 级别的类库进行引用,以保证在团队协作中,项目的依赖关系不丢失。

当然这一切的前提是把 IntelliJ IDEA 的项目文件也放到 VCS 中。稍后我会在另一篇中详细介绍 IntelliJ IDEA 的项目文件管理。

参考

How to manage local libraries in IntelliJ IDEA的更多相关文章

  1. How to add libraries to “External Libraries” in WebStorm/PhpStorm/Intellij

    Stack Overflow Questions Developer Jobs Tags Users   Log In Sign Up Join Stack Overflow to learn, sh ...

  2. Awesome Go精选的Go框架,库和软件的精选清单.A curated list of awesome Go frameworks, libraries and software

    Awesome Go      financial support to Awesome Go A curated list of awesome Go frameworks, libraries a ...

  3. Awesome Go

    A curated list of awesome Go frameworks, libraries and software. Inspired by awesome-python. Contrib ...

  4. Awesome Go (http://awesome-go.com/)

    A curated list of awesome Go frameworks, libraries and software. Inspired by awesome-python. Contrib ...

  5. Docker Resources

    Menu Main Resources Books Websites Documents Archives Community Blogs Personal Blogs Videos Related ...

  6. /etc目录深入理解

    /etc This is the nerve center of your system, it contains all system related configuration files in ...

  7. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  8. Go 语言相关的优秀框架,库及软件列表

    If you see a package or project here that is no longer maintained or is not a good fit, please submi ...

  9. A Simple Makefile Tutorial

    A Simple Makefile Tutorial A Simple Makefile Tutorial: http://www.cs.colby.edu/maxwell/courses/tutor ...

随机推荐

  1. 根据车辆品牌获取品牌所属公司,车标logo,创建年份等基本信息

    接口:http://api.besttool.cn/?c=Car&a=brand 请求方式:post 参数: appid 请联系博主QQ987332767获取,注明车标接口,测试appid: ...

  2. sqlserver2014新特性

    1.SQL Server 2014新特性探秘(1)-内存数据库 在传统的数据库表中,由于磁盘的物理结构限制,表和索引的结构为B-Tree,这就使得该类索引在大并发的OLTP环境中显得非常乏力,虽然有很 ...

  3. 第六章 Windows应用程序对键盘与鼠标的响应

  4. python之selenium调用js(execute_script)

    转载: http://www.cnblogs.com/fnng/p/3230768.html 本节重点: 调用js方法 execute_script(script, *args) 在当前窗口/框架 同 ...

  5. Spring MVC的handlermapping之请求分发如何找到正确的Handler(RequestMappingHandlerMapping)

    这个思路同样是通过在AbstractHandlerMethodMapping里面来实现getHandlerInternal()实现自己的方法来处理寻找正确的处理器,不懂得请看上一篇. protecte ...

  6. 解决Error running 'index.jsp : Address localhost:1099 is already in use的方法

    晚上在idea中  启动服务器一次后 正常运行,但是改了注解配置后  报错,报错为Error running 'index.jsp : Address localhost:1099 is alread ...

  7. Redis数据结构(三)

    Redis五种数据类型: (1)字符串 (2)字符串列表 (3)有序字符串集合 (4)哈希 (5)字符串集合 其中(4)(5)应用最多. key命名的注意: (1)不要过长,尽量不要超过1024,会降 ...

  8. C++继承-重载-多态-虚函数

    C++ 继承 基类 & 派生类 一个类可以派生自多个类,这意味着,它可以从多个基类继承数据和函数.定义一个派生类,我们使用一个类派生列表来指定基类.类派生列表以一个或多个基类命名,形式如下: ...

  9. Python + Robotframework + Appium 之APP自动化测试小试牛刀(Android)

    Robotframework如何好?这里先不说了~ Python更不用说了~ Appium前面的文章有介绍~ 今天直接来Python+Robotframework+Appium 三者结合起来,对And ...

  10. Ubuntu的常识使用了解4

    寻找文件的「名称」 在Linux系统当中,文件的数量非常非常的多, 需要使用查找工具来高效查找指定文件位置: