
com.googlecode.flyway.maven.AbstractFlywayMojo Maven / Gradle / Ivy
/**
* Copyright 2010-2013 Axel Fontaine and the many contributors.
*
* 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.api.FlywayException;
import com.googlecode.flyway.core.util.ExceptionUtils;
import com.googlecode.flyway.core.util.StringUtils;
import com.googlecode.flyway.core.util.jdbc.DriverDataSource;
import com.googlecode.flyway.core.util.logging.Log;
import com.googlecode.flyway.core.util.logging.LogFactory;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
import org.sonatype.plexus.components.cipher.PlexusCipherException;
import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
import javax.sql.DataSource;
/**
* Common base class for all mojos with all common attributes.
*
* @requiresDependencyResolution test
* @configurator include-project-dependencies
*/
@SuppressWarnings({"JavaDoc", "FieldCanBeLocal", "UnusedDeclaration"})
abstract class AbstractFlywayMojo extends AbstractMojo {
protected Log log;
/**
* Whether to skip the execution of the Maven Plugin for this module.
* Also configurable with Maven or System Property: ${flyway.skip}
*
* @parameter property="flyway.skip"
*/
/* private -> for testing */ boolean skip;
/**
* The fully qualified classname of the jdbc driver to use to connect to the database.
* By default, the driver is autodetected based on the url.
* Also configurable with Maven or System Property: ${flyway.driver}
*
* @parameter property="flyway.driver"
*/
/* private -> for testing */ String driver;
/**
* The jdbc url to use to connect to the database.
* Also configurable with Maven or System Property: ${flyway.url}
*
* @parameter property="flyway.url"
* @required
*/
/* private -> for testing */ String url;
/**
* The user to use to connect to the database. (default: blank)
* The credentials can be specified by user/password or {@code serverId} from settings.xml
* Also configurable with Maven or System Property: ${flyway.user}
*
* @parameter property="flyway.user"
*/
/* private -> for testing */ String user;
/**
* The password to use to connect to the database. (default: blank)
* Also configurable with Maven or System Property: ${flyway.password}
*
* @parameter property="flyway.password"
*/
private String password;
/**
* List of the schemas managed by Flyway. These schema names are case-sensitive.
* (default: The default schema for the datasource connection)
* Consequences:
*
* - The first schema in the list will be automatically set as the default one during the migration.
* - The first schema in the list will also be the one containing the metadata table.
* - The schemas will be cleaned in the order of this list.
*
* Also configurable with Maven or System Property: ${flyway.schemas} (comma-separated list)
*
* @parameter property="flyway.schemas"
*/
private String[] schemas;
/**
* The name of the metadata table that will be used by Flyway. (default: schema_version)
* By default (single-schema mode) the
* metadata table is placed in the default schema for the connection provided by the datasource.
When the
* {@code flyway.schemas} property is set (multi-schema mode), the metadata table is placed in the first schema of
* the list.
* Also configurable with Maven or System Property: ${flyway.table}
*
* @parameter property="flyway.table"
*/
private String table;
/**
* The version to tag an existing schema with when executing init. (default: 1)
* Also configurable with Maven or System Property: ${flyway.initialVersion}
*
* @parameter property="flyway.initialVersion"
* @deprecated Use initVersion instead. Will be removed in Flyway 3.0.
*/
@Deprecated
private String initialVersion;
/**
* The description to tag an existing schema with when executing init. (default: << Flyway Init >>)
* Also configurable with Maven or System Property: ${flyway.initialDescription}
*
* @parameter property="flyway.initialDescription"
* @deprecated Use initDescription instead. Will be removed in Flyway 3.0.
*/
@Deprecated
private String initialDescription;
/**
* The version to tag an existing schema with when executing init. (default: 1)
* Also configurable with Maven or System Property: ${flyway.initVersion}
*
* @parameter property="flyway.initVersion"
*/
private String initVersion;
/**
* The description to tag an existing schema with when executing init. (default: << Flyway Init >>)
* Also configurable with Maven or System Property: ${flyway.initDescription}
*
* @parameter property="flyway.initDescription"
*/
private String initDescription;
/**
* The id of the server tag in settings.xml (default: flyway-db)
* The credentials can be specified by user/password or {@code serverId} from settings.xml
* Also configurable with Maven or System Property: ${flyway.serverId}
*
* @parameter property="flyway.serverId"
*/
private String serverId = "flyway-db";
/**
* The link to the settings.xml
*
* @parameter property="settings"
* @required
* @readonly
*/
private Settings settings;
/**
* Reference to the current project that includes the Flyway Maven plugin.
*
* @parameter property="project" required="true"
*/
protected MavenProject mavenProject;
/**
* 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) {
user = server.getUsername();
try {
SecDispatcher secDispatcher = new DefaultSecDispatcher() {{
_cipher = new DefaultPlexusCipher();
}};
password = secDispatcher.decrypt(server.getPassword());
} catch (SecDispatcherException e) {
throw new FlywayException("Unable to decrypt password", e);
} catch (PlexusCipherException e) {
throw new FlywayException("Unable to initialized password decryption", e);
}
}
}
}
/**
* 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 {
return new DriverDataSource(driver, url, user, password);
}
public final void execute() throws MojoExecutionException, MojoFailureException {
LogFactory.setLogCreator(new MavenLogCreator(this));
log = LogFactory.getLog(getClass());
if (skip) {
log.info("Skipping Flyway execution");
return;
}
try {
loadCredentialsFromSettings();
Flyway flyway = new Flyway();
flyway.setDataSource(createDataSource());
String schemasProperty = mavenProject.getProperties().getProperty("flyway.schemas");
if (schemasProperty != null) {
flyway.setSchemas(StringUtils.tokenizeToStringArray(schemasProperty, ","));
} else if (schemas != null) {
flyway.setSchemas(schemas);
}
if (table != null) {
flyway.setTable(table);
}
if (initialVersion != null) {
flyway.setInitialVersion(initialVersion);
}
if (initialDescription != null) {
flyway.setInitialDescription(initialDescription);
}
if (initVersion != null) {
flyway.setInitVersion(initVersion);
}
if (initDescription != null) {
flyway.setInitDescription(initDescription);
}
doExecute(flyway);
} catch (Exception e) {
throw new MojoExecutionException(e.toString(), ExceptionUtils.getRootCause(e));
}
}
/**
* Retrieves this property from either the system or the maven properties.
*
* @param name The name of the property to retrieve.
* @return The property value. {@code null} if not found.
*/
protected String getProperty(String name) {
String systemProperty = System.getProperty(name);
if (systemProperty != null) {
return systemProperty;
}
return mavenProject.getProperties().getProperty(name);
}
/**
* Executes this mojo.
*
* @param flyway The flyway instance to operate on.
* @throws Exception any exception
*/
protected abstract void doExecute(Flyway flyway) throws Exception;
}