Skip to content

Commit

Permalink
Provide option to override catalog versions from CLI (#594)
Browse files Browse the repository at this point in the history
This commit adds the ability to override the versions of dependencies
declared in the catalogs that the build uses. For example, to override
the `libs.managed.jackson` version, you can specify on the command-line:

`./gradlew :foo -Poverride.libs.managed.jackson=2.6.0`

Alternatively, it is possible to provide an override properties file:

`./gradlew foo -Poverride.file.libs=overrides.properties`

and the properties file must contain the following entry:

`managed.jackson=2.6.0`

in order to behave the same as the 1st example.
  • Loading branch information
melix authored Jul 28, 2023
1 parent 3393dc1 commit 048b5fe
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
Expand All @@ -62,20 +64,23 @@ public abstract class MicronautBuildSettingsExtension {
* This makes it cleaner for publications, since the project name will match
* the publication artifact id, making it easier to use composite builds.
* Defaults to false.
*
* @return the standardized project names property
*/
public abstract Property<Boolean> getUseStandardizedProjectNames();

/**
* Configures a list of project path prefixes which correspond to projects which do
* not use the "standard" project naming convention (e.g `examples:`).
*
* @return the list of non-standard project path prefixes
*/
public abstract ListProperty<String> getNonStandardProjectPathPrefixes();

/**
* Configures a list of project name prefixes which correspond to projects which do
* not use the "standard" project naming convention (e.g `test-suite-`).
*
* @return the list of non-standard project path prefixes
*/
public abstract ListProperty<String> getNonStandardProjectNamePrefixes();
Expand All @@ -102,6 +107,35 @@ public MicronautBuildSettingsExtension(ProviderFactory providers, Settings setti
this.micronautVersion = determineMicronautVersion();
this.micronautTestVersion = determineMicronautTestVersion();
this.micronautLoggingVersion = determineMicronautLoggingVersion();
settings.getDependencyResolutionManagement().getVersionCatalogs().configureEach(catalog -> {
var prefix = "override." + catalog.getName() + ".";
var provider = providers.gradlePropertiesPrefixedBy(prefix);
if (provider.isPresent()) {
for (Map.Entry<String, String> entry : provider.get().entrySet()) {
var key = entry.getKey().substring(prefix.length());
var version = entry.getValue();
catalog.version(key, version);
LOGGER.info("Overriding {} version to {}", key, version);
}
}
var overrideFile = providers.gradleProperty("override.file." + catalog.getName());
if (overrideFile.isPresent()) {
try {
List<String> overrides = Files.readAllLines(Path.of(overrideFile.get()));
for (String override : overrides) {
var entry = override.split("=");
if (entry.length == 2) {
var key = entry[0].trim();
var version = entry[1].trim();
catalog.version(key, version);
LOGGER.info("Overriding {} version to {}", key, version);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}

private VersionCatalogTomlModel loadVersionCatalogTomlModel() {
Expand Down

0 comments on commit 048b5fe

Please sign in to comment.