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/>元素进行配置。
这里仅列举几个常用的插件,每个插件的如何进行个性化配置在官网都有详细的介绍。
<plugins> <plugin> <!– 编译插件 –> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-compiler-plugin </artifactId> <version>2.3.2 </version> <configuration> <source>1.5 </source> <target>1.5 </target> </configuration> </plugin> <plugin> <!– 发布插件 –> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-deploy-plugin </artifactId> <version>2.5 </version> </plugin> <plugin> <!– 打包插件 –> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-jar-plugin </artifactId> <version>2.3.1 </version> </plugin> <plugin> <!– 安装插件 –> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-install-plugin </artifactId> <version>2.3.1 </version> </plugin> <plugin> <!– 单元测试插件 –> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-surefire-plugin </artifactId> <version>2.7.2 </version> <configuration> <skip>true </skip> </configuration> </plugin> <plugin> <!– 源码插件 –> <groupId>org.apache.maven.plugins </groupId> <artifactId>maven-source-plugin </artifactId> <version>2.1 </version> <!– 发布时自动将源码同时发布的配置 –> <executions> <execution> <id>attach-sources </id> <goals> <goal>jar </goal> </goals> </execution> </executions> </plugin> </plugins>
除了这些核心插件之外,还有很多优秀的第三方插件,可以帮助我们快捷、方便的构架项目。当使用到某些功能或者特性的时候多加搜索,往往得到让你惊喜的效果。例如,项目中使用了Mybatis,就有一款神奇的maven插件,运行一个命令,就可以根据数据库的表,自动生成Mybatis的mapper配置文件以及DAO层接口模板。
在pom.xml中添加plugin:
<plugin> <groupId>org.mybatis.generator </groupId> <artifactId>mybatis-generator-maven-plugin </artifactId> <version>1.3.2 </version> <configuration> <configurationFile>src/main/resources/mybatis-generator/generatorConfig.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> </dependencies> </plugin>
定义generatorConfig.xml配置文件:
<?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> <classPathEntry location="/Users/winner/mysql/mysql-connector-java-5.1.36.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <!-- 去掉自动生成的注解 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> < jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3344/db?characterEncoding=utf8" userId="id" password="password"> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成映射类--> <javaModelGenerator targetPackage="com.clf.model" targetProject="/Users/winner/Documents/workspace/project/src/main/java/"> <!-- enableSubPackages:是否让schema作为包的后缀 --> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成xml文件--> <sqlMapGenerator targetPackage="com.clf.mapper" targetProject="/Users/winner/Documents/workspace/project/src/main/resources/"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </sqlMapGenerator> <!-- 生成mapperinterface--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.clf.mapper" targetProject="/Users/winner/Documents/workspace/project/src/main/java/"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaClientGenerator> <table tableName="table_name" domainObjectName="object_name" enableCountByExample= "false" enableUpdateByExample= "false" enableDeleteByExample= "false" enableSelectByExample= "false" selectByExampleQueryId= "false"/> </context> </generatorConfiguration>
然后定位到pom.xml所在的路径下面,运行:
mvn mybatis-generator:generate
所有的文件就会自动生成,怎一个爽字了得。
Maven之(三)Maven插件
最新推荐文章于 2024-08-02 11:24:19 发布