How do I use classes from another project in IntelliJ IDEA?

前端 未结 3 1380
萌比男神i
萌比男神i 2020-12-17 08:43

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?

In Eclipse I would simply g

3条回答
  •  离开以前
    2020-12-17 09:30

    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.

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

    1. Right click on project and select open module settings
    2. Go to dependencies tab
    3. click plus sign and add the module you want to use.

    Best practices:

    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 :)

提交回复
热议问题