Created
January 17, 2012 14:02
-
-
Save xseignard/1626717 to your computer and use it in GitHub Desktop.
In memory mysql mojo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<artifactId>mysql-mojo</artifactId> | |
<packaging>maven-plugin</packaging> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>mysql-mojo Maven Mojo</name> | |
<url>http://www.pod-programming.com</url> | |
<parent> | |
<groupId>com.podprogramming</groupId> | |
<artifactId>parent</artifactId> | |
<version>0.0.1</version> | |
</parent> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.maven</groupId> | |
<artifactId>maven-plugin-api</artifactId> | |
<version>2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>mysql</groupId> | |
<artifactId>mysql-connector-mxj</artifactId> | |
<version>5.0.12</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright (c) 2012 Pod Programming (http://www.pod-programming.com). | |
* All rights reserved. | |
*******************************************************************************/ | |
package com.podprogramming.mysql.mojo; | |
import java.io.File; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
import org.apache.maven.plugin.AbstractMojo; | |
import org.apache.maven.plugin.MojoExecutionException; | |
import org.apache.maven.plugin.MojoFailureException; | |
import com.mysql.management.driverlaunched.ServerLauncherSocketFactory; | |
/** | |
* Starts an in-memory mysql db according to the pom.xml conf. | |
* | |
* @author <a href="mailto:[email protected]">Xavier Seignard</a> | |
* | |
* @goal start | |
*/ | |
public class StartMysqlMojo extends AbstractMojo implements IDbFolder { | |
/** | |
* Jdbc driver used by this mojo. | |
*/ | |
private static final String DRIVER = "com.mysql.jdbc.Driver"; | |
/** | |
* The mysql port. | |
* | |
* @parameter default-value=3336 | |
*/ | |
private Integer port; | |
/** | |
* The mysql user. | |
* | |
* @parameter default-value="root" | |
*/ | |
private String username; | |
/** | |
* The mysql user password. | |
* | |
* @parameter default-value="" | |
*/ | |
private String password; | |
/** | |
* The mysql baseName. | |
* | |
* @parameter | |
* @required | |
*/ | |
private String basename; | |
/** | |
* Location of the in memory db parent folder. | |
* | |
* @parameter expression="${project.build.directory}" | |
* @required | |
*/ | |
private File outputDirectory; | |
/* | |
* (non-Javadoc) | |
* | |
* @see org.apache.maven.plugin.Mojo#execute() | |
*/ | |
public void execute() throws MojoExecutionException, MojoFailureException { | |
// Checks wether the target folder exists | |
File targetDir = this.outputDirectory; | |
// Create it if not | |
if (!targetDir.exists()) { | |
targetDir.mkdirs(); | |
} | |
// Create the db folder in the target folder | |
File mysqlDb = new File(targetDir, MYSQL); | |
// Close a previous db if there is one | |
ServerLauncherSocketFactory.shutdown(mysqlDb, null); | |
// Create the connection url | |
String url = "jdbc:mysql:mxj://localhost:" + this.port + "/" + this.basename // | |
+ "?" + "server.basedir=" + mysqlDb // | |
+ "&" + "createDatabaseIfNotExist=true"// | |
+ "&" + "server.initialize-user=true" // | |
; | |
// Logs the jdbs connection url | |
getLog().info("Using jdbc connection url '" + url + "'"); | |
try { | |
// Initialize the jdbc driver | |
Class.forName(DRIVER); | |
getLog().info("Using jdbc driver '" + DRIVER + "'"); | |
// Connect to the mysql in memory db. | |
// Note that mxj will start the db when creating the connection | |
// DriverManager.getConnection(url, this.username, this.password); | |
DriverManager.getConnection(url, this.username, this.password); | |
getLog().info("Starting mysql in memory database"); | |
} | |
catch (ClassNotFoundException e) { | |
throw new MojoExecutionException("Driver not found", e); | |
} | |
catch (SQLException e) { | |
throw new MojoExecutionException("Connection failed to the database", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment