The whole process of mvn from download and installation to pure command line creation of the first mvn program (coding, compiling, testing, installation, packaging) is decomposed in detail

1. maven download and installation:

  Notes on downloading a.maven: If you are windows, please select number 1, if you are linux, please select number 2, download address: http://maven.apache.org/download.html;

  b. After downloading, decompress and install, and configure the environment variables. Now take the Windows installation package as an example to introduce the installation and configuration process:

    b-1. After downloading, open the folder where the downloaded file is located, and create a maven folder in the root directory of a certain disk (personal habits, newly installed software is to be installed under the disk root, which is easy to find), and decompress the compressed file to the Under contents:

    

    b-2. Since this file is free of installation, the next step is to configure the environment variables. If it is used temporarily, you can set the environment variables through the set command (set PATH="D:\maven\apache-maven-3.5 .3\bin";%PATH%). If you want to use it for a long time, go back to the computer desktop (win7), right click on my computer -> properties -> advanced -> environment variables -> create a new environment variable MAVEN_HOME=D:\maven\apache-maven-3.5.3

Find the path environment variable and add it at the end; %MAVEN_HOME%\bin; after saving, click win+r, enter the cmd command, press Enter to enter the command line, enter: mvn -v to view the output: (please install jdk in advance)

    

  b-3. Go to the conf folder of the maven installation directory, copy a copy of settings.xml to the .m2 folder of the user directory, there should be a folder repository (central warehouse) under this folder, copy The purpose is that modifications to the settings.xml file by the current user will not affect all computer users:

    

  b-4. Go back to the command line and enter mvn help:system, maven will download some important files to the central repository. ok now you can start writing your first maven project

2. Create maven file

  a. Create a new folder on the desktop named helloworld, create a file named pom.xml under the folder, and enter the following content:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 5         http://maven.apache.org/maven-v4_0_0.xsd">
 6         
 7 <modelVersion>4.0.0</modelVersion>  
 8   
 9     <!-- The unique logo of the company or organization, and the path generated during configuration is also generated from this, such as com.winner.trade, maven will put the jar package of the project into the local path: /com/winner/trade - ->  
 10      <groupId>com.yichu.mytest</groupId>  
 11    
12      <!-- The unique ID of this project, there may be multiple projects under one groupId, which are distinguished by artifactId -->  
 13      <artifactId>helloworld< /artifactId>  
 14    
15      <!-- The current version number of this project-->  
 16      <version> 1.0 . 0 -SNAPSHOT</version>  
 17    
18      <!-- Packaging mechanism, such as pom,jar, maven- plugin, ejb, war, ear, rar, par, default is jar -->  
 19      <packaging>jar</packaging>  
 20      <name>hello world programe</name>
21       <!-- Configure the dependencies of the test unit --> 
22     <dependencies>
23     <dependency>
24     <groupId>junit</groupId>
25     <artifactId>junit</artifactId>
26     <version>4.7</version>
27     <scope>test</scope>
28     </dependency>
29     </dependencies>
30 </project>
31         

  b. Create the following directory structure in turn

    b-1.src/main/java: This folder is the core code folder of maven. Files such as dao, service, controller, pojo and other files of web projects are stored here by default.

    b-2.src/test/java: This folder is obviously where our test files are stored.

    b-3.src/main/resources: The default path of the main class to store the configuration file.

    b-4.src/test/resources: The location where the configuration file of the test class is stored.

    b-5.logs: log storage path

  c. Of course, our helloworld program does not use so many folders yet, but b-1 and b-2 still have to be created. After creation, go to the b-1 folder, according to the pom.xml configuration file declaration, create the following directory structure: com/yichu/mytest, and create your own java file in this directory. Such as HelloWorld.java

 1 package com.yichu.mytest;
 2 public class HelloWorld{
 3     public String getstring(){
 4         return "hello world";
 5     }
 6     public static void main(String args[]){
 7         
 8         System.out.println(new HelloWorld().getstring());
 9         
10     }
11     
12 }

  d. Go back to the b-2 directory and create your own test class, such as helloworldtest.java

 1 package com.yichu.mytest;
 2 import static org.junit.Assert.assertEquals;
 3 import org.junit.Test;
 4 public class helloworldtest{
 5     @Test
 6     public void testgetstring(){
 7         HelloWorld hello=new HelloWorld();
 8         String res=hello.getstring();
 9         assertEquals("hello world",res);    
10     }
11     
12     
13 }

  e. At this point, our coding work is over, and the next step is to compile, run, install, and package.

    e-1. Compile: Go back to the command line, cd to the helloworld folder, enter: mvn clean compile to compile, and then you will see the download prompt.

 

    e-2. Test: Input in the same directory: mvn clean test, wait for the download.

 

    e-3. Packaging: Considering that if the main entry of the program is not declared, it cannot be executed using java -jar ***, we need to add such a paragraph to pom.xml, and then enter: mvn clean package in the same directory. Pack

 1 <build>
 2     <plugins>
 3         <plugin>  
 4                 <groupId>org.apache.maven.plugins</groupId>  
 5                 <artifactId>maven-shade-plugin</artifactId>  
 6                 <version>1.4</version>  
 7                 <configuration>  
 8                     <createDependencyReducedPom>false</createDependencyReducedPom>  
 9                 </configuration>  
10                 <executions>  
11                     <execution>  
12                         <phase>package</phase>  
13                         <goals>  
14                             <goal>shade</goal>  
15                         </goals>  
16                         <configuration>  
17                             <transformers>  
18                                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
19     <!--主程序入口指点-->                                    <mainClass>com.yichu.mytest.HelloWorld</mainClass>  
20                                 </transformer>  
21                             </transformers>  
22                         </configuration>  
23                     </execution>  
24                 </executions>  
25         </plugin>  
26     </plugins>
27     </build>

    The effect is as shown in the figure:

     e-4. In the same directory, cd to the target directory, then enter java -jar helloworld-1.0.0-SNAPSHOT.jar and press Enter to see the effect as shown:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023760&siteId=291194637