Skip to content

Commit

Permalink
Add quarkus.native.resources.excludes quarkusio#13475
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga authored and cem.nura committed Jan 20, 2021
1 parent 6fd00ff commit ef84183
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@
*/
public final class NativeImageResourcePatternsBuildItem extends MultiBuildItem {

private final List<String> excludePatterns;

private final List<String> includePatterns;

private NativeImageResourcePatternsBuildItem(List<String> includePatterns) {
private NativeImageResourcePatternsBuildItem(List<String> includePatterns, List<String> excludePatterns) {
this.includePatterns = includePatterns;
this.excludePatterns = excludePatterns;
}

public List<String> getExcludePatterns() {
return excludePatterns;
}

public List<String> getIncludePatterns() {
Expand All @@ -45,12 +52,102 @@ public static Builder builder() {
}

public static class Builder {
private List<String> excludePatterns = new ArrayList<>();
private List<String> includePatterns = new ArrayList<>();

public NativeImageResourcePatternsBuildItem build() {
final List<String> incl = includePatterns;
includePatterns = null;
return new NativeImageResourcePatternsBuildItem(Collections.unmodifiableList(incl));
final List<String> excl = excludePatterns;
excludePatterns = null;
return new NativeImageResourcePatternsBuildItem(Collections.unmodifiableList(incl),
Collections.unmodifiableList(excl));
}

/**
* Add a glob pattern for matching resource paths that should <strong>not</strong> be added to the native image.
* <p>
* Use slash ({@code /}) as a path separator on all platforms. Globs must not start with slash. See
* {@link NativeConfig.ResourcesConfig#includes} for the supported glob syntax.
*
* @param glob the glob pattern to add to the list of patterns to exclude
* @return this {@link Builder}
*/
public Builder excludeGlob(String glob) {
excludePatterns.add(GlobUtil.toRegexPattern(glob));
return this;
}

/**
* Add a collection of glob patterns for matching resource paths that should <strong>not</strong> be added to the
* native image.
* <p>
* Use slash ({@code /}) as a path separator on all platforms. Globs must not start with slash. See
* {@link NativeConfig.ResourcesConfig#includes} for the supported glob syntax.
*
* @param globs the glob patterns to add to the list of patterns to exclude
* @return this {@link Builder}
*/
public Builder excludeGlobs(Collection<String> globs) {
globs.stream().map(GlobUtil::toRegexPattern).forEach(excludePatterns::add);
return this;
}

/**
* Add an array of glob patterns for matching resource paths that should <strong>not</strong> be added to the
* native image.
* <p>
* Use slash ({@code /}) as a path separator on all platforms. Globs must not start with slash. See
* {@link NativeConfig.ResourcesConfig#includes} for the supported glob syntax.
*
* @param globs the glob patterns to add to the list of patterns to exclude
* @return this {@link Builder}
*/
public Builder excludeGlobs(String... globs) {
Stream.of(globs).map(GlobUtil::toRegexPattern).forEach(excludePatterns::add);
return this;
}

/**
* Add a regular expression for matching resource paths that should <strong>not</strong> be added to the native
* image.
* <p>
* Use slash ({@code /}) as a path separator on all platforms. The pattern must not start with slash.
*
* @param pattern the regular expression to add to the list of patterns to exclude
* @return this {@link Builder}
*/
public Builder excludePattern(String pattern) {
excludePatterns.add(pattern);
return this;
}

/**
* Add a collection of regular expressions for matching resource paths that should <strong>not</strong> be added
* to the native image.
* <p>
* Use slash ({@code /}) as a path separator on all platforms. The pattern must not start with slash.
*
* @param patterns the regular expressions to add to the list of patterns to exclude
* @return this {@link Builder}
*/
public Builder excludePatterns(Collection<String> patterns) {
excludePatterns.addAll(patterns);
return this;
}

/**
* Add an array of regular expressions for matching resource paths that should <strong>not</strong> be added
* to the native image.
* <p>
* Use slash ({@code /}) as a path separator on all platforms. The pattern must not start with slash.
*
* @param patterns the regular expressions to add to the list of patterns to exclude
* @return this {@link Builder}
*/
public Builder excludePatterns(String... patterns) {
Stream.of(patterns).forEach(excludePatterns::add);
return this;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,29 @@ public static class ResourcesConfig {
@ConfigItem
public Optional<List<String>> includes;

/**
* A comma separated list of globs to match resource paths that should <b>not</b> be added to the native image.
* <p>
* Use slash ({@code /}) as a path separator on all platforms. Globs must not start with slash.
* <p>
* Please refer to {@link #includes} for details about the glob syntax.
* <p>
* By default, no resources are excluded.
* <p>
* Example: Given that you have {@code src/main/resources/red.png}
* and {@code src/main/resources/foo/green.png} in your source tree and one of your dependency JARs contains
* {@code bar/blue.png} file, with the following configuration
*
* <pre>
* quarkus.native.resources.includes = **&#47;*.png
* quarkus.native.resources.excludes = foo/**,**&#47;green.png
* </pre>
*
* the resource {@code red.png} will be available in the native image while the resources {@code foo/green.png}
* and {@code bar/blue.png} will not be available in the native image.
*/
@ConfigItem
public Optional<List<String>> excludes;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ public void write(String s, byte[] bytes) {
overallCatch.loadClass("com.oracle.svm.core.configure.ResourcesRegistry"));
TryBlock tc = overallCatch.tryBlock();
for (NativeImageResourcePatternsBuildItem resourcePatternsItem : resourcePatterns) {
for (String pattern : resourcePatternsItem.getExcludePatterns()) {
tc.invokeInterfaceMethod(RESOURCES_REGISTRY_IGNORE_RESOURCES, resourcesRegistrySingleton,
overallCatch.load(pattern));
}
for (String pattern : resourcePatternsItem.getIncludePatterns()) {
tc.invokeInterfaceMethod(
RESOURCES_REGISTRY_ADD_RESOURCES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ void forwardResourcePatternConfigToBuildItem(
BuildProducer<NativeImageResourcePatternsBuildItem> nativeImageResourcePatterns) {

final Optional<List<String>> includes = nativeConfig.resources.includes;
if (includes.isPresent()) {
final Optional<List<String>> excludes = nativeConfig.resources.excludes;
if (includes.isPresent() || excludes.isPresent()) {
final Builder builder = NativeImageResourcePatternsBuildItem.builder();
includes.ifPresent(builder::includeGlobs);
excludes.ifPresent(builder::excludeGlobs);
nativeImageResourcePatterns.produce(builder.build());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ configproperties.number=ONE
quarkus.swagger-ui.always-include=true

quarkus.native.resources.includes = test-resources/**.txt
quarkus.native.resources.excludes = **/unwanted.*

quarkus.log.metrics.enabled=true

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
An unwanted file
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public void excludedNative() {
.get("/resources/test-resources/file.adoc")
.then()
.statusCode(404);

RestAssured.when()
.get("/resources/test-resources/excluded/unwanted.txt")
.then()
.statusCode(404);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@ public void excludedJvm() {
.then()
.statusCode(200)
.body(is("= An AsciiDoc File"));

RestAssured.when()
.get("/resources/test-resources/excluded/unwanted.txt")
.then()
.statusCode(200)
.body(is("An unwanted file"));
}
}

0 comments on commit ef84183

Please sign in to comment.