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

com.googlecode.flyway.maven.AbstractFlywayMojo Maven / Gradle / Ivy

/**
 * Copyright (C) 2010-2012 the original author or authors.
 *
 * 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 com.googlecode.flyway.maven;

import com.googlecode.flyway.core.Flyway;
import com.googlecode.flyway.core.exception.FlywayException;
import com.googlecode.flyway.core.util.ClassUtils;
import com.googlecode.flyway.core.util.ExceptionUtils;
import com.googlecode.flyway.core.util.StringUtils;
import com.googlecode.flyway.core.util.jdbc.DriverDataSource;
import com.pyx4j.log4j.MavenLogAppender;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;

import javax.sql.DataSource;
import java.sql.Driver;

/**
 * Common base class for all mojos with all common attributes.
* * @requiresDependencyResolution compile * @configurator include-project-dependencies */ @SuppressWarnings({"JavaDoc", "FieldCanBeLocal"}) abstract class AbstractFlywayMojo extends AbstractMojo { /** * Logger. */ private static final Log LOG = LogFactory.getLog(AbstractFlywayMojo.class); /** * The fully qualified classname of the jdbc driver to use to connect to the database.
default property: * ${flyway.driver} * * @parameter expression="${flyway.driver}" * @required */ /* private -> for testing */ String driver; /** * The jdbc url to use to connect to the database.
default property: ${flyway.url} * * @parameter expression="${flyway.url}" * @required */ /* private -> for testing */ String url; /** * The user to use to connect to the database.
default property: ${flyway.user}
* The credentials can be specified by user/password or serverId from settings.xml * * @parameter expression="${flyway.user}" */ /* private -> for testing */ String user; /** * The password to use to connect to the database. (default: blank)
default property: ${flyway.password} * * @parameter expression="${flyway.password}" */ private String password = ""; /** * Comma-separated list of the schemas managed by Flyway. The first schema in the list will be the one containing * the metadata table. (default: The default schema for the datasource connection)
default property: * ${flyway.schemas} * * @parameter expression="${flyway.schemas}" */ private String schemas; /** *

The name of the schema metadata table that will be used by Flyway.

By default (single-schema mode) the * metadata table is placed in the default schema for the connection provided by the datasource.

When the * flyway.schemas property is set (multi-schema mode), the metadata table is placed in the first schema of * the list.

(default: schema_version)
default property: ${flyway.table} * * @parameter expression="${flyway.table}" */ private String table; /** * The link to the settings.xml * * @parameter expression="${settings}" * @required * @readonly */ private Settings settings; /** * The id of the server tag in settings.xml
default: flyway-db
* The credentials can be specified by user/password or serverId from settings.xml
default property: * ${flyway.serverId} * * @parameter expression="${flyway.serverId}" */ private String serverId = "flyway-db"; /** * Load username password from settings * * @throws FlywayException when the credentials could not be loaded. */ private void loadCredentialsFromSettings() throws FlywayException { if (user == null) { final Server server = settings.getServer(serverId); if (server == null) { throw new FlywayException(String.format("Database username missing. It was not specified as a property" + " and it was not defined in settings.xml for the server with the id '%s'", serverId)); } user = server.getUsername(); password = server.getPassword(); } } /** * Creates the datasource based on the provided parameters. * * @return The fully configured datasource. * @throws Exception Thrown when the datasource could not be created. */ /* private -> for testing */ DataSource createDataSource() throws Exception { DriverDataSource dataSource = new DriverDataSource(); dataSource.setDriver(ClassUtils.instantiate(driver)); dataSource.setUrl(url); dataSource.setUser(user); dataSource.setPassword(password); return dataSource; } public final void execute() throws MojoExecutionException, MojoFailureException { MavenLogAppender.startPluginLog(this); try { loadCredentialsFromSettings(); Flyway flyway = new Flyway(); flyway.setDataSource(createDataSource()); if (schemas != null) { flyway.setSchemas(StringUtils.tokenizeToStringArray(schemas, ",")); } if (table != null) { flyway.setTable(table); } doExecute(flyway); } catch (Exception e) { LOG.error(e.toString()); Throwable rootCause = ExceptionUtils.getRootCause(e); if (rootCause != null) { LOG.error("Caused by " + rootCause.toString()); } throw new MojoExecutionException("Flyway Error: " + e.toString(), e); } finally { MavenLogAppender.endPluginLog(this); } } /** * Executes this mojo. * * @param flyway The flyway instance to operate on. * @throws Exception any exception */ protected abstract void doExecute(Flyway flyway) throws Exception; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy