Search in sources :

Example 1 with MainClassBuildItem

use of io.quarkus.deployment.builditem.MainClassBuildItem in project quarkus by quarkusio.

the class MainClassBuildStep method mainClassBuildStep.

@BuildStep
public MainClassBuildItem mainClassBuildStep(BuildProducer<GeneratedClassBuildItem> generatedClass, ApplicationArchivesBuildItem applicationArchivesBuildItem, CombinedIndexBuildItem combinedIndexBuildItem, Optional<QuarkusApplicationClassBuildItem> quarkusApplicationClass, PackageConfig packageConfig) {
    String mainClassName = MAIN_CLASS;
    Map<String, String> quarkusMainAnnotations = new HashMap<>();
    Collection<AnnotationInstance> quarkusMains = combinedIndexBuildItem.getIndex().getAnnotations(DotName.createSimple(QuarkusMain.class.getName()));
    for (AnnotationInstance i : quarkusMains) {
        AnnotationValue nameValue = i.value("name");
        String name = "";
        if (nameValue != null) {
            name = nameValue.asString();
        }
        if (quarkusMainAnnotations.containsKey(name)) {
            throw new RuntimeException("More than one @QuarkusMain method found with name '" + name + "': " + i.target().asClass().name() + " and " + quarkusMainAnnotations.get(name));
        }
        quarkusMainAnnotations.put(name, i.target().asClass().name().toString());
    }
    if (packageConfig.mainClass.isPresent()) {
        String mainAnnotationClass = quarkusMainAnnotations.get(packageConfig.mainClass.get());
        if (mainAnnotationClass != null) {
            mainClassName = mainAnnotationClass;
        } else {
            mainClassName = packageConfig.mainClass.get();
        }
    } else if (quarkusMainAnnotations.containsKey("")) {
        mainClassName = quarkusMainAnnotations.get("");
    }
    if (mainClassName.equals(MAIN_CLASS)) {
        if (quarkusApplicationClass.isPresent()) {
            // user has not supplied main class, but extension did.
            generateMainForQuarkusApplication(quarkusApplicationClass.get().getClassName(), generatedClass);
        } else {
            // generate a main that just runs the app, the user has not supplied a main class
            ClassCreator file = new ClassCreator(new GeneratedClassGizmoAdaptor(generatedClass, true), MAIN_CLASS, null, Object.class.getName());
            MethodCreator mv = file.getMethodCreator("main", void.class, String[].class);
            mv.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
            mv.invokeStaticMethod(MethodDescriptor.ofMethod(Quarkus.class, "run", void.class, String[].class), mv.getMethodParam(0));
            mv.returnValue(null);
            file.close();
        }
    } else {
        Collection<ClassInfo> impls = combinedIndexBuildItem.getIndex().getAllKnownImplementors(DotName.createSimple(QuarkusApplication.class.getName()));
        ClassInfo classByName = combinedIndexBuildItem.getIndex().getClassByName(DotName.createSimple(mainClassName));
        MethodInfo mainClassMethod = null;
        if (classByName != null) {
            mainClassMethod = classByName.method("main", Type.create(DotName.createSimple(String[].class.getName()), Type.Kind.ARRAY));
        }
        if (mainClassMethod == null) {
            boolean found = false;
            for (ClassInfo i : impls) {
                if (i.name().toString().equals(mainClassName)) {
                    found = true;
                    break;
                }
            }
            if (found) {
                // this is QuarkusApplication, generate a real main to run it
                generateMainForQuarkusApplication(mainClassName, generatedClass);
                mainClassName = MAIN_CLASS;
            } else {
                ClassInfo classInfo = combinedIndexBuildItem.getIndex().getClassByName(DotName.createSimple(mainClassName));
                if (classInfo == null) {
                    throw new IllegalArgumentException("The supplied 'main-class' value of '" + mainClassName + "' does not correspond to either a fully qualified class name or a matching 'name' field of one of the '@QuarkusMain' annotations");
                }
            }
        }
    }
    return new MainClassBuildItem(mainClassName);
}
Also used : MainClassBuildItem(io.quarkus.deployment.builditem.MainClassBuildItem) HashMap(java.util.HashMap) ClassCreator(io.quarkus.gizmo.ClassCreator) GeneratedClassGizmoAdaptor(io.quarkus.deployment.GeneratedClassGizmoAdaptor) MethodCreator(io.quarkus.gizmo.MethodCreator) Quarkus(io.quarkus.runtime.Quarkus) AnnotationValue(org.jboss.jandex.AnnotationValue) MethodInfo(org.jboss.jandex.MethodInfo) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with MainClassBuildItem

use of io.quarkus.deployment.builditem.MainClassBuildItem in project quarkus by quarkusio.

the class JibProcessor method createContainerBuilderFromLegacyJar.

private JibContainerBuilder createContainerBuilderFromLegacyJar(String baseJvmImage, JibConfig jibConfig, ContainerImageConfig containerImageConfig, JarBuildItem sourceJarBuildItem, OutputTargetBuildItem outputTargetBuildItem, MainClassBuildItem mainClassBuildItem, List<ContainerImageLabelBuildItem> containerImageLabels) {
    try {
        // not ideal since this has been previously zipped - we would like to just reuse it
        Path classesDir = outputTargetBuildItem.getOutputDirectory().resolve("jib");
        ZipUtils.unzip(sourceJarBuildItem.getPath(), classesDir);
        JavaContainerBuilder javaContainerBuilder = JavaContainerBuilder.from(toRegistryImage(ImageReference.parse(baseJvmImage), jibConfig.baseRegistryUsername, jibConfig.baseRegistryPassword)).addResources(classesDir, IS_CLASS_PREDICATE.negate()).addClasses(classesDir, IS_CLASS_PREDICATE);
        // when there is no custom entry point, we just set everything up for a regular java run
        if (!jibConfig.jvmEntrypoint.isPresent()) {
            javaContainerBuilder.addJvmFlags(determineEffectiveJvmArguments(jibConfig, Optional.empty())).setMainClass(mainClassBuildItem.getClassName());
        }
        if (sourceJarBuildItem.getLibraryDir() != null) {
            try (Stream<Path> dependenciesPaths = Files.list(sourceJarBuildItem.getLibraryDir())) {
                javaContainerBuilder.addDependencies(dependenciesPaths.filter(p -> Files.isRegularFile(p) && p.getFileName().toString().endsWith(".jar")).sorted(Comparator.comparing(Path::getFileName)).collect(Collectors.toList()));
            }
        }
        JibContainerBuilder jibContainerBuilder = javaContainerBuilder.toContainerBuilder().setEnvironment(getEnvironmentVariables(jibConfig)).setLabels(allLabels(jibConfig, containerImageConfig, containerImageLabels));
        if (jibConfig.useCurrentTimestamp) {
            jibContainerBuilder.setCreationTime(Instant.now());
        }
        if (jibConfig.jvmEntrypoint.isPresent()) {
            jibContainerBuilder.setEntrypoint(jibConfig.jvmEntrypoint.get());
        }
        return jibContainerBuilder;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (InvalidImageReferenceException e) {
        throw new RuntimeException(e);
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ImageReference(com.google.cloud.tools.jib.api.ImageReference) ContainerImageBuilderBuildItem(io.quarkus.container.spi.ContainerImageBuilderBuildItem) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) UpxCompressedBuildItem(io.quarkus.deployment.pkg.builditem.UpxCompressedBuildItem) Port(com.google.cloud.tools.jib.api.buildplan.Port) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) DirectoryStream(java.nio.file.DirectoryStream) Map(java.util.Map) Path(java.nio.file.Path) IsNormalNotRemoteDev(io.quarkus.deployment.IsNormalNotRemoteDev) ContainerImageLabelBuildItem(io.quarkus.container.spi.ContainerImageLabelBuildItem) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) NativeImageBuildItem(io.quarkus.deployment.pkg.builditem.NativeImageBuildItem) ZipUtils(io.quarkus.fs.util.ZipUtils) Predicate(java.util.function.Predicate) ContainerImagePushRequestBuildItem(io.quarkus.container.spi.ContainerImagePushRequestBuildItem) JarBuildItem(io.quarkus.deployment.pkg.builditem.JarBuildItem) PathsUtil.findMainSourcesRoot(io.quarkus.container.util.PathsUtil.findMainSourcesRoot) Collection(java.util.Collection) ContainerImageConfig(io.quarkus.container.image.deployment.ContainerImageConfig) Set(java.util.Set) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) Instant(java.time.Instant) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) PackageConfig(io.quarkus.deployment.pkg.PackageConfig) ContainerImageInfoBuildItem(io.quarkus.container.spi.ContainerImageInfoBuildItem) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) Stream(java.util.stream.Stream) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) ResolvedDependency(io.quarkus.maven.dependency.ResolvedDependency) Optional(java.util.Optional) NativeBinaryUtil(io.quarkus.container.image.deployment.util.NativeBinaryUtil) AvailableContainerImageExtensionBuildItem(io.quarkus.container.spi.AvailableContainerImageExtensionBuildItem) AppCDSResultBuildItem(io.quarkus.deployment.pkg.builditem.AppCDSResultBuildItem) AppCDSContainerImageBuildItem(io.quarkus.deployment.pkg.builditem.AppCDSContainerImageBuildItem) ContainerRuntimeUtil(io.quarkus.runtime.util.ContainerRuntimeUtil) DockerDaemonImage(com.google.cloud.tools.jib.api.DockerDaemonImage) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Version(io.quarkus.builder.Version) Logger(org.jboss.logging.Logger) JibContainer(com.google.cloud.tools.jib.api.JibContainer) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage) HashMap(java.util.HashMap) EnablementUtil.pushContainerImageNeeded(io.quarkus.container.image.deployment.util.EnablementUtil.pushContainerImageNeeded) CurateOutcomeBuildItem(io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) CompiledJavaVersionBuildItem(io.quarkus.deployment.pkg.builditem.CompiledJavaVersionBuildItem) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BuildStep(io.quarkus.deployment.annotations.BuildStep) Jib(com.google.cloud.tools.jib.api.Jib) CredentialRetrieverFactory(com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory) ContainerImageBuildRequestBuildItem(io.quarkus.container.spi.ContainerImageBuildRequestBuildItem) Files(java.nio.file.Files) ArtifactResultBuildItem(io.quarkus.deployment.pkg.builditem.ArtifactResultBuildItem) IOException(java.io.IOException) NativeBuild(io.quarkus.deployment.pkg.steps.NativeBuild) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Paths(java.nio.file.Paths) OutputTargetBuildItem(io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem) PathsUtil(io.quarkus.container.util.PathsUtil) EnablementUtil.buildContainerImageNeeded(io.quarkus.container.image.deployment.util.EnablementUtil.buildContainerImageNeeded) Comparator(java.util.Comparator) Collections(java.util.Collections) MainClassBuildItem(io.quarkus.deployment.builditem.MainClassBuildItem) JarResultBuildStep(io.quarkus.deployment.pkg.steps.JarResultBuildStep) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) UncheckedIOException(java.io.UncheckedIOException) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Aggregations

BuildStep (io.quarkus.deployment.annotations.BuildStep)2 MainClassBuildItem (io.quarkus.deployment.builditem.MainClassBuildItem)2 HashMap (java.util.HashMap)2 Containerizer (com.google.cloud.tools.jib.api.Containerizer)1 DockerDaemonImage (com.google.cloud.tools.jib.api.DockerDaemonImage)1 ImageReference (com.google.cloud.tools.jib.api.ImageReference)1 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)1 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)1 Jib (com.google.cloud.tools.jib.api.Jib)1 JibContainer (com.google.cloud.tools.jib.api.JibContainer)1 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)1 LogEvent (com.google.cloud.tools.jib.api.LogEvent)1 RegistryImage (com.google.cloud.tools.jib.api.RegistryImage)1 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)1 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)1 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)1 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)1 Port (com.google.cloud.tools.jib.api.buildplan.Port)1 CredentialRetrieverFactory (com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory)1 Version (io.quarkus.builder.Version)1