Search in sources :

Example 6 with AugmentResult

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

the class BuildMojo method doExecute.

@Override
protected void doExecute() throws MojoExecutionException {
    try {
        Set<String> propertiesToClear = new HashSet<>();
        // if and only if they are not already set.
        for (Map.Entry<String, String> entry : systemProperties.entrySet()) {
            String key = entry.getKey();
            if (System.getProperty(key) == null) {
                System.setProperty(key, entry.getValue());
                propertiesToClear.add(key);
            }
        }
        // they execute "mvn package -Dnative" even if quarkus.package.type has been set in application.properties
        if (!System.getProperties().containsKey(PACKAGE_TYPE_PROP) && isNativeProfileEnabled(mavenProject())) {
            Object packageTypeProp = mavenProject().getProperties().get(PACKAGE_TYPE_PROP);
            String packageType = NATIVE_PACKAGE_TYPE;
            if (packageTypeProp != null) {
                packageType = packageTypeProp.toString();
            }
            System.setProperty(PACKAGE_TYPE_PROP, packageType);
            propertiesToClear.add(PACKAGE_TYPE_PROP);
        }
        if (!propertiesToClear.isEmpty() && mavenSession().getRequest().getDegreeOfConcurrency() > 1) {
            getLog().warn("*****************************************************************");
            getLog().warn("* Your build is requesting parallel execution, but the project  *");
            getLog().warn("* relies on System properties at build time which could cause   *");
            getLog().warn("* race condition issues thus unpredictable build results.       *");
            getLog().warn("* Please avoid using System properties or avoid enabling        *");
            getLog().warn("* parallel execution                                            *");
            getLog().warn("*****************************************************************");
        }
        try (CuratedApplication curatedApplication = bootstrapApplication()) {
            AugmentAction action = curatedApplication.createAugmentor();
            AugmentResult result = action.createProductionApplication();
            Artifact original = mavenProject().getArtifact();
            if (result.getJar() != null) {
                if (!skipOriginalJarRename && result.getJar().isUberJar() && result.getJar().getOriginalArtifact() != null) {
                    final Path standardJar = result.getJar().getOriginalArtifact();
                    if (Files.exists(standardJar)) {
                        final Path renamedOriginal = standardJar.getParent().toAbsolutePath().resolve(standardJar.getFileName() + ".original");
                        try {
                            IoUtils.recursiveDelete(renamedOriginal);
                            Files.move(standardJar, renamedOriginal);
                        } catch (IOException e) {
                            throw new UncheckedIOException(e);
                        }
                        original.setFile(result.getJar().getOriginalArtifact().toFile());
                    }
                }
                if (result.getJar().isUberJar()) {
                    projectHelper.attachArtifact(mavenProject(), result.getJar().getPath().toFile(), result.getJar().getClassifier());
                }
            }
        } finally {
            // Clear all the system properties set by the plugin
            propertiesToClear.forEach(System::clearProperty);
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to build quarkus application", e);
    }
}
Also used : Path(java.nio.file.Path) AugmentAction(io.quarkus.bootstrap.app.AugmentAction) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AugmentResult(io.quarkus.bootstrap.app.AugmentResult) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Artifact(org.apache.maven.artifact.Artifact) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) UncheckedIOException(java.io.UncheckedIOException) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) Map(java.util.Map) HashSet(java.util.HashSet)

Example 7 with AugmentResult

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

the class QuarkusProdModeTest method beforeAll.

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
    ensureNoInjectAnnotationIsUsed(extensionContext.getRequiredTestClass());
    ExclusivityChecker.checkTestType(extensionContext, QuarkusProdModeTest.class);
    originalHandlers = rootLogger.getHandlers();
    rootLogger.addHandler(inMemoryLogHandler);
    timeoutTask = new PrintStackTraceTimerTask();
    timeoutTimer.schedule(timeoutTask, 1000 * 60 * 5);
    ExtensionContext.Store store = extensionContext.getRoot().getStore(ExtensionContext.Namespace.GLOBAL);
    if (store.get(TestResourceManager.class.getName()) == null) {
        TestResourceManager manager = new TestResourceManager(extensionContext.getRequiredTestClass());
        manager.init(null);
        testResourceProperties = manager.start();
        store.put(TestResourceManager.class.getName(), manager);
        store.put(TestResourceManager.CLOSEABLE_NAME, new ExtensionContext.Store.CloseableResource() {

            @Override
            public void close() throws Throwable {
                manager.close();
            }
        });
    }
    Class<?> testClass = extensionContext.getRequiredTestClass();
    try {
        outputDir = Files.createTempDirectory("quarkus-prod-mode-test");
        Path deploymentDir = outputDir.resolve("deployment-result");
        buildDir = outputDir.resolve("build-result");
        if (applicationName != null) {
            overrideConfigKey("quarkus.application.name", applicationName);
        }
        if (applicationVersion != null) {
            overrideConfigKey("quarkus.application.version", applicationVersion);
        }
        if (buildNative) {
            overrideConfigKey("quarkus.package.type", "native");
        }
        exportArchive(deploymentDir, testClass);
        Path testLocation = PathTestHelper.getTestClassesLocation(testClass);
        Path customSourcesDir = createCustomSources(testClass);
        // as this project will appear as the root application artifact during the bootstrap
        if (Files.isDirectory(testLocation)) {
            final Path projectClassesDir = PathTestHelper.getAppClassLocationForTestLocation(testLocation.toString());
            if (!Files.exists(projectClassesDir)) {
                Files.createDirectories(projectClassesDir);
            }
        }
        QuarkusBootstrap.Builder builder = QuarkusBootstrap.builder().setApplicationRoot(deploymentDir).setMode(QuarkusBootstrap.Mode.PROD).setLocalProjectDiscovery(true).setIsolateDeployment(true).addExcludedPath(testLocation).setProjectRoot(testLocation).setTargetDirectory(buildDir).setForcedDependencies(forcedDependencies);
        if (applicationName != null) {
            builder.setBaseName(applicationName);
        }
        Map<String, Object> buildContext = new HashMap<>();
        buildContext.put(BUILD_CONTEXT_CUSTOM_SOURCES_PATH_KEY, customSourcesDir);
        if (!buildChainCustomizerEntries.isEmpty()) {
            // we need to make sure all the classes needed to support the customizer flow are available at bootstrap time
            // for that purpose we add them to a new archive that is then added to Quarkus bootstrap
            Path additionalDeploymentDir = Files.createDirectories(outputDir.resolve("additional-deployment"));
            JavaArchive additionalDeploymentArchive = ShrinkWrap.create(JavaArchive.class).addClasses(ProdModeTestBuildChainCustomizerProducer.class, ProdModeTestBuildChainBuilderConsumer.class, ProdModeTestBuildStep.class);
            // we push data from the test extension down to the customizers via JDK classes only because this data needs to be
            // accessible by different classloaders
            Map<Object, Object> entriesMap = new HashMap<>();
            buildContext.put(BUILD_CONTEXT_BUILD_STEP_ENTRIES, entriesMap);
            for (BuildChainCustomizerEntry entry : buildChainCustomizerEntries) {
                additionalDeploymentArchive.addClasses(entry.getBuildStepClass());
                entriesMap.put(entry.getBuildStepClass().getName(), Map.of(BUILD_CONTEXT_BUILD_STEP_ENTRY_PRODUCES, entry.getProduces().stream().map(Class::getName).collect(Collectors.toList()), BUILD_CONTEXT_BUILD_STEP_ENTRY_CONSUMES, entry.getConsumes().stream().map(Class::getName).collect(Collectors.toList())));
            }
            additionalDeploymentArchive.as(ExplodedExporter.class).exportExplodedInto(additionalDeploymentDir.toFile());
            builder.addAdditionalDeploymentArchive(additionalDeploymentDir);
        }
        curatedApplication = builder.build().bootstrap();
        AugmentAction action;
        if (buildChainCustomizerEntries.isEmpty()) {
            action = curatedApplication.createAugmentor();
        } else {
            action = curatedApplication.createAugmentor(ProdModeTestBuildChainCustomizerProducer.class.getName(), buildContext);
        }
        AugmentResult result;
        try {
            result = action.createProductionApplication();
            if (assertBuildException != null) {
                fail("The build was expected to fail");
            }
        } catch (Exception e) {
            if (assertBuildException != null) {
                assertBuildException.accept(e);
                return;
            } else {
                throw e;
            }
        } finally {
            curatedApplication.close();
        }
        builtResultArtifact = setupProdModeResults(testClass, buildDir, result);
        if (run) {
            start();
            if (logfilePath != null) {
                logfileField = Arrays.stream(testClass.getDeclaredFields()).filter(f -> f.isAnnotationPresent(LogFile.class) && Path.class.equals(f.getType())).findAny();
                logfileField.ifPresent(f -> f.setAccessible(true));
            }
        }
    } catch (Exception e) {
        preventOutputDirCleanup = true;
        logOutputPathForPostMortem();
        throw new RuntimeException(e);
    }
}
Also used : Path(java.nio.file.Path) AugmentAction(io.quarkus.bootstrap.app.AugmentAction) HashMap(java.util.HashMap) AugmentResult(io.quarkus.bootstrap.app.AugmentResult) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) JUnitException(org.junit.platform.commons.JUnitException) FileNotFoundException(java.io.FileNotFoundException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) QuarkusBootstrap(io.quarkus.bootstrap.app.QuarkusBootstrap) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) ExplodedExporter(org.jboss.shrinkwrap.api.exporter.ExplodedExporter) TestResourceManager(io.quarkus.test.common.TestResourceManager)

Example 8 with AugmentResult

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

the class BuildMojo method doExecute.

@Override
protected void doExecute() throws MojoExecutionException {
    try {
        Set<String> propertiesToClear = new HashSet<>();
        // if and only if they are not already set.
        for (Map.Entry<String, String> entry : systemProperties.entrySet()) {
            String key = entry.getKey();
            if (System.getProperty(key) == null) {
                System.setProperty(key, entry.getValue());
                propertiesToClear.add(key);
            }
        }
        // they execute "mvn package -Dnative" even if quarkus.package.type has been set in application.properties
        if (!System.getProperties().containsKey(PACKAGE_TYPE_PROP) && isNativeProfileEnabled(mavenProject())) {
            Object packageTypeProp = mavenProject().getProperties().get(PACKAGE_TYPE_PROP);
            String packageType = NATIVE_PACKAGE_TYPE;
            if (packageTypeProp != null) {
                packageType = packageTypeProp.toString();
            }
            System.setProperty(PACKAGE_TYPE_PROP, packageType);
            propertiesToClear.add(PACKAGE_TYPE_PROP);
        }
        if (!propertiesToClear.isEmpty() && mavenSession().getRequest().getDegreeOfConcurrency() > 1) {
            getLog().warn("*****************************************************************");
            getLog().warn("* Your build is requesting parallel execution, but the project  *");
            getLog().warn("* relies on System properties at build time which could cause   *");
            getLog().warn("* race condition issues thus unpredictable build results.       *");
            getLog().warn("* Please avoid using System properties or avoid enabling        *");
            getLog().warn("* parallel execution                                            *");
            getLog().warn("*****************************************************************");
        }
        try (CuratedApplication curatedApplication = bootstrapApplication()) {
            AugmentAction action = curatedApplication.createAugmentor();
            AugmentResult result = action.createProductionApplication();
            Artifact original = mavenProject().getArtifact();
            if (result.getJar() != null) {
                if (!skipOriginalJarRename && result.getJar().isUberJar() && result.getJar().getOriginalArtifact() != null) {
                    final Path standardJar = result.getJar().getOriginalArtifact();
                    if (Files.exists(standardJar)) {
                        final Path renamedOriginal = standardJar.getParent().toAbsolutePath().resolve(standardJar.getFileName() + ".original");
                        try {
                            IoUtils.recursiveDelete(renamedOriginal);
                            Files.move(standardJar, renamedOriginal);
                        } catch (IOException e) {
                            throw new UncheckedIOException(e);
                        }
                        original.setFile(result.getJar().getOriginalArtifact().toFile());
                    }
                }
                if (result.getJar().isUberJar()) {
                    projectHelper.attachArtifact(mavenProject(), result.getJar().getPath().toFile(), result.getJar().getClassifier());
                }
            }
        } finally {
            // Clear all the system properties set by the plugin
            propertiesToClear.forEach(System::clearProperty);
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Failed to build quarkus application", e);
    }
}
Also used : Path(java.nio.file.Path) AugmentAction(io.quarkus.bootstrap.app.AugmentAction) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AugmentResult(io.quarkus.bootstrap.app.AugmentResult) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Artifact(org.apache.maven.artifact.Artifact) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) UncheckedIOException(java.io.UncheckedIOException) CuratedApplication(io.quarkus.bootstrap.app.CuratedApplication) Map(java.util.Map) HashSet(java.util.HashSet)

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