Maven之(三)Maven插件
maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成,像编译是通过maven-compile-plugin实现的、测试是通过maven-surefire-plugin实现的,maven也内置了很多插件,所以我们在项目进行编译、测试、打包的过程是没有感觉到。
进一步说,每个任务对应了一个插件目标(goal),每个插件会有一个或者多个目标,例如maven-compiler-plugin的compile目标用来编译位于src/main/Java/目录下的主源码,testCompile目标用来编译位于src/test/java/目录下的测试源码。
认识上述Maven插件的基本概念能帮助你理解Maven的工作机制,不过要想更高效率地使用Maven,了解一些常用的插件还是很有必要的,这可以帮助你避免一不小心重新发明轮子。多年来Maven社区积累了大量的经验,并随之形成了一个成熟的插件生态圈。Maven官方有两个插件列表,第一个列表的GroupId为org.apache.maven.plugins,这里的插件最为成熟,具体地址为:http://maven.apache.org/plugins/index.html。第二个列表的GroupId为org.codehaus.mojo,这里的插件没有那么核心,但也有不少十分有用,其地址为:http://mojo.codehaus.org/plugins.html。
下面列举了一些常用的核心插件,每个插件的如何配置,官方网站都有详细的介绍。
一个插件通常提供了一组目标,可使用以下语法来执行:
mvn [plugin-name]:[goal-name]
例如,一个Java项目使用了编译器插件,通过运行以下命令编译
mvn compiler:compile
Maven提供以下两种类型的插件:
l 构建插件
在生成过程中执行,并应在pom.xml中的<build/>元素进行配置
l 报告插件
在网站生成期间执行的,应该在pom.xml中的<reporting/>元素进行配置。
这里仅列举几个常用的插件,每个插件的如何进行个性化配置在官网都有详细的介绍。
1 <plugins> 2 <plugin> 3 <!-- 编译插件 --> 4 <groupId>org.apache.maven.plugins</groupId> 5 <artifactId>maven-compiler-plugin</artifactId> 6 <version>2.3.2</version> 7 <configuration> 8 <source>1.5</source> 9 <target>1.5</target> 10 </configuration> 11 </plugin> 12 <plugin> 13 <!-- 发布插件 --> 14 <groupId>org.apache.maven.plugins</groupId> 15 <artifactId>maven-deploy-plugin</artifactId> 16 <version>2.5</version> 17 </plugin> 18 <plugin> 19 <!-- 打包插件 --> 20 <groupId>org.apache.maven.plugins</groupId> 21 <artifactId>maven-jar-plugin</artifactId> 22 <version>2.3.1</version> 23 </plugin> 24 <plugin> 25 <!-- 安装插件 --> 26 <groupId>org.apache.maven.plugins</groupId> 27 <artifactId>maven-install-plugin</artifactId> 28 <version>2.3.1</version> 29 </plugin> 30 <plugin> 31 <!-- 单元测试插件 --> 32 <groupId>org.apache.maven.plugins</groupId> 33 <artifactId>maven-surefire-plugin</artifactId> 34 <version>2.7.2</version> 35 <configuration> 36 <skip>true</skip> 37 </configuration> 38 </plugin> 39 <plugin> 40 <!-- 源码插件 --> 41 <groupId>org.apache.maven.plugins</groupId> 42 <artifactId>maven-source-plugin</artifactId> 43 <version>2.1</version> 44 <!-- 发布时自动将源码同时发布的配置 --> 45 <executions> 46 <execution> 47 <id>attach-sources</id> 48 <goals> 49 <goal>jar</goal> 50 </goals> 51 </execution> 52 </executions> 53 </plugin> 54 </plugins>
除了这些核心插件之外,还有很多优秀的第三方插件,可以帮助我们快捷、方便的构架项目。当使用到某些功能或者特性的时候多加搜索,往往得到让你惊喜的效果。
例如,项目中使用了Mybatis,就有一款神奇的maven插件,运行一个命令,就可以根据数据库的表,自动生成Mybatis的mapper配置文件以及DAO层接口模板。
在pom.xml中添加plugin:
定义generatorConfig.xml配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatisGenerator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 3 4 <generatorConfiguration> 5 <classPathEntry location="/Users/winner/mysql/mysql-connector-java-5.1.36.jar"/> 6 7 <context id="DB2Tables" targetRuntime="MyBatis3"> 8 9 <!-- 去掉自动生成的注解 --> 10 <commentGenerator> 11 <property name="suppressAllComments" value="true"/> 12 </commentGenerator> 13 14 < jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3344/db?characterEncoding=utf8" userId="id" password="password"> 15 </jdbcConnection> 16 17 18 <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> 19 <javaTypeResolver> 20 <property name="forceBigDecimals" value="false"/> 21 </javaTypeResolver> 22 23 <!-- 生成映射类--> 24 <javaModelGeneratortarget Package="com.clf.model" targetProject="/Users/winner/Documents/workspace/project/src/main/java/"> 25 <!-- enableSubPackages:是否让schema作为包的后缀 --> 26 <property name="enableSubPackages" value="true"/> 27 <property name="trimStrings" value="true"/> 28 </javaModelGenerator> 29 30 <!-- 生成xml文件--> 31 <sqlMapGeneratortarget Package="com.clf.mapper" targetProject="/Users/winner/Documents/workspace/project/src/main/resources/"> 32 <property name="enableSubPackages" value="true"/> 33 <property name="trimStrings" value="true"/> 34 </sqlMapGenerator> 35 36 <!-- 生成mapperinterface--> 37 <javaClientGenerator type="XMLMAPPER" targetPackage="com.clf.mapper" targetProject="/Users/winner/Documents/workspace/project/src/main/java/"> 38 <property name="enableSubPackages" value="true"/> 39 <property name="trimStrings" value="true"/> 40 </javaClientGenerator> 41 42 <table tableName="table_name" domainObjectName="object_name" 43 enableCountByExample="false" enableUpdateByExample="false" 44 enableDeleteByExample="false" enableSelectByExample="false" 45 selectByExampleQueryId="false"/> 46 </context> 47 </generatorConfiguration>
然后定位到pom.xml所在的路径下面,运行:
mvn mybatis-generator:generate
所有的文件就会自动生成,怎一个爽字了得。
浙公网安备 33010602011771号