I’d like to be able to have a plugin enable --write-locks dynamically. The api appears to support it, but at the time of writing this, appears to not work for me.
I have something along these lines:
public class DependencyLocking implements Plugin<Project> {
public static final String RESOLVE_AND_LOCK_ALL = "resolveAndLockAll";
public static final String COMPILE_CLASSPATH = "compileClasspath";
public static final String RUNTIME_CLASSPATH = "runtimeClasspath";
@Override
public void apply(Project project) {
if(!project.hasProperty("CI")) {
project.getGradle().getStartParameter().setWriteDependencyLocks(true);
}
project.getConfigurations().getByName(COMPILE_CLASSPATH).resolutionStrategy(ResolutionStrategy::activateDependencyLocking);
project.getConfigurations().getByName(RUNTIME_CLASSPATH).resolutionStrategy(ResolutionStrategy::activateDependencyLocking);
project.getTasks().register(RESOLVE_AND_LOCK_ALL);
Task resolveAndLockAll = project.getTasks().getByName(RESOLVE_AND_LOCK_ALL);
resolveAndLockAll.doFirst((task)-> {
assert task.getProject().getGradle().getStartParameter().isWriteDependencyLocks();
});
resolveAndLockAll.doLast((task) -> {
resolveConfiguration(project, COMPILE_CLASSPATH);
resolveConfiguration(project, RUNTIME_CLASSPATH);
});
}
private void resolveConfiguration(Project project, String configurationName) {
Configuration configuration = project.getConfigurations().getByName(configurationName);
if(configuration.isCanBeResolved()) {
configuration.resolve();
}
}
}
The weird thing is that the assert passes, but the lockfiles are not written.