[Java] IDEA 2022.2 How to create a Maven project (detailed explanation with pictures and text)

Preface

  For beginners, creating a new project is already a big challenge. Especially now, many articles on the Internet are about old versions of IDEA. How to create Maven projects is not very friendly to novices who use the new version of IDEA to learn, so Today I will write an article on how to use the new version of IDEA to create a Maven project.

Create a new Maven project

first step

Click File => New => Project

Insert image description here

Step 2

Click New Project and fill in the corresponding information

Insert image description here

  • Name: Fill it in as you like, but try to use English and avoid Chinese and Pinyin;
  • Location: This is the location where the project is stored. Try to store it on a drive other than the C drive. I set it under the installation path of IDEA so that it is easier to find the workspace;
  • Create a Git project: We don’t need this currently, so you can leave it unchecked;
  • Language: There is no doubt that Java is the choice~
  • Build system: Select Maven. If there is no Maven option here, it may be that your Maven is not installed or has not been set up properly. You can read my other article about the installation and configuration of Maven ;
  • JDK: The version I am using here is relatively old, and it does not have to be 1.8. It depends on the version installed on your own computer;
  • Add sample code: A basic Hello World will be generated. Here we can check it first or not. Later, we will not check this option;
  • Group ID: Usually it is the inversion of company name + domain name. If there is no company here, I use my own name instead of the company name, so fill in com.sue;
  • Artifact ID: I actually don’t know what it is, but I found that it always changes with the change of the name. I don’t think these two are related at all. You won’t believe me if I tell you. If you know, please leave a message in the comment area to tell me.

After filling in the corresponding information, we click Create to create a basic Maven project.


Simple analysis of Maven projects

Let's take a brief look at what he created for us:
Insert image description here

  1. On the side, the project tree has a src and pom.xml. This pom.xml is the most important file in Maven. It is the Maven configuration file and where we import dependencies;
  2. There are two branches in the src directory: main and test. As the name implies, one is where the code is written, and the other is where the code is tested;
  3. Here, we can first click the small gear next to the project, select the tree appearance, and then uncheck the compressed empty intermediate software package. This way our blank software package will not be collapsed. You can try checking it and you will know the effect. , I have installed the Chinese plug-in here, you can install it if necessary, but in fact it is best to use the English plug-in.
    Insert image description here
  4. The code in Main is the code generated for us by checking the example code earlier. We can click on this small triangle to run the same playback
    Insert image description here
    console output.
    Insert image description here
    Open the pom.xml file to see
    Insert image description here
    modelVersionthe version of the module;
    the rest grouIdshould look familiar to you. Yes, it is the parameters we filled in when configuring and generating the project earlier;
    propertiesit is the configuration of the entire project, using JDK8 and Chinese encoding;

Since they are all Maven projects, we might as well import a jar package and give it a try:
remember dependenciesto write it propertiesafter. As for why, this is a human regulation. I don’t know what you think. After all, Maven is more about convention than configuration.
Insert image description here

 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

dependencids is the outermost package, which means that there are all imported dependencies.
Dependency is a specific dependency. The middle is filled in information. The junit Jar package is a package specially used for testing.
After importing the junit package, we can Using the @Test annotation can provide multiple executable entries for our program, saving us the need to write a lot of main
Insert image description here

public class MyTest {
    
    
    @Test
    public void Test01(){
    
    
        Main test = new Main();
        System.out.println(test);
    }
}

Here is a website for finding Maven. If you want to know some information about Maven, just look for the Maven repository
on this website . Enter the Jar package you want to find here,
Insert image description here
select the version,
Insert image description here
and then paste it into our code.
Insert image description here

Guess you like

Origin blog.csdn.net/Daears/article/details/127421290