Java basic tutorial-how Intellij IDEA creates a maven parent-child aggregation project

Java zero-based self-study website, click to understand: https://how2j.cn

Aliyun server, click to learn: https://www.aliyun.com/minisite/goods

table of Contents

Step 1: parent-child-aggregate project

Step 2: Create a new project

Step 3: select maven project

Step 4: project parameters

Step 5: maven settings

Step 6: project location

Step 7: modify pom.xml

Step 8: create subproject

Step 9: select simple items

Step 10: project parameters

Step 11: maven settings

Step 12: project directory

Step 13: TestHutool

Step 14: pom.xml of the subproject

Step 15: pom.xml of the parent project

Step 16: project structure

Step 17: Run the project


Step 1: parent-child-aggregate project

Through maven, you can create parent-child-aggregation projects. The so-called parent-child project means that there is one parent project and multiple sub-projects.
These sub-projects, in terms of business logic, are all summarized under this parent project, and generally speaking, there will be repeated jar package sharing.
Therefore, the common practice is to place the duplicate jar packages under the parent project for dependency, so the subproject does not need to rely on these duplicate jar packages.

Step 2: Create a new project

New Project

Step 3: select maven project

1. Select Maven on the left
2. Check Create from archetype
3. Select org.apache.maven.archetypes:maven-archetype-quickstart
4. Next

Select maven project

Step 4: project parameters

Enter the project parameters as shown

Project parameters

Step 5: maven settings

After the IDEA settings of the previous knowledge point , you can see the maven settings as shown in the figure

maven settings

Step 6: project location

As shown in the figure, then click Finish

Project location

Step 7: modify pom.xml

The pom.xml automatically generated by idea has a lot of things, many of which are not needed. Amend to the following content.
1. The default is jar, modified to pom. Only then can it exist as a parent project.

<packaging>pom</packaging>


2. Increase the dependency of hutool jar and junit package for observing the invocation of it in subsequent subprojects.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.how2j</groupId>

  <artifactId>parentMavenProject</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <name>parentMavenProject</name>

  <description>parentMavenProject</description>

  <packaging>pom</packaging>

  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>4.11</version>

      <scope>test</scope>

    </dependency>

    <dependency>

      <groupId>cn.hutool</groupId>

      <artifactId>hutool-all</artifactId>

      <version>4.3.1</version>

    </dependency>

  </dependencies>

</project>

Step 8: create subproject

The so-called subproject is actually a maven module.
Right-click on parentMavenProject->New->Module.

Create subproject

Step 9: select simple items

1. Select Maven on the left
2. Check Create from archetype
3. Select org.apache.maven.archetypes:maven-archetype-quickstart
4. Next

Choose simple items

Step 10: project parameters

Enter ArtifactId: childMavenProject

Project parameters

Step 11: maven settings

Next is the same maven setting

maven settings

Step 12: project directory

As shown in the figure, then click Finish.

Project directory

Step 13: TestHutool

Create a new TestHutool class under childMavenProject and run it.
It can be found that the classes in the hutool jar can be used. This shows that the child project can use the jar package in the parent project.

TestHutool

package childMavenProject;

  

import java.util.Date;

  

import cn.hutool.core.date.DateUtil;

   

public class TestHutool {

   

    public static void main(String[] args) {

        String dateStr = "2012-12-12 12:12:12";

        Date date = DateUtil.parse(dateStr);

        System.out.println(date);

    }

}

Step 14: pom.xml of the subproject

Observing the pom.xml of the child project, you can find that it has an additional parent, which is a dependency on the parent project.

<parent>

    <artifactId>parentMavenProject</artifactId>

    <groupId>cn.how2j</groupId>

    <version>0.0.1-SNAPSHOT</version>

</parent>

Pom.xml of the subproject

Step 15: pom.xml of the parent project

At this time, open the pom.xml of the parent project, you can find that it has such a module s, which means the association to the sub-project.

<modules>

  <module>childMavenProject</module>

</modules>

Pom.xml of the parent project

Step 16: project structure

Observing the directory structure, you can find that childMavenProject is located under parentMavenProject. So if there are childMavenProject1, childMavenProject2, childMavenProject3 in the future, they will also be placed in such a directory, which is convenient for management.

 

Step 17: Run the project

In the download area (click to enter), there is a runnable project download corresponding to this knowledge point. If you really can't figure it out by yourself, just download and unzip it for comparison.


For more information, click to understand:  https://how2j.cn/k/idea/idea-parent-child/2051.html

Guess you like

Origin blog.csdn.net/java_zdc/article/details/105848418
Recommended