diff --git a/extensions/picocli/deployment/src/main/java/io/quarkus/picocli/deployment/PicocliProcessor.java b/extensions/picocli/deployment/src/main/java/io/quarkus/picocli/deployment/PicocliProcessor.java index b2ed32c390f8b6..06562e639d7ac3 100644 --- a/extensions/picocli/deployment/src/main/java/io/quarkus/picocli/deployment/PicocliProcessor.java +++ b/extensions/picocli/deployment/src/main/java/io/quarkus/picocli/deployment/PicocliProcessor.java @@ -4,7 +4,10 @@ import java.util.List; import java.util.stream.Collectors; +import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationTarget; +import org.jboss.jandex.AnnotationValue; +import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; import org.jboss.jandex.IndexView; import org.jboss.jandex.MethodInfo; @@ -12,6 +15,7 @@ import io.quarkus.arc.deployment.AdditionalBeanBuildItem; import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem; import io.quarkus.arc.deployment.AutoAddScopeBuildItem; +import io.quarkus.arc.deployment.UnremovableBeanBuildItem; import io.quarkus.arc.processor.AnnotationsTransformer; import io.quarkus.arc.processor.BuiltinScope; import io.quarkus.deployment.Feature; @@ -76,6 +80,7 @@ IndexDependencyBuildItem picocliIndexDependency() { void picocliRunner(ApplicationIndexBuildItem applicationIndex, CombinedIndexBuildItem combinedIndex, BuildProducer additionalBean, + BuildProducer unremovableBean, BuildProducer quarkusApplicationClass, BuildProducer annotationsTransformer) { IndexView index = combinedIndex.getIndex(); @@ -99,6 +104,20 @@ void picocliRunner(ApplicationIndexBuildItem applicationIndex, additionalBean.produce(AdditionalBeanBuildItem.unremovableOf(DefaultPicocliCommandLineFactory.class)); quarkusApplicationClass.produce(new QuarkusApplicationClassBuildItem(PicocliRunner.class)); } + + // make the arguments of @Command unremovable beans if necessary + Collection commandInstances = index.getAnnotations(CommandLine.Command.class); + for (AnnotationInstance commandInstance : commandInstances) { + AnnotationValue versionProvider = commandInstance.value("versionProvider"); + if (versionProvider != null) { + ClassInfo versionProviderInfo = index.getClassByName(versionProvider.asClass().name()); + if (versionProviderInfo != null) { + if (BuiltinScope.isDeclaredOn(versionProviderInfo)) { // TODO: should we make these beans even when they are not annotated as such? + unremovableBean.produce(UnremovableBeanBuildItem.beanTypes(versionProviderInfo.name())); + } + } + } + } } private List classesAnnotatedWith(IndexView indexView, String annotationClassName) { diff --git a/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/EntryWithVersionCommand.java b/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/EntryWithVersionCommand.java new file mode 100644 index 00000000000000..c2121fe991b46a --- /dev/null +++ b/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/EntryWithVersionCommand.java @@ -0,0 +1,9 @@ +package io.quarkus.it.picocli; + +import io.quarkus.picocli.runtime.annotations.TopCommand; +import picocli.CommandLine; + +@TopCommand +@CommandLine.Command(mixinStandardHelpOptions = true, versionProvider = VersionProvider.class) +public class EntryWithVersionCommand { +} diff --git a/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/VersionProvider.java b/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/VersionProvider.java new file mode 100644 index 00000000000000..e06d7c92156f7e --- /dev/null +++ b/integration-tests/picocli/src/main/java/io/quarkus/it/picocli/VersionProvider.java @@ -0,0 +1,22 @@ +package io.quarkus.it.picocli; + +import jakarta.inject.Singleton; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + +import picocli.CommandLine; + +@Singleton +public class VersionProvider implements CommandLine.IVersionProvider { + + private final String version; + + public VersionProvider(@ConfigProperty(name = "some.version", defaultValue = "0.0.1") String version) { + this.version = version; + } + + @Override + public String[] getVersion() throws Exception { + return new String[] { version }; + } +} diff --git a/integration-tests/picocli/src/test/java/io/quarkus/it/picocli/TestVersion.java b/integration-tests/picocli/src/test/java/io/quarkus/it/picocli/TestVersion.java new file mode 100644 index 00000000000000..1218095d3de492 --- /dev/null +++ b/integration-tests/picocli/src/test/java/io/quarkus/it/picocli/TestVersion.java @@ -0,0 +1,25 @@ +package io.quarkus.it.picocli; + +import static io.quarkus.it.picocli.TestUtils.createConfig; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusProdModeTest; + +public class TestVersion { + + @RegisterExtension + static final QuarkusProdModeTest config = createConfig("version-app", EntryWithVersionCommand.class, + VersionProvider.class) + .overrideConfigKey("some.version", "1.1") + .setCommandLineParameters("--version"); + + @Test + public void simpleTest() { + Assertions.assertThat(config.getStartupConsoleOutput()).containsOnlyOnce("1.1"); + Assertions.assertThat(config.getExitCode()).isZero(); + } + +}