如何在IntelliJ IDEA中使用另一个项目中的类? [英] How do I use classes from another project in 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):
- 右键单击项目并选择打开的模块设置
- 转到依赖项选项卡
- 单击加号并添加要使用的模块。
最佳做法:
- 切勿在另一个项目中使用项目类,始终创建一个漂亮的界面并在其他项目中使用该界面。
- 如果可能的话,使用依赖注入来管理不同的项目及其依赖项(这在内部使用接口来完成)
- 使用诸如ant / maven之类的构建工具/ ivy等来管理构建过程。
- 享受:)
- Never use project class in another project, always create a nice interface and use that interface in other projects.
- If possible use Dependency Injection to manage different projects and their dependencies (this internally uses interfaces to do this)
- Use build tool like ant/maven/ivy etc to manage build process.
- Enjoy :)
这篇关于如何在IntelliJ IDEA中使用另一个项目中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!