Maven

Maven

Overview

Ref: https://maven.apache.org/guides/getting-started/index.html

Maven is a project management tool that provides developers with a complete project development life-cycle framework, and Maven is an open source project developed in pure Java under Apache

POM (Project Object Model) is the basic unit of the Maven system. It is stored in the root directory of the project in the form of an XML file named pom.xml.

Maven project default directory structure(${basedir} represents the current working directory):

Item Default
source code ${basedir}/src/main/java
resources ${basedir}/src/main/resources
tests ${basedir}/src/test
distributable JAR ${basedir}/target
complied byte code ${basedir}/target/classes

The Bulid Lifecycle

Ref: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

There are three built-in build lifecycles: default, clean and site. The default lifecycle handles your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle handles the creation of your project's web site.

Each lifecycle contains a series of construction phases. The phases have a sequence. When a phase of the life cycle is executed, it will be executed from the first phase of the life cycle to a specified phase in sequence.

standard life cycle:

  1. Clean

    phase: pre-clean, clean, post-clean

  2. Default

    phase: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, test-compile, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy

  3. Site

    phase: pre-site, site, post-site, site-deploy

phase explain
validate validate the project is correct and all necessary information is available
compile compile the source code of the project
test test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
package take the compiled code and package it in its distributable format, such as a JAR.
verify run any checks on results of integration tests to ensure quality criteria are met
install install the package into the local repository, for use as a dependency in other projects locally
deploy done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

The POM

Ref: https://maven.apache.org/guides/introduction/introduction-to-the-pom.html

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/test/java; and so on. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.

Some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists and such can also be specified.

The Super POM is Maven's default POM. All POMs extend the Super POM unless explicitly set, meaning the configuration specified in the Super POM is inherited by the POMs you created for your projects.

The basic information of the project is defined in the POM, which is used to describe how the project is built, declare project dependencies, and so on.

When creating a POM, we need to provide the group (groupId) information of the project, the project name (artifactId) and the version information (version), which uniquely represent a POM object and project.

Node Description
groupId This is an Id of project's group. This is generally unique amongst an organization or a project. For example, a banking group com.company.bank has all bank related projects.
artifactId This is an Id of the project. This is generally name of the project. For example, consumer-banking. Along with the groupId, the artifactId defines the artifact's location within the repository.
version This is the version of the project. Along with the groupId, It is used within an artifact's repository to separate versions from each other.

POM example:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <!-- project coordinates -->
    <groupId>cn.meyok.mavenlearn</groupId>
    <artifactId>pomlearn</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <!-- POM inheritance -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/>
    </parent>
    
    <packaging>jar</packaging>
    
    <!-- related information -->
    <name>mavenlearn</name>
    <url>http://mavenlearn.meyok.cn</url>
    <description>mavenlearn</description>
    
    <!-- local repository path -->
    <localRepository>D:/myRepository/repository</localRepository>
    <!-- remote repository path -->
    <repositories>
        <repository>
            <id>meyok.lib</id>
            <url>http://download.meyok.org/maven/lib</url>
        </repository>
    </repositories>
    
    <!-- POM aggregation -->
    <modules>
        <module>user</module>
    </modules>
    
    <!-- 配置Profile,可选 -->
    <profiles>
        <profile>
            <id>test</id>
            <activation>
                <property>
                    <name>env</name>
                    <value>test</value>
                </property>
            </activation>
            ....
        </profile>
        <profile>
            <id>normal</id>
            ....
        </profile>
        <profile>
            <id>prod</id>
            ....
        </profile>
    </profiles>
    
    <!-- 配置属性,可选 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.9</junit.version>
        ...
    </properties>
    
    <!-- 配置依赖 -->
    <!-- 依赖管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            ...
        </dependencies>
    </dependencyManagement>

    <!-- 依赖导入 -->
    <dependencies>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.7.1</version>
            <scope>compile</scope>
            <optional>false</optional>
            <!-- 排除传递的依赖 -->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.shiro</groupId>
                    <artifactId>shiro-cache</artifactId>
                </exclusion>
                ...
        	</exclusions>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        ...
	</dependencies>
    
    <!-- 配置构建配置 -->
    <build>
        <finalName>pomlearn</finalName>  
        <resources>  
            <resource>  
                <directory>src/main/java</directory>  
                <includes>  
                    <include>**/*.xml</include>  
                </includes>  
                <excludes>  
                    <exclude>**/*.txt</exclude>  
                    <exclude>**/*.doc</exclude>  
                </excludes>  
            </resource>
        </resources>
        <!-- 插件管理配置 -->
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.8</version>
                    <configuration>  
                        <source>1.8</source>  
                        <target>1.8</target>  
                        <encoding>UTF-8</encoding>   
                        <warName>WebMavenLearn</warName>  
                        ...
                    </configuration>  
                    <executions>
                        <execution>
                            <id>mavenlearn.meyok.cn</id>
                            <phase>pre-clean</phase>
                            <goals>
                                <goal>run</goal>
                                ...
                            </goals>
                             <configuration>
                                <tasks>
                                    <echo>预清理阶段</echo>
                                </tasks>
                        	</configuration>
                        </execution>
                    </executions>
                </plugin>
                ...
            </plugins>
        </pluginManagement>
        <!-- 插件配置 -->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
            </plugin>
            ...
        </plugins>
    </build>
    
</project>
posted @ 2023-09-24 17:11  MeYokYang  阅读(54)  评论(0)    收藏  举报