如何在IntelliJ IDEA中使用另一个项目中的类? [英] How do I use classes from another project in IntelliJ IDEA?

查看:1584
本文介绍了如何在IntelliJ IDEA中使用另一个项目中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个IntelliJ IDEA Java项目; ProjectA和ProjectB。我想从ProjectB中的ProjectA导入和使用代码。我该怎么做?

I have two IntelliJ IDEA Java projects; ProjectA and ProjectB. I want to import and use code from ProjectA in ProjectB. How do I do this?

在Eclipse中,我只需转到ProjectB的构建路径设置并添加ProjectA。

In Eclipse I would simply go to ProjectB's Build Path settings and add ProjectA.

推荐答案

您可以在这些项目之间创建依赖关系(使项目B依赖项目A)本质上是编译项目A,然后将其已编译的jar作为项目B的依赖项进行编译或运行。
您也可以手动执行此操作。

You can create dependency between these projects (Make project B dependent on project A) What it does is essentially compiles project A first then put its compiled jar as dependency to Project B for compiling or running. You can do this manually also.

IDEA中的步骤(如果遵循以下最佳实践,则不需要以下步骤):

Steps in IDEA ( You won't need these below steps if you follow below mentioned best practices):


  1. 右键单击项目并选择打开的模块设置

  2. 转到依赖项选项卡

  3. 单击加号并添加要使用的模块。

最佳做法:


  1. 切勿在另一个项目中使用项目类,始终创建一个漂亮的界面并在其他项目中使用该界面。

  2. 如果可能的话,使用依赖注入来管理不同的项目及其依赖项(这在内部使用接口来完成)

  3. 使用诸如ant / maven之类的构建工具/ ivy等来管理构建过程。

  4. 享受:)

  1. Never use project class in another project, always create a nice interface and use that interface in other projects.
  2. If possible use Dependency Injection to manage different projects and their dependencies (this internally uses interfaces to do this)
  3. Use build tool like ant/maven/ivy etc to manage build process.
  4. Enjoy :)

这篇关于如何在IntelliJ IDEA中使用另一个项目中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆