diff --git a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java index 873cbd6cc..b1701c7c7 100644 --- a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java +++ b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java @@ -10,7 +10,6 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; -import java.util.Arrays; import java.util.Enumeration; import java.util.List; @@ -40,7 +39,10 @@ public class ConfigurationMappingLoader { /** * The default names of configuration files */ - public static final List DEFAULT_CONFIG_LOCATIONS = Arrays.asList(".jqassistant.yml", ".jqassistant.yaml", ".jqassistant"); + public static final List DEFAULT_CONFIG_LOCATIONS = List.of(".jqassistant.yml", ".jqassistant.yaml", ".jqassistant") + .stream() + .map(Paths::get) + .collect(toList()); /** * The ordinal for config sources from the user home. @@ -78,7 +80,7 @@ public static Builder builder(Class configuratio * @param configurationMapping * The {@link Configuration} mapping. * @param configLocations - * The name of the configuration directory relative to the working directory. + * The names of the configuration locations. These may either be absolute paths or relative paths to the working directory. */ public static Builder builder(Class configurationMapping, List configLocations) { return new Builder<>(configurationMapping, configLocations); @@ -90,7 +92,7 @@ public static class Builder { private final Class configurationMapping; - private final List configLocations; + private final List relativeConfigLocations; private final List configSources = new ArrayList<>(); @@ -98,7 +100,19 @@ public static class Builder { private Builder(Class configurationMapping, List configLocations) { this.configurationMapping = configurationMapping; - this.configLocations = configLocations; + if (configLocations.isEmpty()) { + this.relativeConfigLocations = DEFAULT_CONFIG_LOCATIONS; + } else { + this.relativeConfigLocations = new ArrayList<>(); + for (String configLocation : configLocations) { + Path configLocationPath = Paths.get(configLocation); + if (configLocationPath.isAbsolute()) { + this.configSources.addAll(getExternalYamlConfigSources(configLocationPath, ORDINAL_WORKING_DIRECTORY)); + } else { + this.relativeConfigLocations.add(configLocationPath); + } + } + } } /** @@ -134,7 +148,7 @@ public Builder withWorkingDirectory(File workingDirectory) { * @return The {@link Builder}. */ public Builder withDirectory(File directory, int ordinal) { - configSources.addAll(getExternalYamlConfigSources(directory, configLocations.isEmpty() ? DEFAULT_CONFIG_LOCATIONS : configLocations, ordinal)); + configSources.addAll(getExternalYamlConfigSources(directory, relativeConfigLocations, ordinal)); return this; } @@ -197,35 +211,36 @@ public C load(ConfigSource... additionalConfigSources) { return configMapping; } - private static List getExternalYamlConfigSources(File directory, List configLocations, int ordinal) { + private List getExternalYamlConfigSources(File directory, List configLocations, int ordinal) { List configSources = new ArrayList<>(); - for (String configLocation : configLocations) { - File file = directory.toPath() - .resolve(Paths.get(configLocation)) - .toFile(); - if (file.exists()) { - if (file.isDirectory()) { - configSources.addAll(getYamlConfigSources(file, ordinal)); - } else { - configSources.add(getYamlConfigSource(file.toPath(), ordinal)); - } - } + for (Path configLocation : configLocations) { + Path path = directory.toPath() + .resolve(configLocation); + configSources.addAll(getExternalYamlConfigSources(path, ordinal)); } return configSources; } - private static List getYamlConfigSources(File configurationDirectory, int ordinal) { - log.info("Loading configuration from directory '{}'.", configurationDirectory.getAbsolutePath()); - List configurationFiles = getYamlConfigurationFiles(configurationDirectory); - return configurationFiles.stream() - .map(path -> getYamlConfigSource(path, ordinal)) - .collect(toList()); + private List getExternalYamlConfigSources(Path configLocationPath, int ordinal) { + File file = configLocationPath.toFile(); + if (!file.exists()) { + return emptyList(); + } + if (file.isDirectory()) { + log.info("Loading configuration from directory '{}'.", configLocationPath.toAbsolutePath()); + List configurationFiles = findYamlConfigurationFiles(configLocationPath); + return configurationFiles.stream() + .map(path -> getYamlConfigSource(path, ordinal)) + .collect(toList()); + } else { + return List.of(getYamlConfigSource(configLocationPath, ordinal)); + } } - private static List getYamlConfigurationFiles(File configurationDirectory) { + private List findYamlConfigurationFiles(Path configurationDirectory) { List configurationFiles = new ArrayList<>(); try { - walkFileTree(configurationDirectory.toPath(), new SimpleFileVisitor() { + walkFileTree(configurationDirectory, new SimpleFileVisitor() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { @@ -244,8 +259,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { return configurationFiles; } - private static YamlConfigSource getYamlConfigSource(Path path, int ordinal) { - log.info("Loading configuration from file '{}'.", path.toAbsolutePath()); + private ConfigSource getYamlConfigSource(Path path, int ordinal) { try { return getYamlConfigSource(path.toUri() .toURL(), ordinal); @@ -254,7 +268,7 @@ private static YamlConfigSource getYamlConfigSource(Path path, int ordinal) { } } - private static List getYamlConfigSourceFromClasspath() { + private List getYamlConfigSourceFromClasspath() { try { Enumeration resources = Thread.currentThread() .getContextClassLoader() @@ -267,13 +281,13 @@ private static List getYamlConfigSourceFromClasspath() { } } - private static YamlConfigSource getYamlConfigSource(URL url, int ordinal) { + private ConfigSource getYamlConfigSource(URL url, int ordinal) { + log.info("Loading YAML configuration from '{}' (priority: {}).", url, ordinal); try { return new YamlConfigSource(url, ordinal); } catch (IOException e) { throw new IllegalArgumentException("Cannot create YAML config source from URL " + url, e); } } - } }