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);
}
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);
}
}
Aggregations