[spring cloud] – create Maven parent-child structure based on springboot – IntelliJ idea

Time:2021-11-26

As a java developer who transferred from eclipse to idea, he suffered a lot in the process of adapting to idea, including the most basic idea directory structure, running and debugging, code completion, editor style and so on. At the beginning, he didn’t adapt at all.

So far, I have been using idea for three months. Although I still focus on sts4 of eclipse series, I am basically familiar with the use of idea. In addition to the recent integration of microservices, it is necessary to integrate multiple completed microservices into a separate Maven project as Maven modules.

At this time, the benefits of idea are reflected. The structure of the idea project itself is that the project contains modules, so it is easier for developers who are used to the idea style to create Maven projects containing multiple spring boot projects.

First, let’s take a look at the comparison between the directory structure of the idea project and that of eclipse (STS):

idea eclipse(STS)
project workspace
module project

For what to do at this timeMaven project for creating parent-child structureGenerally speaking, according to the understanding of eclipse hierarchy:
workspace -> maven peoject -> maven module
But under idea:
maven project -> maven module
I think there is no difference between the two ides in creating parent-child Maven projects.

Talk less and get down to business.

Create Maven parent project:

  1. Create idea spring initializr,
    File – > new, select spring initializr to ensure that the SDK environment is correct. Click next[spring cloud] - create Maven parent-child structure based on springboot - IntelliJ idea
  2. Note: at this time, select Maven POM in type. The advantage of this is to prevent the generation of paths we don’t need, such as Src. It’s OK not to select POM, but you need to manually delete SRC and other paths after creation.[spring cloud] - create Maven parent-child structure based on springboot - IntelliJ idea
  3. Select the dependency of the parent Maven project – note that the dependency selected here is for all child modules[spring cloud] - create Maven parent-child structure based on springboot - IntelliJ idea
  4. The last step is to confirm the information to finish – pay attention to the interface in this stepproject LocationAnd lower halfMore SettingThe path of the part should be correct. Basically, there will be no problem, but because it is an automatically set path, you’d better confirm it and finally finish.

After the creation is successful, the lower right corner of the idea will prompt whether to automatically add Maven dependencies. Clickenable auto importAll right

At this point, a maven parent project is created. Open the project tab, or press windowsAlt+1, MAC downCommand+1You can see the project structure of the newly created project.
You can see that only one POM has been generated:[spring cloud] - create Maven parent-child structure based on springboot - IntelliJ idea
This POM is the POM of a normal spring boot project

Create subproject

proposal: subprojects are created in Maven format instead of the spring boot project.

  1. Parent project mail, create module[spring cloud] - create Maven parent-child structure based on springboot - IntelliJ idea
  2. Select Maven in the left column and click next
  3. Make sure that the parent item and path are correct[spring cloud] - create Maven parent-child structure based on springboot - IntelliJ idea
  4. Click finish
  5. The directory structure is as follows:[spring cloud] - create Maven parent-child structure based on springboot - IntelliJ idea
  6. The parent tag is defined in the POM of the subproject[spring cloud] - create Maven parent-child structure based on springboot - IntelliJ idea
  7. The module tag of the child project is defined in the POM of the parent project:[spring cloud] - create Maven parent-child structure based on springboot - IntelliJ idea

At this point, we can write code happily.