Getting started using maven

Maven basics

Official website: Portal

Maven project structure

$ MavenProject
|-- pom.xml
|-- src
|   |-- main
|   |   `-- java
|   |   `-- resources
|   `-- test
|   |   `-- java
|   |   `-- resources
`-- README.md

POM file

  • POM file on behalf of the project object model (Project Object Model) it is to use the basic components Maven work, the project is located in the root directory.
  • POM file support inheritance
<?xml version="1.0" encoding="UTF-8"?>
<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>
    <packaging>pom</packaging>
    <modules>
        <module>mscx-ad-discovery</module>
        <module>mscx-ad-zuul</module>
        <module>mscx-ad-gateway</module>
        <module>mscx-ad-discovery-nacos</module>
        <module>mscx-ad-common</module>
        <module>mscx-ad-db</module>
        <module>mscx-ad-sponsor</module>
        <module>mscx-ad-search</module>
        <module>mscx-ad-feign-sdk</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.sxzhongf</groupId>
    <artifactId>mscx-ad</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>分布式广告系统</name>
    <description>基于Spring Cloud Alibaba 实现的分布式广告系统</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--定义远程maven仓库-->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>alibaba</id>
            <name>ali Milestones</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Maven coordinates

UTOOLS1563873610326.png

Maven Dependency

Maven idea is greater than the agreed configuration, default dependence, scope is compile.

Scope type

  • compile (it will be packed to the current project)

    That was dependent on the package involved in compiling the current project, including subsequent test run period will participate, it is a strong dependency.

  • test

    It represents dependent jar test only involved in the processing associated compiled, wrapping test code is executed. (Eg junit)

  • runtime

    That was dependent on the jar is not involved in compiling the project, but later testing and operation cycles need to be involved.

  • provided

    When packing need not be included in, the other dependent on the Container will provide support, in theory, can participate in the dependency cycle compile, test runs, etc., equivalent to compile, but do exclude command in packing stage.

  • system

    From participation in environmental point of view, and provided the same, but will not be available from the maven dependencies warehouse, but from a local file system, then we need with the systemPathproperty use

  • import

    This scope is only supported on a dependency of type pom in the <dependencyManagement> section.

Transfer characteristic-dependent

The official explanation: Portal

  • The Mediation Dependency (recently depend principle)

    "nearest definition" means that the version used will be the closest one to your project in the tree of dependencies. For example, if dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0.

    The depth dependent, the choice depends on the route to the nearest package version, dependency if the same depth, then select a front.Note that if two dependency versions are at the same depth in the dependency tree, the first declaration wins.

  • Dependency management

    Dependency Management (project author can be specified directly dependent on the version)

  • Dependency scope described in the previous section

  • Excluded dependencies exclude dependent package dependencies

  • Dependencies optional (disposed corresponding to the transfer is not allowed to go dependent)

UTOOLS1563869243423.png

Common Commands

UTOOLS1563873521089.png

Guess you like

Origin www.cnblogs.com/zhangpan1244/p/11233939.html