mirror of
https://github.com/valitydev/pg-embedded-plugin.git
synced 2024-11-06 01:05:18 +00:00
Maven plugin "PostgreSQL embended" ver.1.0
This commit is contained in:
commit
4e7fa76c97
83
.gitignore
vendored
Normal file
83
.gitignore
vendored
Normal file
@ -0,0 +1,83 @@
|
||||
# Created by .ignore support plugin (hsz.mobi)
|
||||
### Maven template
|
||||
target/
|
||||
pom.xml.tag
|
||||
pom.xml.releaseBackup
|
||||
pom.xml.versionsBackup
|
||||
pom.xml.next
|
||||
release.properties
|
||||
dependency-reduced-pom.xml
|
||||
buildNumber.properties
|
||||
.mvn/timing.properties
|
||||
### JetBrains template
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff:
|
||||
.idea/
|
||||
.idea/workspace.xml
|
||||
.idea/tasks.xml
|
||||
.idea/dictionaries
|
||||
.idea/vcs.xml
|
||||
.idea/jsLibraryMappings.xml
|
||||
|
||||
# Sensitive or high-churn files:
|
||||
.idea/dataSources.ids
|
||||
.idea/dataSources.xml
|
||||
.idea/dataSources.local.xml
|
||||
.idea/sqlDataSources.xml
|
||||
.idea/dynamic.xml
|
||||
.idea/uiDesigner.xml
|
||||
|
||||
# Gradle:
|
||||
.idea/gradle.xml
|
||||
.idea/libraries
|
||||
|
||||
# Mongo Explorer plugin:
|
||||
.idea/mongoSettings.xml
|
||||
|
||||
## File-based project format:
|
||||
*.iws
|
||||
*.ipr
|
||||
*.iml
|
||||
|
||||
## Plugin-specific files:
|
||||
|
||||
# IntelliJ
|
||||
/out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
### Java template
|
||||
*.class
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
env.list
|
||||
|
||||
|
||||
# OSX
|
||||
*.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# TestContainers
|
||||
.testcontainers-*
|
111
README.md
Normal file
111
README.md
Normal file
@ -0,0 +1,111 @@
|
||||
##pg embedded plugin
|
||||
|
||||
This is a maven plugin for for starting a embedded postgresql server.
|
||||
|
||||
You can use this example to start the server during maven initialization lifecicle.
|
||||
|
||||
|
||||
#####Example:
|
||||
|
||||
<plugin>
|
||||
<groupId>com.rbkmoney.maven.plugins</groupId>
|
||||
<artifactId>pg-embedded-plugin</artifactId>
|
||||
<version>1.0</version>
|
||||
<configuration>
|
||||
<port>port</port> <!-- default: 15432 -->
|
||||
<dbName>database_name</dbName>
|
||||
<schemas>
|
||||
<schema>schema_name</schema>
|
||||
</schemas>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>PG_server_start</id>
|
||||
<phase>validate</phase>
|
||||
<goals>
|
||||
<goal>start</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>PG_server_stop</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>stop</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
#####Example for flyway and jooq:
|
||||
|
||||
<plugin>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-maven-plugin</artifactId>
|
||||
<version>${flyway.version}</version>
|
||||
<configuration>
|
||||
<url>${local.db.url}</url>
|
||||
<user>${local.db.user}</user>
|
||||
<password>${local.db.password}</password>
|
||||
<schemas>
|
||||
<schema>${local.db.scheme}</schema>
|
||||
</schemas>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>migrate</id>
|
||||
<phase>initialize</phase>
|
||||
<goals>
|
||||
<goal>clean</goal>
|
||||
<goal>migrate</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.jdbc.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jooq</groupId>
|
||||
<artifactId>jooq-codegen-maven</artifactId>
|
||||
<version>${jooq.version}</version>
|
||||
<configuration>
|
||||
<jdbc>
|
||||
<driver>org.postgresql.Driver</driver>
|
||||
<url>${local.db.url}</url>
|
||||
<user>${local.db.user}</user>
|
||||
<password>${local.db.password}</password>
|
||||
</jdbc>
|
||||
<generator>
|
||||
<generate>
|
||||
<javaTimeTypes>true</javaTimeTypes>
|
||||
<pojos>true</pojos>
|
||||
<pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
|
||||
<pojosToString>true</pojosToString>
|
||||
</generate>
|
||||
<database>
|
||||
<name>org.jooq.util.postgres.PostgresDatabase</name>
|
||||
<includes>.*</includes>
|
||||
<excludes>schema_version|.*func|get_adjustment.*|get_cashflow.*|get_payment.*|get_payout.*|get_refund.*</excludes>
|
||||
<inputSchema>${local.db.scheme}</inputSchema>
|
||||
</database>
|
||||
<target>
|
||||
<directory>target/generated-sources/db/</directory>
|
||||
</target>
|
||||
</generator>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>gen-src</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>generate</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
|
82
pom.xml
Normal file
82
pom.xml
Normal file
@ -0,0 +1,82 @@
|
||||
<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>
|
||||
|
||||
<groupId>com.rbkmoney.maven.plugins</groupId>
|
||||
<artifactId>pg-embedded-plugin</artifactId>
|
||||
<packaging>maven-plugin</packaging>
|
||||
<version>1.0</version>
|
||||
|
||||
<name>pg-embedded-plugin Maven Mojo</name>
|
||||
<description>The plugin starts the embedded PG server</description>
|
||||
|
||||
<properties>
|
||||
<project.maintainer>Dmitrii Baikov <d.baykov@rbkmoney.com></project.maintainer>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<postgresql.jdbc.version>42.2.5</postgresql.jdbc.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven</groupId>
|
||||
<artifactId>maven-plugin-api</artifactId>
|
||||
<version>3.0.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.plugin-tools</groupId>
|
||||
<artifactId>maven-plugin-annotations</artifactId>
|
||||
<version>3.2</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven</groupId>
|
||||
<artifactId>maven-settings</artifactId>
|
||||
<version>3.0.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>4.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.opentable.components</groupId>
|
||||
<artifactId>otj-pg-embedded</artifactId>
|
||||
<version>0.12.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.6.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-plugin-plugin</artifactId>
|
||||
<version>3.6.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>default-descriptor</id>
|
||||
<phase>process-classes</phase>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,23 @@
|
||||
package com.rbkmoney.maven.plugins.pg_embedded_plugin;
|
||||
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
import org.apache.maven.plugins.annotations.Parameter;
|
||||
|
||||
public abstract class GeneralMojo extends AbstractMojo {
|
||||
|
||||
@Parameter(defaultValue = "false")
|
||||
private boolean skipGoal;
|
||||
|
||||
public void execute() throws MojoExecutionException, MojoFailureException {
|
||||
if (skipGoal) {
|
||||
getLog().debug("Goal was skipped");
|
||||
} else {
|
||||
doExecute();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void doExecute() throws MojoExecutionException, MojoFailureException;
|
||||
|
||||
}
|
@ -0,0 +1,152 @@
|
||||
package com.rbkmoney.maven.plugins.pg_embedded_plugin;
|
||||
|
||||
import com.opentable.db.postgres.embedded.EmbeddedPostgres;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
import org.apache.maven.plugins.annotations.LifecyclePhase;
|
||||
import org.apache.maven.plugins.annotations.Mojo;
|
||||
import org.apache.maven.plugins.annotations.Parameter;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Class to start the embedded server
|
||||
*
|
||||
* @author d.baykov
|
||||
*/
|
||||
@Mojo(name = "start", defaultPhase = LifecyclePhase.INITIALIZE)
|
||||
public class StartPgServerMojo extends GeneralMojo {
|
||||
|
||||
/** Directory where the project is located */
|
||||
@Parameter(defaultValue = "${project.build.directory}")
|
||||
private String projectBuildDir;
|
||||
|
||||
/** PostgreSQL version */
|
||||
@Parameter(property = "version")
|
||||
private String version;
|
||||
|
||||
/** Directory that will host the service files of postgresql */
|
||||
@Parameter(property = "dbDir")
|
||||
private String dbDir;
|
||||
|
||||
/** A port on which the instance will be raised */
|
||||
@Parameter(property = "port", defaultValue = "15432", required = true)
|
||||
private int port;
|
||||
|
||||
/** */
|
||||
@Parameter(property = "username", defaultValue = "postgres", required = true)
|
||||
private String userName;
|
||||
|
||||
/** */
|
||||
@Parameter(property = "password", defaultValue = "postgres")
|
||||
private String password;
|
||||
|
||||
/** */
|
||||
@Parameter(property = "dbName", required = true)
|
||||
private String dbName;
|
||||
|
||||
/** */
|
||||
@Parameter(property = "schemas", required = true)
|
||||
private List<String> schemas;
|
||||
|
||||
/** Instance of the PostgreSQL */
|
||||
private static EmbeddedPostgres embeddedPostgres;
|
||||
|
||||
/** Thread where the PostgreSQL server is running */
|
||||
private static Thread postgresThread;
|
||||
|
||||
@Override
|
||||
protected void doExecute() throws MojoExecutionException, MojoFailureException {
|
||||
postgresThread = new Thread(() -> {
|
||||
try {
|
||||
startPgServer();
|
||||
createDatabase();
|
||||
createSchemas();
|
||||
} catch (IOException e) {
|
||||
getLog().error("Errors occurred while starting the PG server:", e);
|
||||
} catch (SQLException e) {
|
||||
getLog().error("Errors occurred while creating objects:", e);
|
||||
}
|
||||
|
||||
}, "PG-embedded-server");
|
||||
postgresThread.start();
|
||||
try {
|
||||
postgresThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException("Embedded Postgres thread was interrupted", e);
|
||||
}
|
||||
}
|
||||
|
||||
/** Method starts PG server */
|
||||
private void startPgServer() throws IOException {
|
||||
if (embeddedPostgres == null) {
|
||||
getLog().info("The PG server is starting...");
|
||||
EmbeddedPostgres.Builder builder = EmbeddedPostgres.builder();
|
||||
String dbDir = prepareDbDir();
|
||||
getLog().info("Dir for PG files: " + dbDir);
|
||||
builder.setDataDirectory(dbDir);
|
||||
builder.setPort(port);
|
||||
//TODO: this can be implemented in the future...
|
||||
//builder.setCleanDataDirectory(true);
|
||||
//builder.setLocaleConfig();
|
||||
//builder.setServerConfig();
|
||||
//builder.setConnectConfig();
|
||||
embeddedPostgres = builder.start();
|
||||
getLog().info("The PG server was started!");
|
||||
} else {
|
||||
getLog().warn("The PG server is already running!");
|
||||
}
|
||||
}
|
||||
|
||||
/** The method creates a new database */
|
||||
private void createDatabase() throws SQLException {
|
||||
try (Connection conn = embeddedPostgres.getPostgresDatabase().getConnection()) {
|
||||
Statement statement = conn.createStatement();
|
||||
statement.execute("CREATE DATABASE " + dbName);
|
||||
statement.close();
|
||||
} catch (SQLException ex) {
|
||||
getLog().error("An error occurred while creating the database "+ dbName);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/** The method creates a new schema in the created database */
|
||||
private void createSchemas() throws SQLException {
|
||||
DataSource database = embeddedPostgres.getDatabase(userName, dbName);
|
||||
|
||||
try (Connection connection = database.getConnection()) {
|
||||
Statement statement = connection.createStatement();
|
||||
for (String schema : schemas) {
|
||||
statement.execute("CREATE SCHEMA " + schema);
|
||||
}
|
||||
statement.close();
|
||||
} catch (SQLException ex) {
|
||||
getLog().error("An error occurred while creating the schemas " + schemas);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
/** The method sets the directory for placing postgre service files */
|
||||
private String prepareDbDir() {
|
||||
if (StringUtils.isEmpty(dbDir)) {
|
||||
return projectBuildDir + File.separator + "pgdata";
|
||||
} else {
|
||||
return dbDir;
|
||||
}
|
||||
}
|
||||
|
||||
public static EmbeddedPostgres getEmbeddedPostgres() {
|
||||
return embeddedPostgres;
|
||||
}
|
||||
|
||||
public static Thread getPostgresThread() {
|
||||
return postgresThread;
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.rbkmoney.maven.plugins.pg_embedded_plugin;
|
||||
|
||||
import com.opentable.db.postgres.embedded.EmbeddedPostgres;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
import org.apache.maven.plugins.annotations.LifecyclePhase;
|
||||
import org.apache.maven.plugins.annotations.Mojo;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Class to stop the embedded server
|
||||
*
|
||||
* @author d.baykov
|
||||
*/
|
||||
@Mojo(name = "stop", defaultPhase = LifecyclePhase.COMPILE, threadSafe = true)
|
||||
public class StopPgServerMojo extends GeneralMojo {
|
||||
|
||||
/** Instance of the PostgreSQL */
|
||||
private EmbeddedPostgres embeddedPostgres;
|
||||
|
||||
@Override
|
||||
protected void doExecute() throws MojoExecutionException, MojoFailureException {
|
||||
embeddedPostgres = StartPgServerMojo.getEmbeddedPostgres();
|
||||
if (embeddedPostgres == null) {
|
||||
getLog().info("The PG server wasn't started");
|
||||
} else {
|
||||
try {
|
||||
getLog().info("Stopping the PG server...");
|
||||
embeddedPostgres.close();
|
||||
getLog().info("The PG server stopped");
|
||||
} catch (IOException e) {
|
||||
getLog().error("Error encountered while stopping the server ", e);
|
||||
}
|
||||
}
|
||||
StartPgServerMojo.getPostgresThread().interrupt();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user