Search in sources :

Example 1 with QuarkusBootstrap

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

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

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

the class JBangAugmentorImpl method accept.

@Override
public void accept(CuratedApplication curatedApplication, Map<String, Object> resultMap) {
    QuarkusClassLoader classLoader = curatedApplication.getAugmentClassLoader();
    QuarkusBootstrap quarkusBootstrap = curatedApplication.getQuarkusBootstrap();
    QuarkusAugmentor.Builder builder = QuarkusAugmentor.builder().setRoot(quarkusBootstrap.getApplicationRoot()).setClassLoader(classLoader).addFinal(ApplicationClassNameBuildItem.class).setTargetDir(quarkusBootstrap.getTargetDirectory()).setDeploymentClassLoader(curatedApplication.createDeploymentClassLoader()).setBuildSystemProperties(quarkusBootstrap.getBuildSystemProperties()).setEffectiveModel(curatedApplication.getApplicationModel());
    if (quarkusBootstrap.getBaseName() != null) {
        builder.setBaseName(quarkusBootstrap.getBaseName());
    }
    boolean auxiliaryApplication = curatedApplication.getQuarkusBootstrap().isAuxiliaryApplication();
    builder.setAuxiliaryApplication(auxiliaryApplication);
    builder.setAuxiliaryDevModeType(curatedApplication.getQuarkusBootstrap().isHostApplicationIsTestOnly() ? DevModeType.TEST_ONLY : (auxiliaryApplication ? DevModeType.LOCAL : null));
    builder.setLaunchMode(LaunchMode.NORMAL);
    builder.setRebuild(quarkusBootstrap.isRebuild());
    builder.setLiveReloadState(new LiveReloadBuildItem(false, Collections.emptySet(), new HashMap<>(), null));
    for (AdditionalDependency i : quarkusBootstrap.getAdditionalApplicationArchives()) {
        // if it is forced as an app archive
        if (i.isForceApplicationArchive()) {
            builder.addAdditionalApplicationArchive(i.getResolvedPaths());
        }
    }
    builder.addBuildChainCustomizer(new Consumer<BuildChainBuilder>() {

        @Override
        public void accept(BuildChainBuilder builder) {
            final BuildStepBuilder stepBuilder = builder.addBuildStep((ctx) -> {
                ctx.produce(new ProcessInheritIODisabledBuildItem());
            });
            stepBuilder.produces(ProcessInheritIODisabledBuildItem.class).build();
        }
    });
    builder.excludeFromIndexing(quarkusBootstrap.getExcludeFromClassPath());
    builder.addFinal(GeneratedClassBuildItem.class);
    builder.addFinal(MainClassBuildItem.class);
    builder.addFinal(GeneratedResourceBuildItem.class);
    builder.addFinal(TransformedClassesBuildItem.class);
    builder.addFinal(DeploymentResultBuildItem.class);
    boolean nativeRequested = "native".equals(System.getProperty("quarkus.package.type"));
    boolean containerBuildRequested = Boolean.getBoolean("quarkus.container-image.build");
    if (nativeRequested) {
        builder.addFinal(NativeImageBuildItem.class);
    }
    if (containerBuildRequested) {
        // TODO: this is a bit ugly
        // we don't nessesarily need these artifacts
        // but if we include them it does mean that you can auto create docker images
        // and deploy to kube etc
        // for an ordinary build with no native and no docker this is a waste
        builder.addFinal(ArtifactResultBuildItem.class);
    }
    try {
        BuildResult buildResult = builder.build().run();
        Map<String, byte[]> result = new HashMap<>();
        for (GeneratedClassBuildItem i : buildResult.consumeMulti(GeneratedClassBuildItem.class)) {
            result.put(i.getName().replace(".", "/") + ".class", i.getClassData());
        }
        for (GeneratedResourceBuildItem i : buildResult.consumeMulti(GeneratedResourceBuildItem.class)) {
            result.put(i.getName(), i.getClassData());
        }
        for (Map.Entry<Path, Set<TransformedClassesBuildItem.TransformedClass>> entry : buildResult.consume(TransformedClassesBuildItem.class).getTransformedClassesByJar().entrySet()) {
            for (TransformedClassesBuildItem.TransformedClass transformed : entry.getValue()) {
                if (transformed.getData() != null) {
                    result.put(transformed.getFileName(), transformed.getData());
                } else {
                    log.warn("Unable to remove resource " + transformed.getFileName() + " as this is not supported in JBangf");
                }
            }
        }
        resultMap.put("files", result);
        final List<String> javaargs = new ArrayList<>();
        javaargs.add("-Djava.util.logging.manager=org.jboss.logmanager.LogManager");
        javaargs.add("-Djava.util.concurrent.ForkJoinPool.common.threadFactory=io.quarkus.bootstrap.forkjoin.QuarkusForkJoinWorkerThreadFactory");
        resultMap.put("java-args", javaargs);
        resultMap.put("main-class", buildResult.consume(MainClassBuildItem.class).getClassName());
        if (nativeRequested) {
            resultMap.put("native-image", buildResult.consume(NativeImageBuildItem.class).getPath());
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : TransformedClassesBuildItem(io.quarkus.deployment.builditem.TransformedClassesBuildItem) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) BuildResult(io.quarkus.builder.BuildResult) DevModeType(io.quarkus.dev.spi.DevModeType) Logger(org.jboss.logging.Logger) BuildStepBuilder(io.quarkus.builder.BuildStepBuilder) LaunchMode(io.quarkus.runtime.LaunchMode) HashMap(java.util.HashMap) AdditionalDependency(io.quarkus.bootstrap.app.AdditionalDependency) ArrayList(java.util.ArrayList) QuarkusAugmentor(io.quarkus.deployment.QuarkusAugmentor) BuildChainBuilder(io.quarkus.builder.BuildChainBuilder) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Path(java.nio.file.Path) DeploymentResultBuildItem(io.quarkus.deployment.pkg.builditem.DeploymentResultBuildItem) GeneratedResourceBuildItem(io.quarkus.deployment.builditem.GeneratedResourceBuildItem) ProcessInheritIODisabledBuildItem(io.quarkus.deployment.pkg.builditem.ProcessInheritIODisabledBuildItem) NativeImageBuildItem(io.quarkus.deployment.pkg.builditem.NativeImageBuildItem) ApplicationClassNameBuildItem(io.quarkus.deployment.builditem.ApplicationClassNameBuildItem) Set(java.util.Set) ArtifactResultBuildItem(io.quarkus.deployment.pkg.builditem.ArtifactResultBuildItem) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) Consumer(java.util.function.Consumer) List(java.util.List) QuarkusBootstrap(io.quarkus.bootstrap.app.QuarkusBootstrap) GeneratedClassBuildItem(io.quarkus.deployment.builditem.GeneratedClassBuildItem) Collections(java.util.Collections) LiveReloadBuildItem(io.quarkus.deployment.builditem.LiveReloadBuildItem) MainClassBuildItem(io.quarkus.deployment.builditem.MainClassBuildItem) LiveReloadBuildItem(io.quarkus.deployment.builditem.LiveReloadBuildItem) Set(java.util.Set) ProcessInheritIODisabledBuildItem(io.quarkus.deployment.pkg.builditem.ProcessInheritIODisabledBuildItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GeneratedClassBuildItem(io.quarkus.deployment.builditem.GeneratedClassBuildItem) AdditionalDependency(io.quarkus.bootstrap.app.AdditionalDependency) Path(java.nio.file.Path) QuarkusAugmentor(io.quarkus.deployment.QuarkusAugmentor) GeneratedResourceBuildItem(io.quarkus.deployment.builditem.GeneratedResourceBuildItem) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) TransformedClassesBuildItem(io.quarkus.deployment.builditem.TransformedClassesBuildItem) BuildResult(io.quarkus.builder.BuildResult) BuildStepBuilder(io.quarkus.builder.BuildStepBuilder) BuildChainBuilder(io.quarkus.builder.BuildChainBuilder) QuarkusBootstrap(io.quarkus.bootstrap.app.QuarkusBootstrap) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

CuratedApplication (io.quarkus.bootstrap.app.CuratedApplication)3 QuarkusBootstrap (io.quarkus.bootstrap.app.QuarkusBootstrap)3 AugmentResult (io.quarkus.bootstrap.app.AugmentResult)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 BootstrapConstants (io.quarkus.bootstrap.BootstrapConstants)1 AdditionalDependency (io.quarkus.bootstrap.app.AdditionalDependency)1 ArtifactResult (io.quarkus.bootstrap.app.ArtifactResult)1 AugmentAction (io.quarkus.bootstrap.app.AugmentAction)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 TsDependency (io.quarkus.bootstrap.resolver.TsDependency)1 BuildChainBuilder (io.quarkus.builder.BuildChainBuilder)1