Skip to content

Configurable gradle configurations #3034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public void buildDocker()
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();

GradleProjectProperties projectProperties =
GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider);
GradleProjectProperties.getForProject(
getProject(), getLogger(), tempDirectoryProvider, jibExtension.readConfigurationName());
GlobalConfig globalConfig = GlobalConfig.readConfig();
Future<Optional<String>> updateCheckFuture =
TaskCommon.newUpdateChecker(projectProperties, globalConfig, getLogger());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void buildImage()
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();

GradleProjectProperties projectProperties =
GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider);
GradleProjectProperties.getForProject(
getProject(), getLogger(), tempDirectoryProvider, jibExtension.readConfigurationName());
GlobalConfig globalConfig = GlobalConfig.readConfig();
Future<Optional<String>> updateCheckFuture =
TaskCommon.newUpdateChecker(projectProperties, globalConfig, getLogger());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public FileCollection getInputFiles() {
.stream()
.map(ExtraDirectoryParameters::getFrom)
.collect(Collectors.toList());
return GradleProjectProperties.getInputFiles(getProject(), extraDirectories);
return GradleProjectProperties.getInputFiles(
getProject(), extraDirectories, jibExtension.readConfigurationName());
}

/**
Expand Down Expand Up @@ -127,7 +128,8 @@ public void buildTar()
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();

GradleProjectProperties projectProperties =
GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider);
GradleProjectProperties.getForProject(
getProject(), getLogger(), tempDirectoryProvider, jibExtension.readConfigurationName());
GlobalConfig globalConfig = GlobalConfig.readConfig();
Future<Optional<String>> updateCheckFuture =
TaskCommon.newUpdateChecker(projectProperties, globalConfig, getLogger());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
import org.gradle.api.artifacts.component.ProjectComponentIdentifier;
import org.gradle.api.file.FileCollection;
import org.gradle.api.logging.Logger;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.WarPlugin;
import org.gradle.api.tasks.SourceSet;
Expand Down Expand Up @@ -104,10 +103,15 @@ public class GradleProjectProperties implements ProjectProperties {
* @param project a gradle project
* @param logger a gradle logging instance to use for logging during the build
* @param tempDirectoryProvider for scratch space during the build
* @param configurationName the configuration of which the dependencies should be packed into the
* container
* @return a GradleProjectProperties instance to use in a jib build
*/
public static GradleProjectProperties getForProject(
Project project, Logger logger, TempDirectoryProvider tempDirectoryProvider) {
Project project,
Logger logger,
TempDirectoryProvider tempDirectoryProvider,
String configurationName) {
Supplier<List<JibGradlePluginExtension<?>>> extensionLoader =
() -> {
List<JibGradlePluginExtension<?>> extensions = new ArrayList<>();
Expand All @@ -117,7 +121,8 @@ public static GradleProjectProperties getForProject(
}
return extensions;
};
return new GradleProjectProperties(project, logger, tempDirectoryProvider, extensionLoader);
return new GradleProjectProperties(
project, logger, tempDirectoryProvider, extensionLoader, configurationName);
}

String getWarFilePath() {
Expand Down Expand Up @@ -155,16 +160,19 @@ private static boolean isProgressFooterEnabled(Project project) {
private final ConsoleLogger consoleLogger;
private final TempDirectoryProvider tempDirectoryProvider;
private final Supplier<List<JibGradlePluginExtension<?>>> extensionLoader;
private final String configurationName;

@VisibleForTesting
GradleProjectProperties(
Project project,
Logger logger,
TempDirectoryProvider tempDirectoryProvider,
Supplier<List<JibGradlePluginExtension<?>>> extensionLoader) {
Supplier<List<JibGradlePluginExtension<?>>> extensionLoader,
String configurationName) {
this.project = project;
this.tempDirectoryProvider = tempDirectoryProvider;
this.extensionLoader = extensionLoader;
this.configurationName = configurationName;
ConsoleLoggerBuilder consoleLoggerBuilder =
(isProgressFooterEnabled(project)
? ConsoleLoggerBuilder.rich(singleThreadedExecutor, false)
Expand Down Expand Up @@ -193,7 +201,7 @@ public JibContainerBuilder createJibContainerBuilder(
project.files(
project
.getConfigurations()
.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)
.getByName(configurationName)
.getResolvedConfiguration()
.getResolvedArtifacts()
.stream()
Expand All @@ -219,7 +227,8 @@ public JibContainerBuilder createJibContainerBuilder(
FileCollection classesOutputDirectories =
mainSourceSet.getOutput().getClassesDirs().filter(File::exists);
Path resourcesOutputDirectory = mainSourceSet.getOutput().getResourcesDir().toPath();
FileCollection allFiles = mainSourceSet.getRuntimeClasspath().filter(File::exists);
FileCollection allFiles =
project.getConfigurations().getByName(configurationName).filter(File::exists);

FileCollection nonProjectDependencies =
allFiles
Expand Down Expand Up @@ -298,7 +307,7 @@ public List<Path> getClassFiles() throws IOException {
@Override
public List<Path> getDependencies() {
List<Path> dependencies = new ArrayList<>();
FileCollection runtimeClasspath = getMainSourceSet().getRuntimeClasspath();
FileCollection runtimeClasspath = project.getConfigurations().getByName(configurationName);
// To be on the safe side with the order, calling "forEach" first (no filtering operations).
runtimeClasspath.forEach(
file -> {
Expand Down Expand Up @@ -387,12 +396,10 @@ public boolean isWarProject() {
* @param extraDirectories the image's configured extra directories
* @return the input files
*/
static FileCollection getInputFiles(Project project, List<Path> extraDirectories) {
JavaPluginConvention javaPluginConvention =
project.getConvention().getPlugin(JavaPluginConvention.class);
SourceSet mainSourceSet = javaPluginConvention.getSourceSets().getByName(MAIN_SOURCE_SET_NAME);
static FileCollection getInputFiles(
Project project, List<Path> extraDirectories, String configurationName) {
List<FileCollection> dependencyFileCollections = new ArrayList<>();
dependencyFileCollections.add(mainSourceSet.getRuntimeClasspath());
dependencyFileCollections.add(project.getConfigurations().getByName(configurationName));

extraDirectories
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
Expand Down Expand Up @@ -94,6 +95,7 @@ public class JibExtension {
private final SkaffoldParameters skaffold;
private final Property<Boolean> allowInsecureRegistries;
private final Property<String> containerizingMode;
private final Property<String> configurationName;
private final ListProperty<ExtensionParameters> pluginExtensions;
private final ExtensionParametersSpec extensionParametersSpec;

Expand All @@ -118,6 +120,10 @@ public JibExtension(Project project) {
objectFactory.newInstance(ExtensionParametersSpec.class, pluginExtensions);
allowInsecureRegistries = objectFactory.property(Boolean.class);
containerizingMode = objectFactory.property(String.class);
configurationName =
objectFactory
.property(String.class)
.convention(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME);

// Sets defaults.
allowInsecureRegistries.set(DEFAULT_ALLOW_INSECURE_REGISTIRIES);
Expand Down Expand Up @@ -221,6 +227,17 @@ public String getContainerizingMode() {
return property != null ? property : containerizingMode.get();
}

@Input
@Optional
public Property<String> getConfigurationName() {
return configurationName;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

our of curiosity, can we just set the property to the system property value here?

public Property<String> getConfigurationName() {
  String property = System.getProperty(PropertyNames.CONFIGURATION_NAME);
  if (property != null) {
    configurationName.set(property);
  }
  return configurationName;
}

while this still looks a little odd, if it is possible, it matches more closely our previous patterns and it removes the ambiguous GetconfigurationName/readConfigurationName situation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've realized it via a default value now, that sounds like a good idea anyway because it gives the user all the control. So if you want the override behavior you can just set the value to System.getProperty("some.property", "default"). The downside is of course that this is different from the rest of the API, creating an inconsistency in some cases.

}

public String readConfigurationName() {
String property = System.getProperty(PropertyNames.CONFIGURATION_NAME);
return property != null ? property : configurationName.get();
}

@Nested
@Optional
public ListProperty<ExtensionParameters> getPluginExtensions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.gradle.api.artifacts.ProjectDependency;
import org.gradle.api.artifacts.PublishArtifact;
import org.gradle.api.initialization.Settings;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskAction;
Expand Down Expand Up @@ -109,8 +108,7 @@ public void listFiles() throws IOException {
}

// Add SNAPSHOT, non-project dependency jars
for (File file :
project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)) {
for (File file : project.getConfigurations().getByName(jibExtension.readConfigurationName())) {
if (!projectDependencyJars.contains(file) && file.toString().contains("SNAPSHOT")) {
skaffoldFilesOutput.addInput(file.toPath());
projectDependencyJars.add(file); // Add to set to avoid printing the same files twice
Expand Down Expand Up @@ -192,18 +190,20 @@ private void addProjectFiles(Project project) {
* @return the set of project dependencies
*/
private Set<ProjectDependency> findProjectDependencies(Project project) {
Preconditions.checkNotNull(jibExtension);

Set<ProjectDependency> projectDependencies = new HashSet<>();
Deque<Project> projects = new ArrayDeque<>();
projects.push(project);

String configurationName = jibExtension.readConfigurationName();

while (!projects.isEmpty()) {
Project currentProject = projects.pop();

// Search through all dependencies
Configuration runtimeClasspath =
currentProject
.getConfigurations()
.findByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME);
currentProject.getConfigurations().findByName(configurationName);
if (runtimeClasspath != null) {
for (Configuration configuration : runtimeClasspath.getHierarchy()) {
for (Dependency dependency : configuration.getDependencies()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public void listFilesAndTargets() {

try (TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider()) {
GradleProjectProperties projectProperties =
GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider);
GradleProjectProperties.getForProject(
getProject(),
getLogger(),
tempDirectoryProvider,
jibExtension.readConfigurationName());

GradleRawConfiguration configuration = new GradleRawConfiguration(jibExtension);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.configuration.ConsoleOutput;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.JavaPlugin;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Assert;
Expand Down Expand Up @@ -233,7 +234,11 @@ public void setUp() {

gradleProjectProperties =
new GradleProjectProperties(
mockProject, mockLogger, mockTempDirectoryProvider, () -> loadedExtensions);
mockProject,
mockLogger,
mockTempDirectoryProvider,
() -> loadedExtensions,
JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME);
}

@Test
Expand Down
Loading