Search in sources :

Example 1 with AugmentResult

use of io.quarkus.bootstrap.app.AugmentResult in project quarkus by quarkusio.

the class IsolatedRemoteDevModeMain method generateApplication.

private synchronized JarResult generateApplication() {
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    try {
        // ok, we have resolved all the deps
        try {
            AugmentResult start = augmentAction.createProductionApplication();
            if (!start.getJar().getType().equalsIgnoreCase(PackageConfig.MUTABLE_JAR)) {
                throw new RuntimeException("remote-dev can only be used with mutable applications generated with the fast-jar format");
            }
            // now extract the artifacts, to mirror the remote side
            DevModeTask.extractDevModeClasses(start.getJar().getPath().getParent(), curatedApplication.getApplicationModel(), null);
            return start.getJar();
        } catch (Throwable t) {
            deploymentProblem = t;
            log.error("Failed to generate Quarkus application", t);
            return null;
        }
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}
Also used : AugmentResult(io.quarkus.bootstrap.app.AugmentResult)

Example 2 with AugmentResult

use of io.quarkus.bootstrap.app.AugmentResult in project quarkus by quarkusio.

the class AugmentActionImpl method createProductionApplication.

@Override
public AugmentResult createProductionApplication() {
    if (launchMode != LaunchMode.NORMAL) {
        throw new IllegalStateException("Can only create a production application when using NORMAL launch mode");
    }
    ClassLoader classLoader = curatedApplication.createDeploymentClassLoader();
    BuildResult result = runAugment(true, Collections.emptySet(), null, classLoader, ArtifactResultBuildItem.class, DeploymentResultBuildItem.class);
    writeDebugSourceFile(result);
    JarBuildItem jarBuildItem = result.consumeOptional(JarBuildItem.class);
    NativeImageBuildItem nativeImageBuildItem = result.consumeOptional(NativeImageBuildItem.class);
    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);
}
Also used : BuildResult(io.quarkus.builder.BuildResult) JarBuildItem(io.quarkus.deployment.pkg.builditem.JarBuildItem) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) ArtifactResultBuildItem(io.quarkus.deployment.pkg.builditem.ArtifactResultBuildItem) BuildSystemTargetBuildItem(io.quarkus.deployment.pkg.builditem.BuildSystemTargetBuildItem) AugmentResult(io.quarkus.bootstrap.app.AugmentResult) NativeImageBuildItem(io.quarkus.deployment.pkg.builditem.NativeImageBuildItem) ArtifactResult(io.quarkus.bootstrap.app.ArtifactResult)

Example 3 with AugmentResult

use of io.quarkus.bootstrap.app.AugmentResult in project quarkus by quarkusio.

the class PackageAppTestBase method testBootstrap.

@Override
protected void testBootstrap(QuarkusBootstrap creator) throws Exception {
    System.setProperty("quarkus.package.type", "legacy-jar");
    try {
        CuratedApplication curated = creator.bootstrap();
        assertAppModel(curated.getApplicationModel());
        final String[] expectedExtensions = expectedExtensionDependencies();
        if (expectedExtensions != null) {
            assertExtensionDependencies(curated.getApplicationModel(), expectedExtensions);
        }
        assertDeploymentDeps(curated.getApplicationModel().getDependencies().stream().filter(d -> d.isDeploymentCp() && !d.isRuntimeCp()).map(d -> new ArtifactDependency(d)).collect(Collectors.toSet()));
        AugmentAction action = curated.createAugmentor();
        AugmentResult outcome = action.createProductionApplication();
        final Path libDir = outcome.getJar().getLibraryDir();
        assertTrue(Files.isDirectory(libDir));
        final Set<String> actualLib = new HashSet<>();
        try (Stream<Path> stream = Files.list(libDir)) {
            final Iterator<Path> i = stream.iterator();
            while (i.hasNext()) {
                actualLib.add(i.next().getFileName().toString());
            }
        }
        final Path runnerJar = outcome.getJar().getPath();
        assertTrue(Files.exists(runnerJar));
        try (JarFile jar = new JarFile(runnerJar.toFile())) {
            final Attributes mainAttrs = jar.getManifest().getMainAttributes();
            // assert the main class
            assertEquals(MAIN_CLS, mainAttrs.getValue("Main-Class"));
            // assert the Class-Path contains all the entries in the lib dir
            final String cp = mainAttrs.getValue("Class-Path");
            assertNotNull(cp);
            String[] cpEntries = Arrays.stream(cp.trim().split("\\s+")).filter(s -> !s.trim().isEmpty()).toArray(String[]::new);
            assertEquals(actualLib.size(), cpEntries.length);
            for (String entry : cpEntries) {
                assertTrue(entry.startsWith(LIB_PREFIX));
                assertTrue(actualLib.contains(entry.substring(LIB_PREFIX.length())));
            }
        }
        List<String> missingEntries = Collections.emptyList();
        for (String entry : expectedLib) {
            if (!actualLib.remove(entry)) {
                if (missingEntries.isEmpty()) {
                    missingEntries = new ArrayList<>();
                }
                missingEntries.add(entry);
            }
        }
        StringBuilder buf = null;
        if (!missingEntries.isEmpty()) {
            buf = new StringBuilder();
            buf.append("Missing entries: ").append(missingEntries.get(0));
            for (int i = 1; i < missingEntries.size(); ++i) {
                buf.append(", ").append(missingEntries.get(i));
            }
        }
        if (!actualLib.isEmpty()) {
            if (buf == null) {
                buf = new StringBuilder();
            } else {
                buf.append("; ");
            }
            final Iterator<String> i = actualLib.iterator();
            buf.append("Extra entries: ").append(i.next());
            while (i.hasNext()) {
                buf.append(", ").append(i.next());
            }
        }
        if (buf != null) {
            fail(buf.toString());
        }
    } finally {
        System.clearProperty("quarkus.package.type");
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) Arrays(java.util.Arrays) TsDependency(io.quarkus.bootstrap.resolver.TsDependency) JarFile(java.util.jar.JarFile) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ArtifactDependency(io.quarkus.maven.dependency.ArtifactDependency) AugmentAction(io.quarkus.bootstrap.app.AugmentAction) Dependency(io.quarkus.maven.dependency.Dependency) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) AugmentResult(io.quarkus.bootstrap.app.AugmentResult) Properties(java.util.Properties) Iterator(java.util.Iterator) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) Collection(java.util.Collection) Set(java.util.Set) BootstrapConstants(io.quarkus.bootstrap.BootstrapConstants) IOException(java.io.IOException) ContentProvider(io.quarkus.bootstrap.resolver.TsArtifact.ContentProvider) TsArtifact(io.quarkus.bootstrap.resolver.TsArtifact) BootstrapTestBase(io.quarkus.bootstrap.resolver.BootstrapTestBase) Attributes(java.util.jar.Attributes) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) QuarkusBootstrap(io.quarkus.bootstrap.app.QuarkusBootstrap) ApplicationModel(io.quarkus.bootstrap.model.ApplicationModel) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ResolvedDependency(io.quarkus.maven.dependency.ResolvedDependency) Collections(java.util.Collections) Path(java.nio.file.Path) AugmentAction(io.quarkus.bootstrap.app.AugmentAction) ArtifactDependency(io.quarkus.maven.dependency.ArtifactDependency) Attributes(java.util.jar.Attributes) AugmentResult(io.quarkus.bootstrap.app.AugmentResult) JarFile(java.util.jar.JarFile) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) HashSet(java.util.HashSet)

Example 4 with AugmentResult

use of io.quarkus.bootstrap.app.AugmentResult in project quarkus by quarkusio.

the class RebuildHandler method handlePostAsync.

@Override
protected void handlePostAsync(RoutingContext event, MultiMap form) throws Exception {
    QuarkusBootstrap existing = (QuarkusBootstrap) DevConsoleManager.getQuarkusBootstrap();
    try (TempSystemProperties properties = new TempSystemProperties()) {
        for (Map.Entry<String, String> i : config.entrySet()) {
            properties.set(i.getKey(), i.getValue());
        }
        for (Map.Entry<String, String> i : form.entries()) {
            if (!i.getValue().isEmpty()) {
                properties.set(i.getKey(), i.getValue());
            }
        }
        QuarkusBootstrap quarkusBootstrap = existing.clonedBuilder().setMode(QuarkusBootstrap.Mode.PROD).setIsolateDeployment(true).build();
        try (CuratedApplication bootstrap = quarkusBootstrap.bootstrap()) {
            AugmentResult augmentResult = bootstrap.createAugmentor().createProductionApplication();
            List<ArtifactResult> containerArtifactResults = augmentResult.resultsMatchingType((s) -> s.contains("container"));
            if (containerArtifactResults.size() >= 1) {
                flashMessage(event, "Container-image: " + containerArtifactResults.get(0).getMetadata().get("container-image") + " created.", Duration.ofSeconds(10));
            }
        }
    }
    ;
}
Also used : TempSystemProperties(io.quarkus.dev.console.TempSystemProperties) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) QuarkusBootstrap(io.quarkus.bootstrap.app.QuarkusBootstrap) AugmentResult(io.quarkus.bootstrap.app.AugmentResult) Map(java.util.Map) MultiMap(io.vertx.core.MultiMap) ArtifactResult(io.quarkus.bootstrap.app.ArtifactResult)

Example 5 with AugmentResult

use of io.quarkus.bootstrap.app.AugmentResult in project quarkus-test-framework by quarkus-qe.

the class ProdQuarkusApplicationManagedResourceBuilder method buildArtifact.

private Path buildArtifact() {
    try {
        createSnapshotOfBuildProperties();
        Path appFolder = getApplicationFolder();
        JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class).addClasses(getAppClasses());
        javaArchive.as(ExplodedExporter.class).exportExplodedInto(appFolder.toFile());
        Path testLocation = PathTestHelper.getTestClassesLocation(getContext().getTestContext().getRequiredTestClass());
        QuarkusBootstrap.Builder builder = QuarkusBootstrap.builder().setApplicationRoot(appFolder).setMode(QuarkusBootstrap.Mode.PROD).addExcludedPath(testLocation).setIsolateDeployment(true).setProjectRoot(testLocation).setBaseName(getContext().getName()).setTargetDirectory(appFolder);
        if (!getForcedDependencies().isEmpty()) {
            // The method setForcedDependencies signature changed from `List<AppDependency>` to `List<Dependency>` where
            // Dependency is an interface of AppDependency, so it should be fine. However, the compiler fails to cast it,
            // so we need to use reflection to sort it out for the most recent version and older versions.
            ReflectionUtils.invokeMethod(builder, "setForcedDependencies", getForcedDependencies());
        }
        // The method `setLocalProjectDiscovery` signature changed from `Boolean` to `boolean` and this might make
        // to fail the tests at runtime when using different versions.
        // In order to workaround this, we need to invoke this method at runtime to let JVM unbox the arguments properly.
        // Note that this is happening because we want to support both 2.x and 1.13.x Quarkus versions.
        // Another strategy could be to have our own version of Quarkus bootstrap.
        ReflectionUtils.invokeMethod(builder, "setLocalProjectDiscovery", true);
        AugmentResult result;
        try (CuratedApplication curatedApplication = builder.build().bootstrap()) {
            AugmentAction action = curatedApplication.createAugmentor();
            result = action.createProductionApplication();
        }
        return Optional.ofNullable(result.getNativeResult()).orElseGet(() -> result.getJar().getPath());
    } catch (Exception ex) {
        fail("Failed to build Quarkus artifacts. Caused by " + ex);
    }
    return null;
}
Also used : Path(java.nio.file.Path) AugmentAction(io.quarkus.bootstrap.app.AugmentAction) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) QuarkusBootstrap(io.quarkus.bootstrap.app.QuarkusBootstrap) AugmentResult(io.quarkus.bootstrap.app.AugmentResult) ExplodedExporter(org.jboss.shrinkwrap.api.exporter.ExplodedExporter) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive)

Aggregations

AugmentResult (io.quarkus.bootstrap.app.AugmentResult)8 AugmentAction (io.quarkus.bootstrap.app.AugmentAction)5 CuratedApplication (io.quarkus.bootstrap.app.CuratedApplication)5 Path (java.nio.file.Path)5 QuarkusBootstrap (io.quarkus.bootstrap.app.QuarkusBootstrap)4 IOException (java.io.IOException)4 UncheckedIOException (java.io.UncheckedIOException)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 ArtifactResult (io.quarkus.bootstrap.app.ArtifactResult)2 Artifact (org.apache.maven.artifact.Artifact)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 ExplodedExporter (org.jboss.shrinkwrap.api.exporter.ExplodedExporter)2 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)2 BootstrapConstants (io.quarkus.bootstrap.BootstrapConstants)1 QuarkusClassLoader (io.quarkus.bootstrap.classloading.QuarkusClassLoader)1 ApplicationModel (io.quarkus.bootstrap.model.ApplicationModel)1 BootstrapTestBase (io.quarkus.bootstrap.resolver.BootstrapTestBase)1 TsArtifact (io.quarkus.bootstrap.resolver.TsArtifact)1 ContentProvider (io.quarkus.bootstrap.resolver.TsArtifact.ContentProvider)1