Search in sources :

Example 6 with CuratedApplication

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

the class DevModeTask method main.

public static Closeable main(Path appRoot) throws Exception {
    try (ObjectInputStream in = new ObjectInputStream(Files.newInputStream(appRoot.resolve(LIB).resolve(DEPLOYMENT_LIB).resolve(JarResultBuildStep.APPMODEL_DAT)))) {
        Properties buildSystemProperties = new Properties();
        try (InputStream buildIn = Files.newInputStream(appRoot.resolve(QUARKUS).resolve(BUILD_SYSTEM_PROPERTIES))) {
            buildSystemProperties.load(buildIn);
        }
        final MutableJarApplicationModel appModel = (MutableJarApplicationModel) in.readObject();
        ApplicationModel existingModel = appModel.getAppModel(appRoot);
        DevModeContext context = createDevModeContext(appRoot, existingModel);
        CuratedApplication bootstrap = QuarkusBootstrap.builder().setAppArtifact(existingModel.getAppArtifact()).setExistingModel(existingModel).setIsolateDeployment(true).setMode(QuarkusBootstrap.Mode.REMOTE_DEV_SERVER).setBuildSystemProperties(buildSystemProperties).setBaseName(appModel.getBaseName()).setApplicationRoot(existingModel.getAppArtifact().getResolvedPaths().getSinglePath()).setTargetDirectory(appRoot.getParent()).setBaseClassLoader(DevModeTask.class.getClassLoader()).build().bootstrap();
        Map<String, Object> map = new HashMap<>();
        map.put(DevModeContext.class.getName(), context);
        map.put(IsolatedDevModeMain.APP_ROOT, appRoot);
        map.put(DevModeType.class.getName(), DevModeType.REMOTE_SERVER_SIDE);
        return (Closeable) bootstrap.runInAugmentClassLoader(IsolatedDevModeMain.class.getName(), map);
    }
}
Also used : HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) Closeable(java.io.Closeable) MutableJarApplicationModel(io.quarkus.bootstrap.model.MutableJarApplicationModel) ApplicationModel(io.quarkus.bootstrap.model.ApplicationModel) Properties(java.util.Properties) MutableJarApplicationModel(io.quarkus.bootstrap.model.MutableJarApplicationModel) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) DevModeContext(io.quarkus.deployment.dev.DevModeContext) DevModeType(io.quarkus.dev.spi.DevModeType) ObjectInputStream(java.io.ObjectInputStream)

Example 7 with CuratedApplication

use of io.quarkus.bootstrap.app.CuratedApplication 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 8 with CuratedApplication

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

the class GenerateCodeMojo method generateCode.

void generateCode(PathCollection sourceParents, Consumer<Path> sourceRegistrar, boolean test) throws MojoFailureException, MojoExecutionException {
    final LaunchMode launchMode = test ? LaunchMode.TEST : LaunchMode.valueOf(mode);
    if (getLog().isDebugEnabled()) {
        getLog().debug("Bootstrapping Quarkus application in mode " + launchMode);
    }
    ClassLoader originalTccl = Thread.currentThread().getContextClassLoader();
    CuratedApplication curatedApplication = null;
    try {
        curatedApplication = bootstrapApplication(launchMode);
        QuarkusClassLoader deploymentClassLoader = curatedApplication.createDeploymentClassLoader();
        Thread.currentThread().setContextClassLoader(deploymentClassLoader);
        final Class<?> codeGenerator = deploymentClassLoader.loadClass("io.quarkus.deployment.CodeGenerator");
        final Method initAndRun = codeGenerator.getMethod("initAndRun", ClassLoader.class, PathCollection.class, Path.class, Path.class, Consumer.class, ApplicationModel.class, Properties.class, String.class, boolean.class);
        initAndRun.invoke(null, deploymentClassLoader, sourceParents, generatedSourcesDir(test), buildDir().toPath(), sourceRegistrar, curatedApplication.getApplicationModel(), mavenProject().getProperties(), launchMode.name(), test);
    } catch (Exception any) {
        throw new MojoExecutionException("Quarkus code generation phase has failed", any);
    } finally {
        // in case of test mode, we can't share the bootstrapped app with the testing plugins, so we are closing it right away
        if (test && curatedApplication != null) {
            curatedApplication.close();
        }
        Thread.currentThread().setContextClassLoader(originalTccl);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) LaunchMode(io.quarkus.runtime.LaunchMode) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) Method(java.lang.reflect.Method) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 9 with CuratedApplication

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

the class QuarkusBuild method buildQuarkus.

@TaskAction
public void buildQuarkus() {
    final ApplicationModel appModel;
    try {
        appModel = extension().getAppModelResolver().resolveModel(new GACTV(getProject().getGroup().toString(), getProject().getName(), getProject().getVersion().toString()));
    } catch (AppModelResolverException e) {
        throw new GradleException("Failed to resolve Quarkus application model for " + getProject().getPath(), e);
    }
    final Properties effectiveProperties = getBuildSystemProperties(appModel.getAppArtifact());
    if (ignoredEntries != null && ignoredEntries.size() > 0) {
        String joinedEntries = String.join(",", ignoredEntries);
        effectiveProperties.setProperty("quarkus.package.user-configured-ignored-entries", joinedEntries);
    }
    exportCustomManifestProperties(effectiveProperties);
    try (CuratedApplication appCreationContext = QuarkusBootstrap.builder().setBaseClassLoader(getClass().getClassLoader()).setExistingModel(appModel).setTargetDirectory(getProject().getBuildDir().toPath()).setBaseName(extension().finalName()).setBuildSystemProperties(effectiveProperties).setAppArtifact(appModel.getAppArtifact()).setLocalProjectDiscovery(false).setIsolateDeployment(true).build().bootstrap()) {
        // Processes launched from within the build task of Gradle (daemon) lose content
        // generated on STDOUT/STDERR by the process (see https://github.com/gradle/gradle/issues/13522).
        // We overcome this by letting build steps know that the STDOUT/STDERR should be explicitly
        // streamed, if they need to make available that generated data.
        // The io.quarkus.deployment.pkg.builditem.ProcessInheritIODisabled$Factory
        // does the necessary work to generate such a build item which the build step(s) can rely on
        appCreationContext.createAugmentor("io.quarkus.deployment.pkg.builditem.ProcessInheritIODisabled$Factory", Collections.emptyMap()).createProductionApplication();
    } catch (BootstrapException e) {
        throw new GradleException("Failed to build a runnable JAR", e);
    }
}
Also used : CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) AppModelResolverException(io.quarkus.bootstrap.resolver.AppModelResolverException) GradleException(org.gradle.api.GradleException) BootstrapException(io.quarkus.bootstrap.BootstrapException) ApplicationModel(io.quarkus.bootstrap.model.ApplicationModel) GACTV(io.quarkus.maven.dependency.GACTV) Properties(java.util.Properties) TaskAction(org.gradle.api.tasks.TaskAction)

Example 10 with CuratedApplication

use of io.quarkus.bootstrap.app.CuratedApplication 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)

Aggregations

CuratedApplication (io.quarkus.bootstrap.app.CuratedApplication)19 Path (java.nio.file.Path)14 QuarkusBootstrap (io.quarkus.bootstrap.app.QuarkusBootstrap)11 IOException (java.io.IOException)9 AugmentAction (io.quarkus.bootstrap.app.AugmentAction)8 ApplicationModel (io.quarkus.bootstrap.model.ApplicationModel)8 ArrayList (java.util.ArrayList)8 Map (java.util.Map)7 HashMap (java.util.HashMap)6 AugmentResult (io.quarkus.bootstrap.app.AugmentResult)5 QuarkusClassLoader (io.quarkus.bootstrap.classloading.QuarkusClassLoader)5 LaunchMode (io.quarkus.runtime.LaunchMode)5 HashSet (java.util.HashSet)5 List (java.util.List)5 Properties (java.util.Properties)5 AdditionalDependency (io.quarkus.bootstrap.app.AdditionalDependency)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Closeable (java.io.Closeable)4 ObjectInputStream (java.io.ObjectInputStream)4 Files (java.nio.file.Files)4