Skip to content

DependencyCustomizer ifAllResourcesPresent adds modules if any resources are present #20418

@wycm

Description

@wycm

I found that the logic of the
ifAllResourcesPresent
and
ifAnyResourcesPresent
methods is exactly the same
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/DependencyCustomizer.java#L131

`

/**
 * Create a nested {@link DependencyCustomizer} that only applies if the specified
 * paths are on the class path.
 * @param paths the paths to test
 * @return a nested {@link DependencyCustomizer}
 */
public DependencyCustomizer ifAllResourcesPresent(String... paths) {
	return new DependencyCustomizer(this) {
		@Override
		protected boolean canAdd() {
			for (String path : paths) {
				try {
					return DependencyCustomizer.this.loader.getResource(path) != null;
				}
				catch (Exception ex) {
					// swallow exception and continue
				}
			}
			return DependencyCustomizer.this.canAdd();
		}
	};
}

/**
 * Create a nested {@link DependencyCustomizer} that only applies at least one of the
 * specified paths is on the class path.
 * @param paths the paths to test
 * @return a nested {@link DependencyCustomizer}
 */
public DependencyCustomizer ifAnyResourcesPresent(String... paths) {
	return new DependencyCustomizer(this) {
		@Override
		protected boolean canAdd() {
			for (String path : paths) {
				try {
					return DependencyCustomizer.this.loader.getResource(path) != null;
				}
				catch (Exception ex) {
					// swallow exception and continue
				}
			}
			return DependencyCustomizer.this.canAdd();
		}
	};
}

`

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions