Maven tutorial installation configuration and configuration in idea use maven

Posted by trock74 on Wed, 22 Dec 2021 05:16:15 +0100

Use of Maven tools

Enter the official website to download

Maven – Download Apache Maven

Click download on the right to download

Link, the second is windows download

Download the compressed package to any location for decompression. My location here is under the directory file on disk D

D:\Java\Maven\apache-maven-3.8.2

Then configure the system variable and add the path system variable

The value is under the bin folder of the decompression location

Then check the maven system version mvn -v

Configure maven domestic image and jdk version

Many plug-ins will download files online for the first time when maven runs. The files exist in the local directory and can be modified in pom

Open settings in the conf folder XML file

  • Configure the local warehouse. Select an address under line 53. If it is not changed, it will default to an address of drive C
<localRepository>D:\Java\Maven\apache-maven-3.8.2\repo</localRepository>   

Here I created a new repo folder

  • Then configure the warehouse

Here we use Alibaba cloud servers. Foreign servers are unstable

<mirrors>
    <!-- Alibaba cloud warehouse -->
    <mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>        <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        </mirror>
     <!-- <mirror>  //Both warehouses are OK
      <id>aliyunmaven</id>
      <mirrorOf>*</mirrorOf>
      <name>Alibaba cloud public warehouse</name>
      <url>https://maven.aliyun.com/repository/public</url>
      </mirror>
      -->
  </mirrors>
  • Finally, configure the JDK version number. My version is jdk16. You can change it as much as your JDK version is
 <profiles>
    <id>jdk-16</id>
    <activation>
      <activeByDefault>true</activeByDefault>
      <jdk>16</jdk>
    </activation>
    <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>        <maven.compiler.compilerVersion>16</maven.compiler.compilerVersion>
    </properties>
  </profiles>

idea uses Maven

  • The first is the local maven address
  • The second is the profile address
  • The third is the local warehouse address

Configure POM xml

<!--Project type pom jar war -->
    <packaging>jar</packaging>
    <!--Add dependency-->
    <dependencies>

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.14.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


Click Add to automatically add dependencies


Remember to refresh maven after adding dependencies, which is the function of the new idea
If you don't have this function, you need to search for dependencies on maven's official website and manually copy dependencies

Here we write a small translation case to add jsoup. We need to create a maven project address. Select the address configured locally above. Generally, Maven built in idea is not used

import org.jsoup.Jsoup;
import java.io.IOException;

public class Translate {
    public static void main(String[] args) throws IOException {
        String w = "Apple";
        String u = "https://cn.bing.com/dict/search?q=" + w;
        String o = Jsoup.connect(u).get().select("span[class=def b_regtxt]").get(0).text();
        System.out.println(o);
    }
}

Construction of Maven project

maven core concept:

  1. POM: a file name is POM XML is translated as project model object. maven uses a project as a model to control the process of maven building the project and manage jar dependencies
  2. Agreed directory structure: the directories and files of maven project are specified.
  3. Coordinate: a unique string used to represent the resource.
  4. Dependency management: you can use jar files to manage your project
  5. Warehouse management (understand): where your resources are stored
  6. Life cycle (understanding): maven's process of building projects
  7. Plug ins and goals (understand): the tool used to execute maven builds is plug-ins
  8. inherit
  9. polymerization

The repository is the plug-in jars used by maven and the jars (third-party tools) used by my project

Classification of warehouse

  • The local warehouse is the folder of your computer to store all kinds of jar s

  • Remote warehouse, a warehouse on the Internet that can only be used by using the network

    1. Central warehouse is the most authoritative, so all developers share a centralized warehouse

      Address: Maven Repository: Search/Browse/Explore (mvnrepository.com)

    2. The image of the central warehouse: it is the backup of the central warehouse. It is the image of important cities on all continents.

    3. Private servers are used within the company and in the LAN, not externally.

    The use of warehouse and maven warehouse do not require human participation

  • Developers need to use mysql driver - > Maven first check the local warehouse - > private server - > Image - > central warehouse

pom file

Project Object Model POM XML is the soul of Maven

  1. Coordinate: a unique value that uniquely identifies an item in the Internet

    <modelVersion>4.0.0</modelVersion>
        <groupId>Reverse writing of company domain name</groupId>
        <!-- Unique name of the project ID,One groupId There may be several projects below, which rely on artifactId To distinguish -->
        <artifactId>Custom item name</artifactId>
        <version>Custom version number</version>
    

    For searching the central warehouse

  • Packaging: the extension of the compressed file after packaging. The default jar can not be written

  • Dependencies and dependencies are equivalent to various dependencies to be used in the import project in Java code

    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala3-library_3</artifactId>
        <version>3.0.3-RC1-bin-20210815-1524a5a-NIGHTLY</version>
    </dependency>
    

    maven's life cycle: clean up, compile test reports, package, install and deploy

  1. Unit testing: junit is used, which is a special testing framework (tool)

    junit test content: it tests the methods in the class, and each method is tested independently

    maven uses unit testing to batch test whether a large number of methods in your class meet expectations.

  • Use steps

    • Add dependencies and unit test dependencies in pom '

    • Create a test program in the src/test/java directory of maven project

      1. The name of the Test class is Test + the name of the class you want to Test

      2. The name of the Test method is Test + the name of the method you want to Test

        For example, if you want to create a Test class TestHello, add @ Test to the TestHello method
        Test whether a large number of methods in your class meet expectations.

  • Use steps

    • Add dependencies and unit test dependencies in pom '

    • Create a test program in the src/test/java directory of maven project

      1. The name of the Test class is Test + the name of the class you want to Test

      2. The name of the Test method is Test + the name of the method you want to Test

        For example, if you want to create a Test class TestHello, add @ Test to the TestHello method

Topics: Java IntelliJ IDEA Maven