如何在 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. oracle 求班级平均分

    select * from ( selectclass 班级,subject,avg(grade) avg_gradefrom student_score group by class,subject ...

  2. ubuntu下的文件夹命名

    本文主要学习引用了http://dbua.iteye.com/blog/943945的方法.分享快乐,嘿嘿. 由于本人菜鸟一枚,所以ubuntu选的中文,但是安装后会发现文件夹名称,什么桌面啦,下载啦 ...

  3. Linq操作DataTable

    IEnumerable<DataRow> q = from dr in dt.AsEnumerable()                          where dr.Field& ...

  4. linux 修改oracle字符集

    问题描述:同事在Linux下安装的Oracle,默认的字符集没有修改,使用的是WE8ISO8859P1,不能够支持中文, 问题表现为,客户端录入的中文数据可以显示,提交之后再查询出来后中文会显示为?? ...

  5. oj1089-1096总结(输入输出练习)

    //无限输出类 #include<stdio.h>int main(void){ int a,b; while((scanf("%d %d",&a,&b ...

  6. 455. Assign Cookies

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  7. System.Web.HttpUtility VS System.Web.HttpServerUtility VS System.Net.WebUtility

    HttpUtility 类作为 HttpServerUtility 类的内部使用,HttpServerUtility 通过System.Web.UI.Page.Server属性(WebForm)/Co ...

  8. Servlet.service() for servlet UserServlet threw exception java.lang.NullPointerException 空指针异常

    错误付现: 严重: Servlet.service() for servlet UserServlet threw exceptionjava.lang.NullPointerException at ...

  9. Linux 基础教程 39-作业控制

        在Linux系统中,作业是由一个或多个关联进程组成的.用户可以运行多个作业并可以在作业间切换.而作业控制则是对作业的行为进行控制,允许用户对作业的前后台的进行切换和终止操作等.作业相关的控制命 ...

  10. eclipse mac

    在mac下安装eclipse,挺简单的.只是字体会发虚,有点麻烦. 安装完成后,双击eclipse图标,显示包内容,contents--info.plist,在</dist>前加<k ...