Maven Project Management - Essay (Getting Started)

Table of contents

foreword

What is Maven

Advantages of Maven

What are the core concepts of Maven

What is POM

What is dependency management

what is a plugin

what is warehouse

overview

1. Build

2. Dependence

installation and configuration

1. Download

 2. Windows Maven installation

1) Unzip to the specified directory

2) Configure environment variables

3) Directory structure

3. Configuration

1. Create a folder in the root directory

2.settings.xml file configuration

3. The settings.xml file configures the mirror warehouse

Common commands

Create a Maven project

1. Manual creation

1) Maven project directory structure

2) pom.xml configuration file

3) Description

JUnit

1. Concept

2. Steps to use

Common APIs

1) @Test annotation

2) assertEquals () method

3) assertTrue() and assertFalse() methods

4) assertNotNull() and assertNull() methods

5) @Before and @After annotations

6) @BeforeClass and @AfterClass annotations

7) @Ignore annotation

Lombok

1. @Data annotation

2. @AllArgsConstructor and @NoArgsConstructor annotations

3. @Builder annotation

Summarize


foreword

Maven is an open source project management tool that helps developers automate building, testing and deploying Java projects


What is Maven

Maven is an open source project management tool that helps developers automate building, testing, and deploying Java projects.

  • A tool for unified management of Jar packages (handling dependencies and conflicts of Jar packages)
  • Tools for easy compilation and testing, and packaging code

Advantages of Maven

Maven can automatically build, test and deploy Java projects, reducing the time and error rate of manual operations. It can also manage project dependencies, avoiding version conflicts and missing dependencies.


What are the core concepts of Maven

Maven's core concepts include project object model (POM), dependency management, plugins, repositories, and lifecycles.


What is POM

POM is the project object model, which is the core file of the Maven project, including the basic information of the project, dependencies, build configuration and plug-ins.

The pom.xml file is the core file of the Maven project (very important, the Maven project cannot be without pom)

pom.xml What is he doing?

  • Simply put, pom.xml is a description of the Moven project

What is dependency management

Dependency management is an important function of Maven, which can automatically manage project dependencies, including downloading, installing and updating dependent libraries; it provides a central warehouse that can help us automatically download components.

Search the website, all kinds of dependencies are in this website, click to enter the website to search for the dependencies (build) you need, such as: servlet, jsp, test(Junit), etc...: Click to enter

Here are some of my commonly used dependencies

<!--依赖第三方配置-->
  <dependencies>
    <!-- 依赖 junit,jar包 便于test测试 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>test</scope>
    </dependency>

    <!-- 依赖 servlet,jar包 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <!-- 依赖 jsp,jar包 -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.3</version>
      <scope>provided</scope>
    </dependency>


    <!--添加依赖Lombok 自动生成JavaBean 所需的 tos,hashCode等-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.26</version>
      <scope>provided</scope>
    </dependency>


    <!-- 依赖jstl -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>


    <!-- 依赖 mysql,jar包 -->
    <dependency>
      <groupId>com.mysql</groupId>
      <artifactId>mysql-connector-j</artifactId>
      <version>8.0.33</version>
    </dependency>


    <!--依赖gson jar包 -->
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.10.1</version>
    </dependency>

  </dependencies>
  <!--忽略test测试 -->
<!--       <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build> -->

what is a plugin

Plug-ins are an extension mechanism of Maven, which can extend the functions of Maven, such as compilation, testing, packaging and deployment.


what is warehouse

Warehouse is an important concept of Maven. It is a place to store dependent libraries and plug-in libraries, including local warehouses and remote warehouses.


overview

Maven is a tool maintained by the Apache Software Foundation that provides build and dependency management support for Java projects. Maven is a software project management tool based on the project object model (POM), which can manage project components, reports and documents through a small piece of description information.

1. Build

Maven is an excellent build tool . It can help us automate the build process, from cleaning, compiling, testing to generating reports, to packaging and deployment. We don't need and shouldn't enter commands over and over again, click the mouse again and again, what we have to do is to use Maven to configure the project, and then enter simple commands (such as: mvn clean install), Maven will help us deal with those tedious task.

In Java project development, construction refers to the process of producing a product from raw materials .

  • raw materials
    • Java source code
    • HTML-based Thymeleaf documentation
    • picture
    • configuration file
  • product
    • A project that can run on the server
  • The main links included in the construction process:
    • Cleanup: delete the results of the previous build, ready for the next build
    • Compile: Compile Java source program into *.class bytecode file
    • Test: run the test program prepared in advance
    • Report: Generate a comprehensive information on the results of the test just now
    • Pack
      • Java project: jar package
      • Web engineering: war package
    • Installation: Store the jar package or war package generated by a Maven project into the Maven warehouse
    • deploy
      • Deploy jar package: deploy a jar package to the Nexus private server
      • Deploy the war package: deploy the war package to the Tomcat server with the help of relevant Maven plug-ins (such as cargo)
  • Build a project out of the IDE environment


 

2. Dependence

Maven is also a dependency management tool and project information management tool. It provides a central warehouse that can help us download components automatically.

If resources such as classes, interfaces, configuration files, etc. of B project are used in A project, then we can say that A depends on B. For example:

  •  junit-4.12 depends on hamcrest-core-1.3
  • thymeleaf-3.0.12.RELEASE depends on ognl-3.1.26
    • ognl-3.1.26 depends on javassist-3.20.0-GA
  • thymeleaf-3.0.12.RELEASE 依赖 attoparser-2.0.5.RELEASE
  • thymeleaf-3.0.12.RELEASE 依赖 unbescape-1.1.6.RELEASE
  • thymeleaf-3.0.12.RELEASE depends on slf4j-api-1.7.26

Specific problems to be solved in dependency management:

  • Download jar package: After using Maven, the jar package will be downloaded from the standard remote warehouse to the local
  • Dependencies between jar packages: automatic completion through dependency transitivity
  • Conflicts between jar packages: By adjusting the dependent configuration, some jar packages will not be imported

installation and configuration

1. Download

Download Maven address: click to enter

 2. Windows Maven installation

1) Unzip to the specified directory

Notice:

  • Maven depends on Java, so you must ensure that JDK is installed and the environment variables are configured
  • There should be no Chinese characters or spaces in the path

2) Configure environment variables

Notice:

  • MAVEN_HOME or M2_HOME: Specify the Maven root directory
  • Path: Specify the bin directory where Maven-related command programs are located
  • MAVEN_HOME:D:\java\Maven\apache-maven-3.9.1-bin\apache-maven-3.9.1
  • Path:%MAVEN_HOME%\bin

I have configured it before, so I will not reconfigure it, so I will show you the configuration steps! If you follow the steps to configure, there will be no problem!

 

Try it after configuration

$ mvn -v

The test passed as shown in the figure

2.1) Note that if it is a Linux or Mac system

To edit the .bash_profile file in the user directory

# Edit the .bash_profile file in the user directory

# 编辑用户目录下的.bash_profile文件
$ vim .bash_profile

########### Maven1.0 Evn ##################
# export MAVEN_HOME=/Users/circleus/apache-maven-3.5.4
# export MAVEN_HOME=/Users/circleus/aliyun-maven-3.6.0
# export PATH=$MAVEN_HOME/bin:$PATH
 
########### Maven2.0 Evn ##################
# export M2_HOME=/Users/circleus/aliyun-maven-3.6.0
# export M2_HOME=/Users/circleus/apache-maven-3.5.4
# export PATH=$PATH:$M2_HOME/bin
 
########### Maven3.0 Evn ##################
# export PATH=/Users/circleus/aliyun-maven-3.6.0/bin:$PATH

3) Directory structure

  • bin directory: contains the running script of mvn
  • Boot directory: a framework containing a class loader, which maven uses to load its own class library
  • conf directory: directory for storing configuration files (core configuration file settings.xml file)
  • lib directory: Contains dependent class libraries when maven runs

3. Configuration

1. Create a folder in the root directory

Path: D:\java\Maven\apache-maven-3.9.1-bin

Create: LocalAddress folder

First create a folder yourself such as: create a LocalAddress folder

The naming of folders is arbitrary, it is recommended to name meaningful! !

2.settings.xml file configuration

路径:D:\java\Maven\apache-maven-3.9.1-bin\apache-maven-3.9.1\conf

Configuration: settings.xml file

Configure local warehouse

<!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
-->

<localRepository> D:\java\Maven\apache-maven-3.9.1-bin\LocalAddress</localRepository>

Look at the picture:

3. The settings.xml file configures the mirror warehouse

<!--阿里云Maven镜像配置--> 
<mirror>
  <id>nexus-aliyun</id>
  <mirrorOf>*</mirrorOf>
  <name>Nexus aliyun.</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

 Note: Open settings.xml, Ctrl+F search  http://0.0.0.0/ to jump to mirrors

<mirrors>

        Configure Alibaba Cloud mirror here

</mirrors>

The installation and configuration are almost complete, the last step is to copy and paste the settings.xml file into the LocalAddress folder

Note: Because I have configured it before, so many files are stored in it, and there are not so many files when I just configured it


Common commands

View maven version

mvn -v 

compile

mvn compile

Pack

mvn package

test

mvn test

clear target

mvn clean

Install the jar package into the local warehouse

mvn install


Create a Maven project

1. Manual creation

1) Maven project directory structure

  • project name
    • src: project source code
      • main: store the main program
        • java: store Java source code
          • package name + class name
        • resoures: store resource files
        • webapp: web resource
      • test: Store the test program
        • java: store Java test source code
          • package name + class name
        • resources: store resource files
    • target: This directory is generated by maven
    • pom.xml: Maven project core configuration file

2) pom.xml configuration file

In Maven, the <packaging> element can specify different packaging types. In addition to "jar", common packaging types include:

  1. JAR (Java Archive): used to package Java class files and related resources.
  2. WAR (Web Application Archive): Used to package Java Web applications, including Java class files, Web resources, and deployment descriptors.
  3. EAR (Enterprise Application Archive): Used to package enterprise-level Java applications, including WAR files and other components (such as EJB modules) and deployment descriptors.
  4. POM (Project Object Model): A parent project used to aggregate multiple sub-projects, which itself does not generate any actual build products.

In addition to these common packaging types, Maven also supports other special packaging types, such as RAR (Resource Adapter Archive) and BUNDLE (OSGi Bundle), for specific application scenarios. You can choose the appropriate packaging type to build the project according to your specific needs.

Packaged into a jar package

<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>

  <!-- 项目坐标:GAV    jar  坐标便于被别人使用,便于管理-->
  <groupId>edu.nf</groupId><!--包名-->
  <artifactId>demo</artifactId><!--项目名 模板-->
  <version>1.0-SNAPSHOT</version><!--指定版本-->
  <packaging>jar</packaging><!--封装成jar包-->

  <name>ums</name>
  <url>http://maven.apache.org</url>

 <!--配置相关的属性-->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

<!--依赖第三方配置-->
 <dependencies>
 <!--添加依赖 junit构件 方便测试-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>

<!--添加依赖 servlet-API  为什么要添加servlet依赖:因为运行servlet需要用到jar包,而这个servlet依赖可以帮助下载需要到的jar包-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
   </dependency>

</dependencies>
</project>

Packaged into a war package

<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>

  <!-- 项目坐标:GAV    jar  坐标便于被别人使用,便于管理-->
  <groupId>edu.nf</groupId>
  <artifactId>demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>ums</name>
  <url>http://maven.apache.org</url>

 <!--配置相关的属性-->
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

<!--依赖第三方配置-->
 <dependencies>
 <!--添加依赖 junit构件 方便测试-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>

<!--添加依赖 servlet-API  为什么要添加servlet依赖:因为运行servlet需要用到jar包,而这个servlet依赖可以帮助下载需要到的jar包-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
   </dependency>

</dependencies>
</project>

attach video

Create a Maven project purely manually

3) Description

  • project coordinates
    • groupId: package name (reverse order of company or component domain name). project name
    • artifactId: project name - module name
    • version: specify the version
  • dependencies: dependency list, specifying the third-party tools (components/jar) that this project depends on
  • archetype plugin - automatically create a directory skeleton that complies with maven regulations
    • Method 1: mvn archetype:generate
    • The first build needs to connect to the network, download the relevant plug-ins from the official website
    • You can configure domestic mirroring to solve

JUnit

1. Concept

JUnit is a testing framework for the Java programming language that helps developers write automated unit and integration tests.

2. Steps to use

First: Add JUnit dependencies to the project (configure Junit dependencies in pom.xml)

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

Second: create a test class which should contain some test methods. Use the @Test annotation in the test class to identify the test method. In java, assertEquals means to compare whether the actual value is equal to the expected value

package com.maven.demo ;
import org.junit.* ;
import org.junit.Assert.* ;

public class UserTest {
	@Test
	public void testHello() {
    	// 断言验证测试结果是否符合预期
			Assert.assertEquals("hello",new User().sayHello()) ;
	}
}

Third: Write the test code in the test method. Assertions can be used to verify that test results are as expected. For example, use the assertEquals() method to compare actual and expected values.

Fourth: Run the test. Tests can be run using IDE tools, or build tools such as Maven or Gradle. After the test runs, relevant information such as test results and test coverage will be displayed.


Common APIs

1) @Test annotation

Used to identify a test method. For example:

@Test
public void testAddition() {
    int result = Calculator.add(2, 3);
    assertEquals(5, result);
}

2) assertEquals () method

Used to compare whether the actual value is equal to the expected value. For example:

int result = Calculator.add(2, 3);
assertEquals(5, result);

3) assertTrue() and assertFalse() methods

Used to verify whether a condition is true or false. For example:

boolean flag = Calculator.isPositive(5);
assertTrue(flag);

flag = Calculator.isPositive(-5);
assertFalse(flag);

4) assertNotNull() and assertNull() methods

Used to verify if an object is empty or not. For example:

String str = Calculator.getString();
assertNotNull(str);

str = null;
assertNull(str);

5) @Before and @After annotations

Used to identify methods to execute before and after a test method. For example:

@Before
public void setUp() {
    Stystem.out.println("我先测试的")
}

@After
public void tearDown() {
       Stystem.out.println("我最后测试")
}

public void tes(){
    System.out.println("测试中...")
}

6) @BeforeClass and @AfterClass annotations

Used to identify methods to be executed before and after all test methods. For example:

@BeforeClass
public static void setUpClass() {
    // 初始化测试数据
}

@AfterClass
public static void tearDownClass() {
    // 清理测试数据
}
@BeforeClass
public static void setUpClass() {
    // 初始化测试数据
}

@AfterClass
public static void tearDownClass() {
    // 清理测试数据
}

7) @Ignore annotation

Used to identify a test method that needs to be ignored. For example:

@Ignore("这个测试方法还没准备好")
@Test
public void testSomething() {
    // 测试代码
}

Lombok

dependent package

<!--添加依赖Lombok 自动生成JavaBean 所需的 tos,hashCode等-->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.26</version>
  <scope>provided</scope>
</dependency>

Lombok is a Java library that simplifies the developer's programming tasks by automating the boilerplate code of Java classes through annotations.

1. @Data annotation

Using the @Data annotation can automatically generate the getter, setter, toString, equals and hashCode methods required by JavaBean.

@Data
public class Person {
    private String name;
    private int age;
}

2. @AllArgsConstructor and @NoArgsConstructor annotations

Using @AllArgsConstructor and @NoArgsConstructor annotations can automatically generate parameterized constructors and parameterless constructors.

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    private String name;
    private int age;
}

3. @Builder annotation

Using the @Builder annotation can automatically generate the code required for the Builder pattern, making it easy to create complex objects.

@Data
@Builder
public class Person {
    private String name;
    private int age;
    private String address;
}

Person person = Person.builder()
        .name("张三")
        .age(20)
        .address("北京市")
        .build();

Summarize

Well, after a lot of hard work, I finally wrote it. I hope you can support me a lot. If there is anything wrong, please point it out!

Guess you like

Origin blog.csdn.net/m0_67840539/article/details/131243933