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 11 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,11 @@ public void buildDocker()
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();

GradleProjectProperties projectProperties =
GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider);
GradleProjectProperties.getForProject(
getProject(),
getLogger(),
tempDirectoryProvider,
jibExtension.getConfigurationName().get());
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,11 @@ public void buildImage()
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();

GradleProjectProperties projectProperties =
GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider);
GradleProjectProperties.getForProject(
getProject(),
getLogger(),
tempDirectoryProvider,
jibExtension.getConfigurationName().get());
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.getConfigurationName().get());
}

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

GradleProjectProperties projectProperties =
GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider);
GradleProjectProperties.getForProject(
getProject(),
getLogger(),
tempDirectoryProvider,
jibExtension.getConfigurationName().get());
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,12 @@ 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)
.getAsFileTree()
.filter(File::exists);

FileCollection nonProjectDependencies =
allFiles
Expand Down Expand Up @@ -298,16 +311,18 @@ 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 -> {
if (file.exists()
&& file.isFile()
&& file.getName().toLowerCase(Locale.US).endsWith(".jar")) {
dependencies.add(file.toPath());
}
});
runtimeClasspath
.getAsFileTree()
.forEach(
file -> {
if (file.exists()
&& file.isFile()
&& file.getName().toLowerCase(Locale.US).endsWith(".jar")) {
dependencies.add(file.toPath());
}
});
return dependencies;
}

Expand Down Expand Up @@ -387,12 +402,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,15 @@ 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(
project.provider(
() ->
System.getProperty(
PropertyNames.CONFIGURATION_NAME,
JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)));

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

/**
* Returns the configurationName property while setting it to the value of the system property if
* present.
*
* @return The configurationName property
*/
@Input
@Optional
public Property<String> getConfigurationName() {
String property = System.getProperty(PropertyNames.CONFIGURATION_NAME);
if (property != null) {
Copy link
Member

Choose a reason for hiding this comment

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

FTR, this can be problematic and should be fixed. #3034 (comment)

configurationName.set(property);
}
return configurationName;
}

@Nested
@Optional
public ListProperty<ExtensionParameters> getPluginExtensions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ public void apply(Project project) {
.getSourceSets()
.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
jibDependencies.add(mainSourceSet.getRuntimeClasspath());
jibDependencies.add(
projectAfterEvaluation
.getConfigurations()
.getByName(jibExtension.getConfigurationName().get()));

Set<TaskProvider<?>> jibTaskProviders =
ImmutableSet.of(buildImageTask, buildDockerTask, buildTarTask, syncMapTask);
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 @@ -110,7 +109,7 @@ public void listFiles() throws IOException {

// Add SNAPSHOT, non-project dependency jars
for (File file :
project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)) {
project.getConfigurations().getByName(jibExtension.getConfigurationName().get())) {
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 +191,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.getConfigurationName().get();

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.getConfigurationName().get());

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