Skip to content

Commit

Permalink
Engage logging InitialConfigurator only when running a Quarkus applic…
Browse files Browse the repository at this point in the history
…ation

This is done by generating the Service file during build time instead
of embedding it inside the quarkus-bootstrap-runner jar unconditionally

Fixes: quarkusio#14295
  • Loading branch information
geoand committed Jan 17, 2021
1 parent 4c323ae commit a32b917
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
import io.quarkus.builder.item.MultiBuildItem;

public final class GeneratedResourceBuildItem extends MultiBuildItem {
final String name;
final byte[] classData;
private final String name;
private final byte[] classData;
/**
* This setting is only relevant for the fast-jar package and determines whether the generated file should be included in
* the
* quarkus-run jar (if set to {@code true}) or the generated-bytecode jar (if set to {@code false}).
* This option is generally not needed except for services that need to be loadable by the (few) jars that are included
* in the quarkus-run (that is the bootstrap) jar
*/
private final boolean includeInBootstrap;

public GeneratedResourceBuildItem(String name, byte[] classData) {
this(name, classData, false);
}

public GeneratedResourceBuildItem(String name, byte[] classData, boolean includeInBootstrap) {
this.name = name;
this.classData = classData;
this.includeInBootstrap = includeInBootstrap;
}

public String getName() {
Expand All @@ -18,4 +31,8 @@ public String getName() {
public byte[] getClassData() {
return classData;
}

public boolean isIncludeInBootstrap() {
return includeInBootstrap;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.deployment.logging;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -21,6 +22,7 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.ConsoleFormatterBannerBuildItem;
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.deployment.builditem.GeneratedResourceBuildItem;
import io.quarkus.deployment.builditem.LogCategoryBuildItem;
import io.quarkus.deployment.builditem.LogConsoleFormatBuildItem;
import io.quarkus.deployment.builditem.LogHandlerBuildItem;
Expand Down Expand Up @@ -109,12 +111,15 @@ void setUpDefaultLogCleanupFilters(List<LogCleanupFilterBuildItem> logCleanupFil
void miscSetup(
Consumer<RuntimeInitializedClassBuildItem> runtimeInit,
Consumer<NativeImageSystemPropertyBuildItem> systemProp,
Consumer<ServiceProviderBuildItem> provider) {
Consumer<ServiceProviderBuildItem> provider,
Consumer<GeneratedResourceBuildItem> generatedResource) {
runtimeInit.accept(new RuntimeInitializedClassBuildItem("org.jboss.logmanager.formatters.TrueColorHolder"));
systemProp
.accept(new NativeImageSystemPropertyBuildItem("java.util.logging.manager", "org.jboss.logmanager.LogManager"));
provider.accept(
new ServiceProviderBuildItem(EmbeddedConfigurator.class.getName(), InitialConfigurator.class.getName()));
generatedResource.accept(new GeneratedResourceBuildItem("META-INF/services/" + EmbeddedConfigurator.class.getName(),
InitialConfigurator.class.getName().getBytes(StandardCharsets.UTF_8), true));
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.function.BiPredicate;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -509,14 +510,9 @@ private JarBuildItem buildThinJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
Files.write(target, i.getClassData());
}

for (GeneratedResourceBuildItem i : generatedResources) {
Path target = out.getPath(i.getName());
if (target.getParent() != null) {
Files.createDirectories(target.getParent());
}
Files.write(target, i.getClassData());
}
generateResources(out, generatedResources, g -> !g.isIncludeInBootstrap());
}

//now the application classes
Path runnerJar = appDir
.resolve(outputTargetBuildItem.getBaseName() + ".jar");
Expand Down Expand Up @@ -584,6 +580,7 @@ private JarBuildItem buildThinJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
generateManifest(runnerZipFs, classPath.toString(), packageConfig, appArtifact,
QuarkusEntryPoint.class.getName(),
applicationInfo);
generateResources(runnerZipFs, generatedResources, GeneratedResourceBuildItem::isIncludeInBootstrap);
}

//now copy the deployment artifacts, if required
Expand Down Expand Up @@ -652,6 +649,20 @@ public void accept(Path path) {
return new JarBuildItem(initJar, null, libDir, packageConfig.type, null);
}

private void generateResources(FileSystem fs, List<GeneratedResourceBuildItem> generatedResources,
Predicate<GeneratedResourceBuildItem> include) throws IOException {
for (GeneratedResourceBuildItem i : generatedResources) {
if (!include.test(i)) {
continue;
}
Path target = fs.getPath(i.getName());
if (target.getParent() != null) {
Files.createDirectories(target.getParent());
}
Files.write(target, i.getClassData());
}
}

private void copyDependency(CurateOutcomeBuildItem curateOutcomeBuildItem, Map<AppArtifactKey, List<Path>> runtimeArtifacts,
Path libDir, Path baseLib, List<Path> jars, boolean allowParentFirst, StringBuilder classPath, AppDependency appDep)
throws IOException {
Expand Down

This file was deleted.

0 comments on commit a32b917

Please sign in to comment.