今天这个算是学习Maven的一个收尾文章,里面内容不局限于标题中提到的,后面还加上了公司实际使用的根据profile配置项目环境以及公司如今用的archetype 模板等例子。java
后面还会总结一个大的思惟导图记录下本身学习的归纳。mysql
先来复习几个命令:git
mvn有三套彻底独立的生命周期,clean、default和site
每套生命周期都会包含多个phase,每一个phase又是由各类插件的goal来完成的。web
phase能够理解为任务单元,生命周期是总任务,phase就是总任务分出来的一个个子任务,可是这些子任务是被规格化的,它能够同时被多个生命周期所包含,一个生命周期包含多个phase,phsse的执行时顺序的,一个phase能够绑定多个goal,至少一个。spring
goal是执行任务最小的单元,它能够绑定到任意的phase中,一个phase有一个或多个goal,goal也是按顺序执行的,一个phase被执行时,绑定到phase里的goal会按绑定的时间被顺序执行。sql
clean的生命周期包含的phase以下:shell
default的生命周期包含的phase以下:数据库
site生命周期的phaseapi
### 默认的phase和pluginmybatis
咱们直接运行mvn clean package的时候,每一个phase都是由插件goal来完成的,phase和plugin绑定的关系是什么了?
实际上,默认maven就绑定了一些plugin goal到phase上,好比:
相似于resources:resources这种格式,说的就是resources这个plugin的resources goal(resources功能,负责处理资源文件)
好比咱们执行mvn clean package生命周期是什么样的?
clean是指的clean生命周期中的clean phase
package是指default生命周期中的package phase
此时就会执行clean生命周期中在clean phase以前的全部phase和clean phase(pre-clean、clean)
同事执行default生命周期中在package phase以前的全部phase和package phase
clean默认绑定的是clean:clean, clean plugin的clean goal,因此就会去执行clean插件的clean goal。
具体执行流程以下图:
需求:项目中有mybatis 自动生成代码,但愿执行某些maven命令能够自动根据指定的表设置 生成对应代码。
添加mybatis.generator的plugin,而后配置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>
再看看mybatis-generator-config.xml配置的sql信息:
<?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>
咱们能够在idea maven中看到咱们配置好的plugin了,而后点击执行便可:
plugin:mybatis-generator
goal:generate
<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>
这里默认使用的是dev环境,经过<activation>
配置的。
一图流:
artchetype其实就是个maven项目模板
拿一个咱们公司如今用的东西举例,当咱们建立一个全新的项目时,不可能再从新new一个全新的maven项目了,公司其实早就为咱们提供了项目生成的脚手架,只须要执行一段maven命令便可生成好一个全新的项目,项目结构在这里都是给定义好了的。
这样的好处一是方便咱们建立项目,二是方便公司规范的执行和管理。
这里直接拿公司的一个artchetype的一个模板来演示(关键信息已经马赛克)
1,本身能够选择生成一个maven archetype quickstart或者maven archetype webapp项目
而后用mvn deploy 便可把archetype jar包上传到maven私服
2,使用自定义的maven archetype
公司里面是直接封装了一个bat可执行文件,执行方式为
cmd到CreateProject-latest.bat目录,执行命令CreateProject-latest.bat 项目名 包名,如CreateProject-latest.bat shop-report report 便可使用最新版本的脚手架生成工程 xx-spring-cloud-api-shop-report,包名为com.xx.report。把项目copy到你的喜欢的地址,再导入到eclipse或idea中便可。请手动添加gitignore。请修改父项目版本为最新的release版本。
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%
感兴趣的小伙伴可关注我的公众号:壹枝花算不算浪漫