Java automated testing rookie article three Maven article

Note: The learning materials come from the industry's predecessor: Davieyang.DY, for personal learning only, intrusion and deletion.

The practical role of Maven

Maven is a project management tool, which includes a project object model (Project Object Model), a set of standard collections, a project lifecycle (Project Lifecycle), a dependency management system (Dependency Management System), and used to run the definition in The logic of the plugin's goal in the phase of the life cycle. When you use Maven, you use a clearly defined project object model to describe your project, and then Maven can apply cross-cutting logic that comes from a set of shared (or custom) plugins.

  • When the project is very large, you can use Maven to split a project into multiple projects, preferably one module corresponds to one project, which is conducive to the division of labor and collaboration. And messages can still be sent between modules
  • With the help of Maven, the jar package can only be stored in the "warehouse". When the file is needed, the file interface is referenced, and there is
    no need to copy the file to take up space
  • Maven will automatically import the jar package you want to add to the project, not only importing, but also automatically importing the jar packages that the jar package depends on.

Download and configuration

Official download address: After downloading maven
Insert picture description here
, unzip it, and then configure the MVN system environment variable environment variable, as shown in the following figure: The
Insert picture description here
variable name can be M2_HOME or MAVEN_HOME, as long as you can distinguish it, the variable value is the installation directory.
Then add the variable value in the system variable path:
Insert picture description here
PATH:%M2_HOME%\bin;

Check the Maven environment

Insert picture description here
Configure warehouse
configure local warehouse

Configure the physical address of the MVN local warehouse, that is, maven will download the required jar package from the central warehouse to the local. According to your own installation, find the following path:
Insert picture description here
Then open the setting.xml under this path, the configuration items are as follows, pay attention to the XML node, and configure it as You can store the downloaded jar package appropriately by yourself:
Insert picture description here

Configure the central warehouse

Configure the MVN central warehouse: add the following configuration content to the mirror configuration:
Insert picture description here
here is a single configuration mirror address, which is the address of Alibaba Cloud, the official address download is too slow and can be replaced by the configuration of this location.

Generate .m2 address

Copy the file to the following path. If there is no .m2 path, start the command line and execute the command mvn help:system to generate it, and then copy the original configuration file to the generated .m2 path. Integrated development tools such as IDEA will automatically Read the configuration under the .m2 path.
Insert picture description here
Maven common commands

The basic commonly used commands are as follows:

  • mvn archetype: create to create a Maven project
  • mvn compile compile source code
  • mvn deploy release project
  • mvn test-compile compile test source code
  • mvn test runs unit tests in the application
  • mvn site The website that generates project-related information
  • mvn clean clears the build results in the project directory
  • mvn package jar generated according to the project
  • mvn install install jar in the local Repository
  • mvn eclipse: eclipse generates eclipse project files
  • mvnjetty:run start jetty service
  • mvntomcat:run starts tomcat service
  • mvn clean package -Dmaven.test.skip=true: repackage after clearing the previous package, skip the test class

Configure IDEA

IDEA official download address: https://www.jetbrains.com/idea/download/, the default installation is fine, open IDEA, create a new maven project, check the configuration of Maven as shown in the figure, the two configurations pointed by the arrow have been updated Go to the position in the previous step.
Insert picture description here
DEA will prompt you in the lower right corner (note the lower right corner) whether you need to automatically install the jar package of the pom configuration. Once it appears, you must enable it.

Configure pom

Find the pom.xml file, open it, and configure the dependent packages required by the project, as shown in the figure below:

Insert picture description here

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>7.3.0</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-server</artifactId>
      <version>3.141.59</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
    <dependency>
      <groupId>io.appium</groupId>
      <artifactId>java-client</artifactId>
      <version>7.3.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.11.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.11.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.uncommons/reportng -->
    <dependency>
      <groupId>org.uncommons</groupId>
      <artifactId>reportng</artifactId>
      <version>1.1.4</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.inject/guice -->
    <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <version>4.2.0</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/velocity/velocity-dep -->
    <dependency>
      <groupId>velocity</groupId>
      <artifactId>velocity-dep</artifactId>
      <version>1.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.0</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

At this point in the Java environment, the dependencies configured in the Pom can be retrieved in the central library of MVN http://mvnrepository.com/, and you can configure it.

Guess you like

Origin blog.csdn.net/weixin_52385863/article/details/114242437