Skip to content

Commit

Permalink
Write metadata file containing the information about the last built a…
Browse files Browse the repository at this point in the history
…rtifact
  • Loading branch information
geoand committed Feb 16, 2021
1 parent 90b8011 commit 4c41be1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.quarkus.runner.bootstrap;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -47,6 +50,7 @@
import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
import io.quarkus.deployment.builditem.TransformedClassesBuildItem;
import io.quarkus.deployment.pkg.builditem.ArtifactResultBuildItem;
import io.quarkus.deployment.pkg.builditem.BuildSystemTargetBuildItem;
import io.quarkus.deployment.pkg.builditem.JarBuildItem;
import io.quarkus.deployment.pkg.builditem.NativeImageBuildItem;
import io.quarkus.dev.spi.DevModeType;
Expand Down Expand Up @@ -165,13 +169,44 @@ public AugmentResult createProductionApplication() {

JarBuildItem jarBuildItem = result.consumeOptional(JarBuildItem.class);
NativeImageBuildItem nativeImageBuildItem = result.consumeOptional(NativeImageBuildItem.class);
return new AugmentResult(result.consumeMulti(ArtifactResultBuildItem.class).stream()
List<ArtifactResultBuildItem> artifactResultBuildItems = result.consumeMulti(ArtifactResultBuildItem.class);
BuildSystemTargetBuildItem buildSystemTargetBuildItem = result.consume(BuildSystemTargetBuildItem.class);

// this depends on the fact that the order in which we can obtain MultiBuildItems is the same as they are produced
// we want to write result of the final artifact created
ArtifactResultBuildItem lastResult = artifactResultBuildItems.get(artifactResultBuildItems.size() - 1);
writeArtifactResultMetadataFile(buildSystemTargetBuildItem, lastResult);

return new AugmentResult(artifactResultBuildItems.stream()
.map(a -> new ArtifactResult(a.getPath(), a.getType(), a.getMetadata()))
.collect(Collectors.toList()),
jarBuildItem != null ? jarBuildItem.toJarResult() : null,
nativeImageBuildItem != null ? nativeImageBuildItem.getPath() : null);
}

private void writeArtifactResultMetadataFile(BuildSystemTargetBuildItem outputTargetBuildItem,
ArtifactResultBuildItem lastResult) {
Path quarkusArtifactMetadataPath = outputTargetBuildItem.getOutputDirectory().resolve("quarkus-artifact.properties");
Properties properties = new Properties();
properties.put("type", lastResult.getType());
if (lastResult.getPath() != null) {
properties.put("path", outputTargetBuildItem.getOutputDirectory().relativize(lastResult.getPath()).toString());
}
Map<String, Object> metadata = lastResult.getMetadata();
if (metadata != null) {
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
if (entry.getValue() instanceof String) {
properties.put("metadata." + entry.getKey(), entry.getValue());
}
}
}
try (FileOutputStream fos = new FileOutputStream(quarkusArtifactMetadataPath.toFile())) {
properties.store(fos, "Generated by Quarkus - Do not edit manually");
} catch (IOException e) {
log.debug("Unable to write artifact result metadata file", e);
}
}

@Override
public StartupActionImpl createInitialRuntimeApplication() {
if (launchMode == LaunchMode.NORMAL) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import io.quarkus.deployment.util.ExecUtil

import java.nio.file.Paths

try {
ExecUtil.exec("docker", "version", "--format", "'{{.Server.Version}}'")
} catch (Exception ignored) {
Expand All @@ -10,3 +12,17 @@ try {
String group = System.getProperty("user.name")
assert ExecUtil.exec("docker", "images", group + "/container-build-jib")
assert ExecUtil.exec("docker", "rmi", group + "/container-build-jib:0.1-SNAPSHOT")

File propertiesFile =
System.getProperty("user.dir").endsWith("maven-invoker-way")
? Paths.get("target", "it", "container-build-jib", "target", "quarkus-artifact.properties").toFile()
: Paths.get("integration-tests", "container-image", "maven-invoker-way", "target", "it", "container-build-jib", "target", "quarkus-artifact.properties").toFile()
Properties properties = new Properties()
propertiesFile.withInputStream {
properties.load(it)
}



assert properties.type == 'jar-container'
assert properties."metadata.container-image" == group + "/container-build-jib:0.1-SNAPSHOT"
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -21,6 +22,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -212,9 +214,21 @@ public void testThatLegacyJarFormatWorks() throws Exception {
.resolve(Paths.get("target/acme-1.0-SNAPSHOT-runner.jar"));
Assertions.assertTrue(Files.exists(jar));

Properties quarkusArtifactProperties = new Properties();
quarkusArtifactProperties
.load(new FileInputStream(testDir.toPath().resolve("target").resolve("quarkus-artifact.properties").toFile()));
Assertions.assertEquals("jar", quarkusArtifactProperties.get("type"));
Assertions.assertEquals("acme-1.0-SNAPSHOT-runner.jar", quarkusArtifactProperties.get("path"));

File output = new File(testDir, "target/output.log");
output.createNewFile();

Properties properties = new Properties();
properties
.load(new FileInputStream(testDir.toPath().resolve("target").resolve("quarkus-artifact.properties").toFile()));
Assertions.assertEquals("jar", properties.get("type"));
Assertions.assertEquals("acme-1.0-SNAPSHOT-runner.jar", properties.get("path"));

Process process = doLaunch(jar, output).start();
try {
// Wait until server up
Expand Down Expand Up @@ -550,6 +564,13 @@ private void assertThatFastJarFormatWorks(String outputDir) throws Exception {
File output = new File(testDir, "target/output.log");
output.createNewFile();

Properties properties = new Properties();
properties
.load(new FileInputStream(testDir.toPath().resolve("target").resolve("quarkus-artifact.properties").toFile()));
Assertions.assertEquals("jar", properties.get("type"));
Assertions.assertTrue(properties.get("path").toString().startsWith(outputDir == null ? "quarkus-app" : outputDir));
Assertions.assertTrue(properties.get("path").toString().endsWith("quarkus-run.jar"));

Process process = doLaunch(jar, output).start();
try {
// Wait until server up
Expand Down

0 comments on commit 4c41be1

Please sign in to comment.