Quick Start with maven

Posted by sonnieboy on Tue, 23 Jul 2019 14:27:36 +0200

Maven Basic Knowledge

Official website: Portal

Maven project structure

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

POM file

  • The POM file represents the Project Object Model, which is the basic component of working with Maven and is located in the project root directory.
  • POM files 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>Distributed Advertising System</name>
    <description>Be based on Spring Cloud Alibaba Implementation of Distributed Advertising System</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>

    <!--Define remote maven Warehouse-->
    <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

Maven Dependency

Maven's idea is that conventions are greater than configurations. In default dependencies, scope is compile.

Scope type

  • compile (to be packaged into the current project)

    Represents that the dependent package participates in the compilation of the current project, including subsequent tests, and the run cycle will be involved, which is a strong dependency.

  • test

    Represents that the dependent jar only participates in test-related processing, encapsulates the compilation and execution of test code. (e.g. junit)

  • runtime

    Represents that the dependent jar does not need to be involved in the compilation of the project, but the later test and run cycles need to be involved.

  • provided

    Other Container s will provide support for this dependency. In theory, this dependency can participate in compilation, test run and other cycles, which is equivalent to compile, but exclude command is made in the packaging phase.

  • system

    In terms of the participating environment, it is the same as provided, but the dependent items are not retrieved from the maven repository, but from the local file system. It must be used with the system Path attribute.

  • import

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

Dependency Transfer Characteristics

Official explanation: Portal

  • Dependency mediation

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

    Depending on the depth of dependency, select the package version closest to the path of dependency. If the depth of dependency is the same, select the former. Note that if two dependency versions are at the same depth in the dependency tree, the first declaration wins.

  • Dependency management

    Dependency management (project authors can directly specify dependency versions)

  • Dependency scope as described in the previous section
  • Excluded dependencies exclude dependencies in dependency packages
  • Optional dependencies

Common commands

Topics: Java Maven Spring Apache