IDEA Project 和 Module 介紹

2022-04-27 10:37 更新

這兩個(gè)概念是 IntelliJ IDEA 的必懂知識(shí)點(diǎn)之一,請(qǐng)務(wù)必要學(xué)會(huì)。

如果你是 Eclipse 用戶(hù),并且已經(jīng)看了上面給的鏈接,那 IntelliJ IDEA 首先告訴你一個(gè)非常重要的事情:IntelliJ IDEA 沒(méi)有類(lèi)似 Eclipse 工作空間(?workspace?)的概念的。很多從 Eclipse 轉(zhuǎn)過(guò)來(lái)的人總是下意識(shí)地要再同一個(gè)窗口管理 n 個(gè)項(xiàng)目,這在 IntelliJ IDEA 是無(wú)法得到。IntelliJ IDEA 提供的體驗(yàn)是:一個(gè) Project 打開(kāi)一個(gè) Window 窗口。

對(duì)于 Project,IntelliJ IDEA 是這樣解釋的:

  • Whatever you do in IntelliJ IDEA, you do that in the context of a project. A project is an organizational unit that represents a complete software solution. It serves as a basis for coding assistance, bulk refactoring, coding style consistency, etc.
  • Your finished product may be decomposed into a series of discrete, isolated modules, but it's a project definition that brings them together and ties them into a greater whole.
  • Projects don't themselves contain development artifacts such as source code, build scripts, or documentation. They are the highest level of organization in the IDE, and they define project-wide settings as well as collections of what IntelliJ IDEA refers to as modules and libraries. 
  • 20170630 更新新地址:https://www.jetbrains.com/help/idea/about-projects.html
  • 對(duì)于 Module,IntelliJ IDEA 是這樣解釋的:

  • A module is a discrete unit of functionality which you can compile, run, test and debug independently.
  • Modules contain everything that is required for their specific tasks: source code, build scripts, unit tests, deployment descriptors, and documentation. However, modules exist and are functional only in the context of a project.
  • Configuration information for a module is stored in a .iml module file. By default, such a file is located in the module's content root folder.Development teams, normally, share the .iml module files through version control.
  • 20170630 更新新地址:https://www.jetbrains.com/help/idea/about-modules.html?search=About
  • 通過(guò)上面的介紹我們知道,在 IntelliJ IDEA 中 ?Project ?是最頂級(jí)的級(jí)別,次級(jí)別是 ?Module?。一個(gè) ?Project ?可以有多個(gè) ?Module?。目前主流的大型項(xiàng)目結(jié)構(gòu)都是類(lèi)似這種多 ?Module ?結(jié)構(gòu),這類(lèi)項(xiàng)目一般是這樣劃分的,比如:?core Module?、?web Module?、?plugin Module?、?solr Module? 等等,模塊之間彼此可以相互依賴(lài)。通過(guò)這些 ?Module? 的命名也可以看出,他們之間應(yīng)該都是處于同一個(gè)項(xiàng)目業(yè)務(wù)情況下的模塊,彼此之間是有不可分割的業(yè)務(wù)關(guān)系的。

    所以我們現(xiàn)在總結(jié):一個(gè) ?Project? 是由一個(gè)或多個(gè) ?Module? 組成,模塊之間盡量是處在同一個(gè)項(xiàng)目業(yè)務(wù)的的情況下,彼此之間互相依賴(lài)關(guān)聯(lián)。這里用的是 盡量,因?yàn)?IntelliJ IDEA 的 ?Project ?是一個(gè)沒(méi)有具備任何編碼設(shè)置、構(gòu)建等開(kāi)發(fā)功能的,主要起到一個(gè)項(xiàng)目定義、范圍約束、規(guī)范等類(lèi)型的效果,也許我們可以簡(jiǎn)單地理解為就是一個(gè)單純的目錄,只是這個(gè)目錄命名上必須有其代表性的意義。

    下面我們以著名的 ?spring-framework? 項(xiàng)目為例介紹多 ?Module ?的結(jié)構(gòu)的:

    • 項(xiàng)目主頁(yè):https://github.com/spring-projects/spring-framework
    • 該項(xiàng)目的 ?Project ?命名是:spring-?framework?。該目錄主要作用為各個(gè) ?Module? 的頂層目錄進(jìn)行約束,告訴協(xié)同者,這個(gè)目錄下都是 ?spring-framework? 相關(guān)的,我絕不會(huì)放 Android 相關(guān)源碼、文檔、文件在上面的。該目錄并不是以一個(gè)實(shí)際性的目錄來(lái)體現(xiàn)的,所以你訪(fǎng)問(wèn)主頁(yè)是看不到的,但是當(dāng)你 ?checkout? 的時(shí)候,你必須為這個(gè)項(xiàng)目命名,至于命名默認(rèn)就是 ?spring-framework?。
    • 該 ?Project? 下有二十來(lái)個(gè) ?Module?,各個(gè) ?Module ?的命名也是有含義的,比如:?spring-core?、?spring-jdbc?、?spring-jms?、?spring-orm?、?spring-web?、?spring-webmvc? 等等,我們通過(guò)這些命名也能清楚地知道他們要表達(dá)的含義,這些 ?Module ?下也都各自有 ?src? 編碼目錄,可以自行編碼和構(gòu)建。

    Project 和 Module 介紹

    • 相比較于多 ?Module ?項(xiàng)目,小項(xiàng)目就無(wú)需搞得這么復(fù)雜。只有一個(gè) ?Module ?的結(jié)構(gòu) IntelliJ IDEA 也是支持的,并且 IntelliJ IDEA 創(chuàng)建項(xiàng)目的時(shí)候,默認(rèn)就是單 ?Module ?的結(jié)構(gòu)的。
    • 如上圖 Gif 圖演示,在輸入 ?Project name ?的時(shí)候,?Module name? 和 ?Module file Location ?自動(dòng)進(jìn)行改變,同時(shí) ?Project location? 和 ?Module file Location? 完全一樣,這也就表示,?Project ?目錄和 ?Module ? 目錄是同一個(gè),所以此時(shí) ?Project ?目錄下就會(huì)有 ?src? 目錄,但是我們應(yīng)該明白其本質(zhì)還是 ?Module ?的目錄。

    關(guān)于 IntelliJ IDEA 的 ?Project ?和 ?Module? 終于解釋清楚了,但是由于 IntelliJ IDEA 官網(wǎng)上又有一段話(huà)對(duì)此解釋得不夠好,特別是對(duì) Eclipse 用戶(hù)來(lái)講:https://www.jetbrains.com/idea/help/eclipse-faq.html,其中有這樣兩句話(huà):

    • An Eclipse workspace is similar to a project in IntelliJ IDEA
    • An Eclipse project maps to a module in IntelliJ IDEA

    你可以把 IntelliJ IDEA 的 ?Project ?當(dāng)做 ?workspace ?使用,IntelliJ IDEA 也是支持的,但是就像我們前面解釋的那么那么多,這樣是非常不符合其初衷的,所以請(qǐng)別把這段話(huà)當(dāng)做教義去學(xué)習(xí)。對(duì)此 zeroturnaround 的大牛也有針對(duì)此進(jìn)行了說(shuō)明:https://www.jrebel.com/blog/intellij-idea-tutorial


    以上內(nèi)容是否對(duì)您有幫助:
    在線(xiàn)筆記
    App下載
    App下載

    掃描二維碼

    下載編程獅App

    公眾號(hào)
    微信公眾號(hào)

    編程獅公眾號(hào)