Hi, I’m YourBatman.

How do I add separate dependencies to the Module Module? How do I know which Module modules use the Spring framework and which are Web projects? IDEA how to add Jar package? The War play packages?

Skillful and correct use of IntelliJ IDEA is what a “master” should look like, because that is your face. The last article introduced the two most important concepts in IDEA: Project and Module. I believe you will never compare the Project of IDEA to the Workspace of Eclipse after reading it, and have a deeper understanding of IDEA.

This article continues to understand IDEA’s management of projects and modules. Managing projects is the basic function of an IDE, but often the most basic is the most important and most easily overlooked. Therefore, this article is the foundation for you to better understand the IDEA management maven structure, Gradle structure, and Spring Boot project structure. Great oaks from little acorns grow. No matter how numerous and varied the superstructure is, the same path is finally reflected by the Project Structure, so as to provide developers with almost the same coding experience.

In this paper, an outline

Version of the agreement

  • IntelliJ IDEA: 2020.3.1

The body of the

A Project Structure is a window that you open occasionally/often during development, but rarely pay attention to. Different from the general setting window, it is very close to the project and has a certain degree of difficulty to understand, if set improperly, the project may not run or even unable to code (such as compilation error, JAR package can not be found, etc.), so I do a common people are not willing to do things, to explain it in detail, I believe that difficult things will be achieved.

Based on the hello Project case established above, this paper continues to study the management of its Project Structure. From structure view, to modify customization, then the question comes, how to open a Project Project structure page?

How to open the Project Structure?

It seems to be a simple operation, but it actually contains your understanding of IDEA Project and Module. Otherwise, you will not know where to start. You may have been a programmer for years and may not know where to start.

To do this, select Hello and right click:

But I’m sorry, it’s not on the right menuProject StructureOptions. Project Structure is a Structure window for the Project dimension. When you select Hello, it is just a module, so it will automatically pop up the operation menu for this module, not Project. You might say: I clickedOpen Module SettingsThe Project Structure window is also open. Yes, you might have it open but it’s not, just because you put them together.

Note: Understanding the concepts of Project and Module of IDEA is the basis of all operations on IDEA. The previous article has been very detailed (probably the most complete web) introduction to both of them, can take a few minutes to learn. Click here to get to the elevator

Three ways to open it

There are at least three ways to open a Project structure window, and this article gives you examples.

  1. The top menuFile -> Project Structure

  1. Click the shortcut button in the upper right corner

  1. Shortcut keys (recommended)

This is my personal favorite, and depending on how you set it, my shortcut is CTRL + Shift + Alt + S.

Long-winded: it is recommended that you use the IDEA shortcut keys, it will greatly improve the efficiency of coding, and look like a master. Basically, 50 or so keyboard shortcuts are enough to remember, and that’s one of your core competencies when you have muscle memory in the long run


Open the Structure page of the Hello project as shown below:

Explanation: Why don’t you need to mouse over items?

Do you notice that opening the project structure does not require the mouse to select anything (shortcuts are optional), which means the mouse is out of focus?

It is not difficult to answer this question if you are already familiar with the Project concept of IDEA. Each Project corresponds to one window, and the relationship is strictly 1:1. In other words, the current window represents Project, so operating the menu bar at the top of this window must be applied to the Project, so why select anything specifically? Again, Project is just a logical concept, and you can’t select it if you want, so just think of Windows as it. Does this sound like the Java this keyword call?

Finally, the answer to this question is: Project is always “selected” as long as the mouse is in the IDEA window (which is an active window).

Project Structure analysis

The project structure window is open, so here’s the focus. You can see the menu bar on the left, which is divided into three parts:

  • Project Settings: Project Settings (most important), explained in this article
  • Platform SettingsPlatform Settings, also known as global Settings. Used to manage SDKS (such as JDK, Kotlin’s SDK, etc.), global library.
    • Generally, the global JDK is configured here; for example, I manage multiple JDK versions because I often try multiple versions

  • -Leonard: Problems. General project problems will be reflected in this (such as dependency inconsistency problems, etc.), in short, the number of problems consistent so that it is zero is optimal

Each TAB in Project Settings is the most commonly used and most concerned. Each TAB page is explained and illustrated below.

Page Project situation

This window shows basic information about the Project itself. For example: name, SDK version, language level and so on, relatively simple.

For elements on this page, say:

  1. Why the SDK version and not the JDK version? A: Because IntelliJ IDEA is the JVM platform IDEA, it supports not only Java but also other languages such as Kotlin, so it is more abstract to write SDK
  2. Why specify the language level when specifying the SDK? A: Because the SDK version does not directly determine the language level. If you’re using JDK 11, you can still set the language level to 8 to compile/run
    1. This is the advantage of integrated development environments, which allow for easy customization of multiple environments
  3. Both the SDK and the language level Project can be specified as the global default
    1. These configuration Modules are integrated by default, but can modify their own. For example, Module 1 was compiled with Java 5 and Module 2 was compiled with Java 11, which is allowed

The Module page is

The Module page is the most important of all, even the most important. After all, Module is the real thing, where all the source code, configuration, dependencies, and so on are, so there’s a lot to learn.

Note: The Tests package has access to Sources, but not the other way around.

Each module can independently manage its own dependencies, which are recorded in the module’s own.imL file.

Knowledge:

  1. A Module with the same name is created by default when a Project is created
  2. Module defaults to the SDK, language level and other Settings of the Project, of course, you can also specify their own
  3. Each Module can manage its own dependencies, which can be a two-party library or a three-party library……
  4. Dependencies for this module are stored by default in the project{moduleName}.imlIn the file

New rely on

Since a Module can manage its own dependencies, how do you add new dependencies to the Module?

For example, if you want to add a Commons-io JAR dependency to the Hello module, click the + sign in the lower left corner of the Dependencies TAB and select Library:

Then select, if not select New Libarary… Create a new one:

The following shows how to select Java and From Maven libraries:

Create a Java dependency library

New Library New menu option select Java option:

This approach is simple: select a JAR from your machine (or a directory containing the JAR, document). The advantage is that it is very portable and network independent, but the disadvantage is that these jars must already exist on your machine.

Create a New Maven dependency library

New Library New menu option select From Maven option:

Type GAV (or keyword search) to locate the JAR, which is very easy to use because Maven is so easy to use. The disadvantage of course is that in most cases you need to rely on the network, unless you already have the corresponding JAR in your local repository.


Add a new dependency in each of these two ways, and then look at the dependency of the Hello module, as shown in the following figure:

Iml: hello.iml: hello.iml: hello.iml: hello.iml: hello.iml

Dependencies are added and the source code can be used normally:

Range of dependence

When you create a dependency in New Library, whichever way you select it, it will pop up a window that lets you select the scope of the dependency

  • Module Library: Module level, used only by this Module, invisible to other modules
  • Project Library (selected by default) : Project level, under which all modules are visible and selected
  • Global Library: Global level, visible and used by any project

In this case commons-io is the module level and Commons-lang3 is the project level. So when the hello-client module adds a dependency, it can also see commons-lang3 dependency (but not commons-io) :

Libraries page is

When a Library is a dependency required by all/most modules, it can be upgraded to a project-level dependency and extracted to the Libraries TAB for unified management. As shown, because the commons-lang3 created in the previous steps is project level, it will also appear here.

There is no need to go over how to create/add project-level dependencies, as the [New Dependencies] section above explains. The only difference is that after the page is selected, you no longer need to select the scope of Library (because it is project-level), instead you can select the module to use:

Of course, you can cancel all of them (cancle), and the JAR will just be created, not on any Module.

Note: For a multi-module Project, it is recommended that all jars used by the Project be placed here for unified management. When the module needs to be used, choose directly as needed instead of adding separately, which is convenient for unified management

Page called Facets of

Facets can be understood as the framework area used to configure Project items. It can see the framework, language and other conditions used by each Module Module of the Project, and can also configure them.

Like the Spring framework, if a module uses it, it can be configured here. The advantage is that you will find that with the power of IDEA, it gives you a good IDEA of where to configure, and you can change it, allowing you to implement the configuration interface. In addition to Spring, other frameworks such as Hibernate do the same

The types of Facets (Language/framework) currently supported are:

Facets IDEA corresponding to the module will automatically detect and detect. If no, you can add it manually.

To better illustrate what this TAB does, here’s a production project of my own to see it in action:

Note: Different facets correspond to different right-most window content configuration items.

From this window, you can see your current Project, which modules use the Spring framework and which are Web projects. It plays a very important role in the overall control of each module from the perspective of Project. For example, if you find that a module does not need to be a Web Project (it does not need to provide service interfaces externally), it must be caused by multiple packages or unclear responsibilities, so you can immediately address the specific problems and eliminate hidden dangers.

In practice, I often use this function to locate the nature of modules. For example, if it is a common module, it is definitely not allowed to be a Web project, and if it does not need to rely on Spring, it is definitely not allowed to be a Spring project. Because strict control of Jar package dependence and engineering nature is an effective means to deal with large projects.

Of course, Facets also serves to let the IDEA compiler know your module. For example, if you are a Web module, If it is not reflected in Facets, Then IDEA does not know you and cannot provide you with some convenient operations of web.

Artifacts page is

IDEA how to add Jar package? How to pack a War bag? Here, serve

In today’s world of Maven, the IDEA package is rarely used, but sometimes it is useful for local debugging, and it is still useful for understanding Maven packaging.

Artifacts is a concept that is not particularly easy to understand, artifact is a concept in Maven that is borrowed from IDEA. Indicates how a module is packaged, such as JAR, war exploded, WAR, EAR, and so on. Artifact is a collection of project resources, integrating compiled Java files, resource files, etc. There are different ways to integrate, such as jar, war, war exploded, and so on. For a module, there is an Artifact that can be deployed, similar to Maven’s package package.

Note: The difference between war and exploded is that the war is not compressed and that exploded allows you to see the effects of modifying files in real time

Here’s how to package the Hello module into a Jar:

Once configured, just use the top menu bar Build -> Build Artifacts to type this Jar package:

After executing this command, you can see the hello.jar package in the Output Directory. Then java-jar.\hello. Jar will run (because we are calling executable jar package). About the use of IDEA packaging also includes the executable JAR package, Fatjar, package reference JAR package and so on, here will not expand, later will be put in a separate article to summarize the various ways together for a chat.

In general, we are required by Intellij IDEA to configure Facets and Artifacts (although some can be automatically identified) for the purpose of identifying these files and integrating various plug-in implementation functions (such as automatic configuration, automatic packaging), all for coding experience and coding efficiency.

How do modules depend on other modules

A medium – to large-sized project usually has several modules, each with its own function. There are dependencies between modules. For example, the common XXX-core module is generally depended on by almost all other modules. The external Library Library knows how to do this, so how to increase module dependencies for this project?

For example, the Hello-core module has a Person class:

The Hello-service module also needs the Person class and its functions, so you need to rely on the Hello-core module as follows:

To add a Dependency, select Module Dependency… Options:

Select the modules you need to rely on in this project:

Select the hello-core module and attach it to the Hello-service:

Click OK and you’re done. This dependency is also represented in the hello-service.iml configuration file:

Thus, we can use the Person class in the Hello-service module normally:

public static void main(String[] args) {
    System.out.println(new Person());
}
Copy the code

Perfect.

conclusion

This paper makes a comprehensive analysis of each TAB page of Project Structure of IntelliJ IDEA. As far as my short-sighted vision is concerned, it may be the only one in the whole Internet to write this content. Many students think IntelliJ IDEA does not need special study and analysis, they will use it to import maven project, run the main function to start Spring Boot, but I do not think so.

The difference between a novice and an expert is not when things go smoothly, but who can solve problems quickly and who can only fail. I believe the difference in salary also shows in this. I’ve seen “masters” use their most commonly used tools very 666, isn’t that what technology should look like? After all, it’s impossible to think that someone who uses one-finger zen to type code is a genius

Ok, let’s leave the topic of IDEA here. In fact, I have several themes in mind, such as:

  • How does IDEA proactively identify imported Maven projects that cannot be automatically identified? How does that work?
  • IDEA how to install executable Jar package? How to hit FatJar? How to package external Jar package (bulk package)?
  • How does IDEA take advantage of its latest Http Client scripting capabilities, combined with sniffing controllers, to quickly perform local tests?
  • .

Do you have any pick? Please leave a message and tell me if there is a lot of demand, otherwise this topic will come to an end for the time being and we will continue to spend our time on other topics.

This article considers the question

This article belongs to the column: IDEA, the background reply column name can get all the content. This article has been included in *www.yourbatman.cn*.

Finished reading may not understand, understand may not be. Here, the last three questions to help you review:

  1. How does the Module set the JDK version separately?
  2. IDEA how to add jar package?
  3. Get creative: Maven uses POM to manage project structure. How does IDEA recognize it?

Recommended reading

  • If anyone compares the Project of IDEA to the Workspace of Eclipse again, I will be anxious with him
  • IntelliJ IDEA is 20 years old, and the 2-day anniversary event is free for developers
  • IntelliJ IDEA 2020.3 is officially released, and the last version of the year is very martial morality

System.out.println("Give it a thumbs up!);
print_r('Focus on [BAT utopia]! ');
var_dump('Private chat BROTHER A: FSX1056342982');
console.log("Give it a thumbs up!);
NSLog(@"Focus on BAT's Utopia!");
print("Private chat A: FSX1056342982");
echo("Give it a thumbs up!);
cout << "Focus on BAT's Utopia!" << endl;
printf("Private chat A: FSX1056342982");
Console.WriteLine("Give it a thumbs up!);
fmt.Println("Focus on BAT's Utopia!");
Response.Write("Private chat A: FSX1056342982");
alert("Give it a thumbs up!);
Copy the code

YourBatman: Spring Framework open source contributor, Java architect, domain expert. Article not title party, not sensationalism, each article into a series to break a knowledge point, each series may be the best/only network. Pay attention to basic skill accomplishment, bottom foundation decides superstructure. Existing IDEA series, Spring N series, Bean Validation series, date and time series…… Pay attention to free access