All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.sql.maven.plugin.CreateDatabaseMojo Maven / Gradle / Ivy

/*
 * Copyright 2015 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.sql.maven.plugin;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.maven.plugin.AbstractMojo;
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;

/**
 * @author [email protected] (Marcio Endo)
 */
@Mojo(name = "create-database", defaultPhase = LifecyclePhase.VALIDATE)
public class CreateDatabaseMojo extends AbstractMojo {

  @Parameter(required = true)
  Vendor vendor;

  @Parameter(required = true)
  String server;

  @Parameter(required = true)
  int port;

  @Parameter(required = true)
  String db;

  @Parameter(required = true)
  String user;

  @Parameter(required = true)
  String password;

  @Parameter
  private boolean skip;

  @Override
  public void execute() throws MojoExecutionException, MojoFailureException {
    if (!skip) {
      try {
        execute0();
      } catch (ClassNotFoundException e) {
        throw new MojoExecutionException("Could not load JDBC driver. "
            + "Are you sure you defined the correct plugin dependency?", e);
      } catch (SQLException e) {
        throw new MojoExecutionException("Could not create database", e);
      }
    }
  }

  private void execute0() throws ClassNotFoundException, SQLException {
    vendor.loadDriver();
    String url = vendor.url(server, port);
    try (Connection connection = DriverManager.getConnection(url, user, password)) {
      try (Statement stmt = connection.createStatement()) {
        vendor.createDatabase(stmt, db);
      }
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy