Idea creates a maven project, using maven

This article explains in detail the use of IDEA to create Maven projects and the basics of Maven.
 

1. Open IDEA, select File->New->Project in the upper right corner

 

2. Select Maven as shown in the figure (you can add it according to your own needs, otherwise the loading speed is very slow)

3. Add the Groupld, ArtifactId, and Version required by the project (the configuration requirements of the three will be described in detail later) Xiaobian named it here


4. Set Project name (project name) Project location (workspace)


5. Since the editor has added all configuration files, the waiting time is longer.
6. After waiting patiently, a box will appear in the lower right corner, as follows: Click the option in the circle! ! ! (automatic configuration related files)


7. Next, let's look at its file structure, as shown below:


(1) In most cases, put the main code of the project in the src/main/java directory (following the Maven convention). If there is no other configuration, Maven will automatically search for the main code in this directory at runtime.

(2) In order to keep the structure of the project clear, IDEA automatically created a test file and put the main code and test code in separate directories.

(3) pom.xml is the core configuration file of Maven. Its full name is (Project Object Model, project object model). When we use Maven, we usually configure it in this pom.xml.

8. pom. xml! ! ! core configuration

<?xml version="1.0" encoding="UTF-8"?>
<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>com.feiyu.web</groupId>
    <artifactId>hello-world</artifactId>
    <version>1.0-SNAPSHOT</version>
 
</project>



(1) The first line is the xml header, which specifies the version information and encoding method of the xml document. Currently, the default version number of version is 1.0 and the encoding method is UTF-8.

(2) <project> is the root element of all pom.xml, which declares some POM-related namespaces and xsd elements. These elements are not necessarily added in pom.xml, but using these attributes can make third-party tools, such as IDE The xml editor in helps developers quickly edit POM.

(3) The first child element <modeVersion> under the root element specifies the version of the current POM template. For most developers now, Maven 2 Maven 3 

This version number can only be 4.0.0.

(4) <groupId> defines which project group the project belongs to. In enterprise-level development, it is usually related to the organization and company to which the project belongs. For example: there is a project named ourApp on BATcode, so the name of groupId should be com.BATcode.ourApp. The codes in this article are com.feiyu.helloMaven.

(5) <artifactId> defines the unique ID of the current Maven project in the project group. In this article, the Hello Maven artifactId is hello-Maven. In actual development, other artifactIds will be assigned, while the previous groupId may be different child Projects (modules) are assigned an artifactId.

(6) <version> defines the current version number of the Hello Maven project. 1.0-SNAPSHOT is the default initial version number of IDEA. With the development progress of the project, the version number is upgraded to 1.1, 2.0, etc.

(7) When there is no actual java code, we can completely create a pom.xml of a Maven project, which shows that Maven can make the project object model independent of the code to the greatest extent, which fully embodies the principle of decoupling and idea! It saves time for developers and greatly shortens the project development cycle. When the project develops to a stable stage, when upgrading the version, developers do not need to modify the actual java code, but only modify the pom.xml, which makes Maven widely used.

9. Write the main code
The main code of the project is different from the test code. The main code will be packaged into the final component, while the test code is only used when running the test and will not be packaged. We create the file com/feiyu/web/helloworld/helloWorld.java in src/main/java, the code is as follows:

package com.feiyu.web.helloworld;
 
public class HelloWorld {
 
    public String sayHello() {
        return "Hello Maven!";
    }
 
    public static void main(String[] args) {
        System.out.println(new HelloWorld().sayHello());
    }
}


 Generally speaking, the groupId and artifactId defined in pom.xml should match the package name, which can make the structure clearer.

10. Run the main code


In the console we see that the operation is successful and the output Hello Maven! 


Original link: https://blog.csdn.net/wfy2695766757/article/details/81189291

Guess you like

Origin blog.csdn.net/sinat_40641604/article/details/103457915