Skip to content

Commit

Permalink
flyway#1738 Introduced generic configurations property
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel Fontaine committed Feb 16, 2018
1 parent 77859ae commit 813e591
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public class ConfigUtils {
// Command-line specific
public static final String JAR_DIRS = "flyway.jarDirs";

// Gradle specific
public static final String CONFIGURATIONS = "flyway.configurations";

private ConfigUtils() {
// Utility class
}
Expand Down Expand Up @@ -252,6 +255,11 @@ private static String convertKey(String key) {
return JAR_DIRS;
}

// Gradle specific
if ("FLYWAY_CONFIGURATIONS".equals(key)) {
return CONFIGURATIONS;
}

return null;
}

Expand Down
22 changes: 21 additions & 1 deletion flyway-gradle-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<name>${project.artifactId}</name>
<dependencies>
<dependency>
<!-- Force correct module ordering -->
<groupId>${project.groupId}</groupId>
<artifactId>flyway-core</artifactId>
<version>${project.version}</version>
Expand Down Expand Up @@ -64,6 +63,13 @@










</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -91,6 +97,20 @@
</execution>
</executions>
</plugin>














</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package org.flywaydb.gradle;

import org.gradle.api.artifacts.Configuration;

import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -288,12 +285,11 @@ public class FlywayExtension {
public String installedBy;

/**
* Configurations that will be added to the classpath for running flyway tasks.
* <p>
* By default flyway respects <code>compile</code>, <code>runtime</code>, <code>testCompile</code>, <code>testRuntime</code> (in this order).
* {@code empty list or null} for accepting the default classpath. (default: {@code null}).
* Gradle configurations that will be added to the classpath for running Flyway tasks.
* (default: <code>compile</code>, <code>runtime</code>, <code>testCompile</code>, <code>testRuntime</code>)
* <p>Also configurable with Gradle or System Property: ${flyway.configurations}</p>
*/
public List<Configuration> classpathExtensions;
public String[] configurations;

/**
* The fully qualified class names of handlers for errors and warnings that occur during a migration. This can be
Expand Down Expand Up @@ -329,4 +325,4 @@ public class FlywayExtension {
* <p>Also configurable with Gradle or System Property: ${flyway.configFiles}</p>
*/
public String[] configFiles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.flywaydb.core.internal.util.StringUtils;
import org.flywaydb.gradle.FlywayExtension;
import org.gradle.api.DefaultTask;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.artifacts.ResolvedConfiguration;
import org.gradle.api.file.FileCollection;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
Expand All @@ -32,17 +32,18 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import static org.flywaydb.core.internal.configuration.ConfigUtils.putIfSet;

Expand All @@ -51,6 +52,11 @@
*/
@SuppressWarnings("WeakerAccess")
public abstract class AbstractFlywayTask extends DefaultTask {
/**
* The default Gradle configurations to use.
*/
private static final String[] DEFAULT_CONFIGURATIONS = {"compile", "runtime", "testCompile", "testRuntime"};

/**
* The flyway {} block in the build script.
*/
Expand Down Expand Up @@ -318,12 +324,11 @@ public abstract class AbstractFlywayTask extends DefaultTask {
public String installedBy;

/**
* Configurations that will be added to the classpath for running flyway tasks.
* <p>
* By default flyway respects <code>compile</code>, <code>runtime</code>, <code>testCompile</code>, <code>testRuntime</code> (in this order).
* {@code empty list or null} for accepting the default classpath. (default: {@code null}).
* Gradle configurations that will be added to the classpath for running Flyway tasks.
* (default: <code>compile</code>, <code>runtime</code>, <code>testCompile</code>, <code>testRuntime</code>)
* <p>Also configurable with Gradle or System Property: ${flyway.configurations}</p>
*/
public List<Configuration> classpathExtensions;
public String[] configurations;

/**
* The fully qualified class names of handlers for errors and warnings that occur during a migration. This can be
Expand Down Expand Up @@ -370,79 +375,80 @@ public AbstractFlywayTask() {
@TaskAction
public Object runTask() {
try {
List<URL> extraURLs = new ArrayList<>();
if (isJavaProject()) {
JavaPluginConvention plugin = getProject().getConvention().getPlugin(JavaPluginConvention.class);

for (SourceSet sourceSet : plugin.getSourceSets()) {
try {
@SuppressWarnings("JavaReflectionMemberAccess")
Method getClassesDirs = SourceSetOutput.class.getMethod("getClassesDirs");

// use alternative method available in Gradle 4.0
FileCollection classesDirs = (FileCollection) getClassesDirs.invoke(sourceSet.getOutput());
for (File directory : classesDirs.getFiles()) {
URL classesUrl = directory.toURI().toURL();
getLogger().debug("Adding directory to Classpath: " + classesUrl);
extraURLs.add(classesUrl);
}
} catch (NoSuchMethodException e) {
// use original method available in Gradle 3.x
URL classesUrl = sourceSet.getOutput().getClassesDir().toURI().toURL();
getLogger().debug("Adding directory to Classpath: " + classesUrl);
extraURLs.add(classesUrl);
}

URL resourcesUrl = sourceSet.getOutput().getResourcesDir().toURI().toURL();
getLogger().debug("Adding directory to Classpath: " + resourcesUrl);
extraURLs.add(resourcesUrl);
}
Map<String, String> envVars = ConfigUtils.environmentVariablesToPropertyMap();

addClasspathDependencies(extraURLs);
Set<URL> extraURLs = new HashSet<>();
if (isJavaProject()) {
addClassesAndResourcesDirs(extraURLs);
addConfigurationArtifacts(determineConfigurations(envVars), extraURLs);
}

ClassLoader classLoader = new URLClassLoader(
extraURLs.toArray(new URL[extraURLs.size()]),
getProject().getBuildscript().getClassLoader());

return run(Flyway.config(classLoader).configure(createFlywayConfig()).load());
return run(Flyway.config(classLoader).configure(createFlywayConfig(envVars)).load());
} catch (Exception e) {
handleException(e);
return null;
}
}

// classpath methods having protected access to allow for adaption/override in a users custom flyway tasks
protected void addClasspathDependencies(List<URL> urls) throws IOException {
addDefaultClasspathDependencies(urls);
addClasspathExtensionDependencies(urls);
}
private void addClassesAndResourcesDirs(Set<URL> extraURLs) throws IllegalAccessException, InvocationTargetException, MalformedURLException {
JavaPluginConvention plugin = getProject().getConvention().getPlugin(JavaPluginConvention.class);

protected void addDefaultClasspathDependencies(List<URL> urls) throws IOException {
for (String scope : Arrays.asList("compile", "runtime", "testCompile", "testRuntime")) {
addDependenciesWithScope(urls, scope);
}
}
for (SourceSet sourceSet : plugin.getSourceSets()) {
try {
@SuppressWarnings("JavaReflectionMemberAccess")
Method getClassesDirs = SourceSetOutput.class.getMethod("getClassesDirs");

protected void addDependenciesWithScope(List<URL> urls, String scope) throws IOException {
final Configuration configuration = getProject().getConfigurations().getByName(scope);
addDependenciesWithScope(urls, configuration);
// use alternative method available in Gradle 4.0
FileCollection classesDirs = (FileCollection) getClassesDirs.invoke(sourceSet.getOutput());
for (File directory : classesDirs.getFiles()) {
URL classesUrl = directory.toURI().toURL();
getLogger().debug("Adding directory to Classpath: " + classesUrl);
extraURLs.add(classesUrl);
}
} catch (NoSuchMethodException e) {
// use original method available in Gradle 3.x
URL classesUrl = sourceSet.getOutput().getClassesDir().toURI().toURL();
getLogger().debug("Adding directory to Classpath: " + classesUrl);
extraURLs.add(classesUrl);
}

URL resourcesUrl = sourceSet.getOutput().getResourcesDir().toURI().toURL();
getLogger().debug("Adding directory to Classpath: " + resourcesUrl);
extraURLs.add(resourcesUrl);
}
}

protected void addClasspathExtensionDependencies(List<URL> urls) throws IOException {
List<Configuration> classPathConfigs = this.classpathExtensions != null ? this.classpathExtensions : nullToEmpty(this.extension.classpathExtensions);
for (Configuration configuration : classPathConfigs) {
getLogger().debug("Adding extension to classpath: " + configuration);
addDependenciesWithScope(urls, configuration);
private void addConfigurationArtifacts(String[] configurations, Set<URL> urls) throws IOException {
for (String configuration : configurations) {
getLogger().debug("Adding configuration to classpath: " + configuration);
ResolvedConfiguration resolvedConfiguration =
getProject().getConfigurations().getByName(configuration).getResolvedConfiguration();
for (ResolvedArtifact artifact : resolvedConfiguration.getResolvedArtifacts()) {
URL artifactUrl = artifact.getFile().toURI().toURL();
getLogger().debug("Adding artifact to classpath: " + artifactUrl);
urls.add(artifactUrl);
}
}
}

protected void addDependenciesWithScope(List<URL> urls, Configuration configuration) throws MalformedURLException {
for (ResolvedArtifact artifact : configuration.getResolvedConfiguration().getResolvedArtifacts()) {
URL artifactUrl = artifact.getFile().toURI().toURL();
getLogger().debug("Adding dependency to classpath: " + artifactUrl);
urls.add(artifactUrl);
private String[] determineConfigurations(Map<String, String> envVars) {
if (envVars.containsKey(ConfigUtils.CONFIGURATIONS)) {
return StringUtils.tokenizeToStringArray(envVars.get(ConfigUtils.CONFIGURATIONS), ",");
}
if (System.getProperties().containsKey(ConfigUtils.CONFIGURATIONS)) {
return StringUtils.tokenizeToStringArray(System.getProperties().getProperty(ConfigUtils.CONFIGURATIONS), ",");
}
if (configurations != null) {
return configurations;
}
if (extension.configurations != null) {
return extension.configurations;
}
return DEFAULT_CONFIGURATIONS;
}

/**
Expand All @@ -456,11 +462,10 @@ protected void addDependenciesWithScope(List<URL> urls, Configuration configurat
/**
* Creates the Flyway config to use.
*/
private Map<String, String> createFlywayConfig() {
private Map<String, String> createFlywayConfig(Map<String, String> envVars) {
Map<String, String> conf = new HashMap<>();
conf.put(ConfigUtils.LOCATIONS, Location.FILESYSTEM_PREFIX + getProject().getProjectDir().getAbsolutePath() + "/src/main/resources/db/migration");

Map<String, String> envVars = ConfigUtils.environmentVariablesToPropertyMap();
addConfigFromProperties(conf, loadConfigurationFromDefaultConfigFiles(envVars));

putIfSet(conf, ConfigUtils.DRIVER, driver, extension.driver);
Expand Down Expand Up @@ -643,6 +648,7 @@ private File toFile(String fileName) {
private static void removeGradlePluginSpecificPropertiesToAvoidWarnings(Map<String, String> conf) {
conf.remove(ConfigUtils.CONFIG_FILES);
conf.remove(ConfigUtils.CONFIG_FILE_ENCODING);
conf.remove(ConfigUtils.CONFIGURATIONS);
conf.remove("flyway.version");
}

Expand Down Expand Up @@ -685,14 +691,7 @@ private String collectMessages(Throwable throwable, String message) {
return message;
}

private List<Configuration> nullToEmpty(List<Configuration> input) {
if (input != null) {
return input;
}
return Collections.emptyList();
}

private boolean isJavaProject() {
return getProject().getPluginManager().hasPlugin("java");
}
}
}
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@




</modules>

<distributionManagement>
Expand Down

0 comments on commit 813e591

Please sign in to comment.