mybatis-generator是常用的mybatis映射文件生成工具,由于项目使用Maven集成,为提高开发效率,尝试将此功能封装成Maven插件,不多啰嗦,步骤如下:
1、在eclipse中建立一个普通的Maven工程(插件工程不太靠谱),然后修改pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.biristone</groupId>
<artifactId>mybatisgen</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>mybatisgen</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.0</version>
<executions>
</executions>
<configuration>
<!-- Needed for Java 5 annotation based configuration, for some reason. -->
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>snapshots</id>
<url>http://xxxxxxx/nexus/content/groups/public/mybatisgen</url>
</snapshotRepository>
</distributionManagement>
</project>
2、编写插件Java代码
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
/**
* @author biristone
*
* @goal gen
*
* @phase process-sources
*/
public class Creater extends AbstractMojo {
/**
* @parameter property="jarpath" 插件参数,mysql的jar包路径
*/
private String jarpath;
/**
* @parameter property="xmlpath" 插件参数,mybatis-generator配置文件的路径
*/
private String xmlpath;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info(jarpath);
getLog().info(xmlpath);
final String cmd1 = "java -jar ";
final String cmd2 = " -configfile ";
final String cmd3 = " -overwrite";
StringBuffer sb = new StringBuffer();
sb.append(cmd1).append(jarpath).append(cmd2).append(xmlpath).append(cmd3);
Runtime run = Runtime.getRuntime();
try {
Process p = run.exec(sb.toString());
BufferedInputStream in = new BufferedInputStream(p.getInputStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;
while ((lineStr = inBr.readLine()) != null)
System.out.println(lineStr);
if (p.waitFor() != 0) {
if (p.exitValue() == 1)
System.err.println("Command execute fail.");
}
inBr.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3、安装插件
本地:maven install 即可
仓库:pakage deploy
4、使用插件
新建一个普通Maven工程,在pom.xml中引入插件
<build>
<plugins>
<plugin>
<groupId>com.biristone</groupId>
<artifactId>mybatisgen</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>gen</goal>
</goals>
<configuration>
<jarpath>D:/mybatis-generator-core-1.3.2/lib/mybatis-generator-core-1.3.2.jar</jarpath>
<xmlpath>D:/works/mybatisgen/src/main/resources/generator.xml</xmlpath>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
OK