Maven Junior

Maven Junior

Introduction to Maven

Traditional project management status analysis

  • Jar packages are not unified and jar packages are incompatible
  • The engineering upgrade and maintenance process is cumbersome

img

What is Maven

  • The essence of Maven is a project management tool that abstracts the project development and management process into a project object model (POM)
  • POM: Project Object Model

img

The role of Maven

  • Project construction: Provides a standard, cross-platform automated project construction method
  • Dependency management: Conveniently and quickly manage the resources that projects depend on to avoid version conflicts between resources.
  • Unified development structure: Provide a standard and unified project structure

img

Download and install

Official website: https://maven.apache.org/

Maven environment variable configuration

  • Depends on Java, needs to configure Java_HOME
  • To set up MAVEN's own operating environment, you need to configure MAVEN_HOME

img

Description configuration successful

Maven basic concepts

storehouse

  • Warehouse: used to store resources, including various jar packages

img

coordinate

  • what are coordinates

    • Coordinates in Maven are used to describe the location of resources in the warehouse
    • https://mvnrepository.com/
  • The main components of Maven coordinates

    • groupId: defines the name of the organization to which the current Maven project belongs
    • artifactId: defines the current Maven project name
    • version: defines the current project version number
    • packaging: defines how the project is packaged
  • The role of Maven coordinates

    • Use a unique identifier to uniquely locate the location of the resource. Through this identifier, the identification and downloading of the resource can be completed by the machine.

Warehouse configuration

settings.xml in the conf folder

<!--  本地仓库位置-->
<localRepository>D:\Softs\Maven\repository</localRepository>

<!--  下载源-->
<mirrors>
   <mirror>
    <id>aliyunmaven</id>
    <mirrorOf>*</mirrorOf>
    <name>阿里云公共仓库</name>
    <url>https://maven.aliyun.com/repository/public</url>
   </mirror>
  </mirrors>

The first Maven project (handmade)

Maven project directory structure

img

Build command

  • The Maven build command starts with mvn, followed by function parameters. Multiple commands can be executed at one time, separated by spaces.
mvn compile		#编译
mvn clean			#清理
mvn test			#测试
mvn package		#打包
mvn install		#安装到本地仓库

Plug-in creation project

  • Create project
mvn archetype:generate
  	-DgroupId={
    
    project-packaging}
  	-DartifactId={
    
    project-name}
  	-DarchetypeArtifactId=maven-archetype-quickstart
  	-DinteractiveMode=false

The first Maven project (generated by IDEA)

img

img

img

Dependency management

  • Depends on the jars required to run the current project. A project can set multiple dependencies.

img

transitive dependency

If you need to use project 3 in project 2, you only need to configure project 3 in project 2.

img

After introduction, it was found that project 2 has dependencies in project 3.

img

  • Dependencies are transitive

    • Direct dependencies: dependencies established through dependency configuration in the current project
    • Indirect dependency: If the resource being resourced depends on other resources, the current project indirectly depends on other resources.

img

Dependency transfer conflict problem

  • Path priority: When the same resource appears in dependencies, the deeper the hierarchy, the lower the priority, and the higher the priority.
  • Declaration priority: When resources are dependent on the same level, the one with the higher configuration order overrides the one with the lower configuration order.
  • Special priority: When different versions of the same resource are configured at the same level, the one configured later overwrites the one configured first.

img

optional dependencies

  • Optional dependencies refer to hiding the currently dependent resources from the outside - opaque
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
            <optional>true</optional>
        </dependency>

img

exclude dependencies

  • Excluding dependencies means actively disconnecting dependent resources. Excluded resources do not need to specify versions - no need
  <dependency>
      <groupId>com.xc</groupId>
      <artifactId>project03</artifactId>
      <version>1.0-SNAPSHOT</version>
      <exclusions>
          <exclusion>
              <groupId>log4j</groupId>
              <artifactId>log4j</artifactId>
          </exclusion>
      </exclusions>
  </dependency>

img

dependency scope

  • Dependent jars can be used anywhere by default, and their scope can be set through the scope tag.

  • Scope

    • Valid within the main program scope (within the scope of the main folder)
    • The test program scope is valid (within the test folder scope)
    • Whether to participate in packaging (within the scope specified by package)

img

Dependence on scope transitivity

  • When resources with dependent scopes are passed, the scope will be affected

img

Lifecycle and plug-ins

Build life cycle

  • The Maven build life cycle describes how many events a build process goes through.

img

  • Maven divides the project construction life cycle into three sets

    • clean: cleaning work
      • pre-clean
      • clean
      • post-clean
    • default: core work, such as compilation, testing, packaging, deployment, etc.

img

    • site: generate reports, publish sites, etc.
      • pre-site
      • site
      • post-site
      • site-deploy

plug-in

  • The plug-in is bound to the stage in the life cycle, and the corresponding plug-in function is executed when the corresponding life cycle is executed.
  • By default, maven has preset functions bound to each life cycle.
  • Other functions can be customized through plug-ins
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <phase>generate-test-resources</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
              <goals>
                        <goal>jar</goal>
                    </goals>
                    <phase>generate-test-resources</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Guess you like

Origin blog.csdn.net/xcxcxcxx1/article/details/130920950