Maven tutorial three: maven life cycle and plug-in mechanism Detailed

Foreword

Today this be a learning Maven ending of the article, which is not limited to the contents mentioned in the title, later also added an example profile configuration project environment and the company is now using archetype templates based on actual use.

Behind will summarize a large mind maps to record generalize their learning.

Maven lifecycle introduction

First to review a few commands:

  • mvn clean package:打包
  • mvn clean install: install local
  • mven clean deploy: deploy to a remote repository

mvn have three completely independent life cycle, clean, default site and
each will contain multiple life cycle phase, goal of each phase in turn, various plug-ins to complete.

phase will be appreciated that the task unit, the life cycle of a general task, the task is the total phase out of a sub-sub-tasks, but these subtasks are normalized, it can be simultaneously included in the plurality of life cycle, life cycle comprising a plurality a phase, the order of execution of phsse, can bind a plurality of Goal phase, at least one.

mission goal is the smallest unit that can be bound to any phase, one or more phase has a goal, a goal is executed in order, when a phase is performed in the phase bound to the goal will be tied by given time are sequentially performed.

Maven life cycle and phase

phase clean life cycle consists of the following:

  • pre-clean
  • clean
  • post-clean

phase default life cycle consists of the following:

  • validate: check this project some configuration information is correct
  • initialize: initialize build status, such as setting a few properties or create some directories
  • generate-sources: automatically generates source code, compiled together and contained in the project code
  • process-sources: source code, such as do some alternative placeholder
  • generate-resources: generate the resource files, it is dry when I said those things, mainly to deal with a variety of xml, properties that the configuration file, do replace some configuration files inside placeholders
  • process-resources: the resource files copied to the target directory, the back pack to facilitate
  • compile: compile the project source code
  • process-classes: compiled code files processed, such as to enhance the java class bytecode
  • generate-test-sources: generating automated test code
  • process-test-sources: test code processing, such as filtering placeholders
  • generate-test-resources: generate the resource files used for testing
  • process-test-resources: a resource file copy test to the target directory
  • test-compile: compile the test code
  • process-test-classes: test processing the compiled code, such as byte code enhancement
  • test: a test unit testing frameworks run
  • prepare-package: preparatory work prior to packaging, such as handling the version number of the package
  • package: packaged code, such as jar package
  • pre-integration-test: preparatory work before integration testing, such as the establishment of environmental need,
  • integration-test: The package is deployed to an environment to run integration tests
  • post-integration-test: perform some operations after integration testing, such as cleaning up the test environment
  • verify: to package some checks to ensure the quality of clearance
  • install: install the package into the local repository, so developers can use in their own local
  • deploy: The package uploaded to a remote repository, other developers in such companies can also use the

site lifecycle phase

  • pre-site
  • site
  • post-site
  • site-deploy

### default phase and plugin

When we run directly mvn clean package, each phase is to be completed by the plug-goal, what is the relationship between phase and plugin is bound up?

In fact, the default maven to bind a number of plugin goal to phase, such as:

Similar resources: resources in this format, that is the resources of this plugin resources goal (resources function is responsible for processing the resource file)

maven command line with the life cycle

For example, we executed mvn clean package lifecycle is what?

clean phase clean is clean referring to the life cycle of the
package refers to the package phase default life cycle

At this point it will all perform clean lifecycle phase before the clean and clean phase of the phase (pre-clean, clean)
all phase and colleagues to perform default package phase in the life cycle before the package phase of

clean default binding is clean: clean, clean plugin's clean goal, so it will go to perform clean goal clean plug-ins.

FIG specific implementation process is as follows:

04_maven life cycle principle .png

maven plugin used in combat

Demand: item has mybatis automatic code generation, it is desirable to perform certain maven command can be automatically generated code corresponding to the specified table settings.

Add plugin mybatis.generator, and then configure generate goal

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>mybatis-generator-config.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.2</version>
                    </dependency>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql-connector.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>com.wangmeng.game</groupId>
                        <artifactId>game-wangmeng-common</artifactId>
                        <version>1.0-SNAPSHOT</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Look at sql information mybatis-generator-config.xml configuration:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
    <context id="context" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.plugin.PaginationPlugin">
        </plugin>

        <commentGenerator>
            <property name="suppressAllComments" value="false"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <!-- 数据库的相关配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://ip:3306/db?useUnicode=true" userId="root"
                        password=""/>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 实体类生成的位置 -->
        <javaModelGenerator targetPackage="com.wangmeng.game.league.entity"
                            targetProject="game-wangmeng-entity/src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- *Mapper.xml 文件的位置 -->
        <sqlMapGenerator targetPackage="mapper" targetProject="game-wangmeng-dao/src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- Mapper 接口文件的位置 -->
        <javaClientGenerator targetPackage="com.wangmeng.game.league.dao" 
                             targetProject="game-wangmeng-dao/src/main/java"
                             type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!-- 相关表的配置 -->
        <table tableName="t_table" domainObjectName="TableEntity" enableCountByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               enableUpdateByExample="true">
            <generatedKey column="id" sqlStatement="MySql" identity="true"/>
        </table>

    </context>
</generatorConfiguration>

We can see that we configured in the idea maven plugin, and then click Go to:

2E54BFFE-005B-4464-9A38-2CFD74176C8D.png

plugin:mybatis-generator
goal:generate

Using the profile to distinguish between different environments

    <profiles>
    <profile>
        <!--local env-->
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
        </properties>
        <activation>                  
           <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <!--test env-->
        <id>test</id>
        <properties>
            <profiles.active>test</profiles.active>
        </properties>
    </profile>
    <profile>
        <!--UT env-->
        <id>ut</id>
        <properties>
            <profiles.active>ut</profiles.active>
        </properties>
    </profile>
</profiles>

Here is the default dev environment, through <activation>configuration.

The relationship between maven and git version control

A map stream:

The relationship between 05_maven and git .png

higher-order maven: archetype template

artchetype actually a maven project templates

We now take a thing with, for example, when we create a new project, can not re-new a new maven project, the company in fact has long provided a scaffolding project generated for us, only need to perform some maven command good to generate a new project, project structure here is a good definition.

One advantage of this to help us create a project, and second, to facilitate the implementation and management of the company norms.

Here take a direct template artchetype a company to demonstrate (key information has mosaic)

1, you can choose to generate a maven archetype quickstart maven archetype webapp project or

211.png

Then mvn deploy package can be uploaded to the archetype jar maven PW

2, using a custom maven archetype

The company which is directly executable file that encapsulates a bat, implementation is

cmd to CreateProject-latest.bat directory, execute the command CreateProject-latest.bat project name package name , such as CreateProject-latest.bat shop-report report can be generated using the latest version of scaffolding engineering xx-spring-cloud-api- shop- report, package name com.xx.report. The project to copy your favorite addresses, and then import into eclipse or idea can be. Please add gitignore manually. Please modify the parent project is the latest version of the release version.

CreateProject-latest.bat

echo on & color 0A
setlocal enabledelayedexpansion

if "%1"=="" goto BLANK
if "%2"=="" goto BLANK

set ProjectName=%1
set packageName=%2
set archetypeVersion=LATEST

mvn dependency:copy -Dartifact=com.xx:xx-archetype-springcloud-archetype:%archetypeVersion% -Dmdep.stripVerison=true & echo Y | mvn archetype:generate -DarchetypeCatalog=local -DarchetypeGroupId=com.tuhu -DarchetypeArtifactId=xx-archetype-springcloud-archetype -DarchetypeVersion=%archetypeVersion% -DgroupId=com.tuhu -DartifactId=%ProjectName% -Dversion=0.0.1-SNAPSHOT -Dpackage=com.xx.%packageName% & rd/s/q ${project.basedir} & rd/s/q %ProjectName%\.idea & ren %ProjectName% xx-spring-cloud-api-%ProjectName%

Interested partner may be concerned about the small number of individual public: One branch count romantic flowers

22.jpg

Guess you like

Origin www.cnblogs.com/wang-meng/p/11902415.html