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:


   
   
  1. <plugin>
  2. <groupId>org.mybatis.generator </groupId>
  3. <artifactId>mybatis-generator-maven-plugin </artifactId>
  4. <version>1.3.2 </version>
  5. <configuration>
  6. <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml </configurationFile>
  7. <verbose>true </verbose>
  8. <overwrite>true </overwrite>
  9. </configuration>
  10. <executions>
  11. <execution>
  12. <id>Generate MyBatis Artifacts </id>
  13. <goals>
  14. <goal>generate </goal>
  15. </goals>
  16. </execution>
  17. </executions>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.mybatis.generator </groupId>
  21. <artifactId>mybatis-generator-core </artifactId>
  22. <version>1.3.2 </version>
  23. </dependency>
  24. </dependencies>
  25. </plugin>


定义generatorConfig.xml配置文件:


   
   
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  3. <generatorConfiguration>
  4. <classPathEntry location="/Users/winner/mysql/mysql-connector-java-5.1.36.jar"/>
  5. <context id="DB2Tables" targetRuntime="MyBatis3">
  6. <!-- 去掉自动生成的注解 -->
  7. <commentGenerator>
  8. <property name="suppressAllComments" value="true"/>
  9. </commentGenerator>
  10. < jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3344/db?characterEncoding=utf8" userId="id" password="password">
  11. </jdbcConnection>
  12. <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
  13. <javaTypeResolver>
  14. <property name="forceBigDecimals" value="false"/>
  15. </javaTypeResolver>
  16. <!-- 生成映射类-->
  17. <javaModelGenerator targetPackage="com.clf.model" targetProject="/Users/winner/Documents/workspace/project/src/main/java/">
  18. <!-- enableSubPackages:是否让schema作为包的后缀 -->
  19. <property name="enableSubPackages" value="true"/>
  20. <property name="trimStrings" value="true"/>
  21. </javaModelGenerator>
  22. <!-- 生成xml文件-->
  23. <sqlMapGenerator targetPackage="com.clf.mapper" targetProject="/Users/winner/Documents/workspace/project/src/main/resources/">
  24. <property name="enableSubPackages" value="true"/>
  25. <property name="trimStrings" value="true"/>
  26. </sqlMapGenerator>
  27. <!-- 生成mapperinterface-->
  28. <javaClientGenerator type="XMLMAPPER" targetPackage="com.clf.mapper" targetProject="/Users/winner/Documents/workspace/project/src/main/java/">
  29. <property name="enableSubPackages" value="true"/>
  30. <property name="trimStrings" value="true"/>
  31. </javaClientGenerator>
  32. <table tableName="table_name" domainObjectName="object_name"
  33. enableCountByExample= "false" enableUpdateByExample= "false"
  34. enableDeleteByExample= "false" enableSelectByExample= "false"
  35. selectByExampleQueryId= "false"/>
  36. </context>
  37. </generatorConfiguration>

然后定位到pom.xml所在的路径下面,运行:

mvn mybatis-generator:generate

所有的文件就会自动生成,怎一个爽字了得。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值