From 9a4a38352952c83a0ba039a7e0ac8959bfc9ca76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Wed, 27 Jul 2022 16:26:58 +0200 Subject: [PATCH] Throw an exception on extension loading if a build step doesn't produce anything --- core/builder/pom.xml | 6 +++++ .../io/quarkus/builder/BuildStepBuilder.java | 9 +++++++ .../java/io/quarkus/builder/BasicTests.java | 27 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/core/builder/pom.xml b/core/builder/pom.xml index b2160369dbd27..d0cf9c314a729 100644 --- a/core/builder/pom.xml +++ b/core/builder/pom.xml @@ -49,6 +49,12 @@ junit-jupiter test + + + org.assertj + assertj-core + test + diff --git a/core/builder/src/main/java/io/quarkus/builder/BuildStepBuilder.java b/core/builder/src/main/java/io/quarkus/builder/BuildStepBuilder.java index 85190c18661b1..254694796a5f6 100644 --- a/core/builder/src/main/java/io/quarkus/builder/BuildStepBuilder.java +++ b/core/builder/src/main/java/io/quarkus/builder/BuildStepBuilder.java @@ -194,6 +194,15 @@ public BuildStepBuilder consumes(Class type, ConsumeFlags f */ public BuildChainBuilder build() { final BuildChainBuilder chainBuilder = this.buildChainBuilder; + if (produces.isEmpty()) { + throw new IllegalArgumentException( + "Build step '" + buildStep.getId() + + "' does not produce any build item and thus will never get executed." + + " Either change the return type of the method to a build item type," + + " add a parameter of type BuildProducer<[some build item type]>/Consumer<[some build item type]>," + + " or annotate the method with @Produces." + + " Use @Produce(EmptyBuildItem.class) if you want to always execute this step."); + } if (BuildChainBuilder.LOG_CONFLICT_CAUSING) { chainBuilder.addStep(this, new Exception().getStackTrace()); } else { diff --git a/core/builder/src/test/java/io/quarkus/builder/BasicTests.java b/core/builder/src/test/java/io/quarkus/builder/BasicTests.java index f9efe4c9a36ca..fcece83ea3625 100644 --- a/core/builder/src/test/java/io/quarkus/builder/BasicTests.java +++ b/core/builder/src/test/java/io/quarkus/builder/BasicTests.java @@ -1,5 +1,6 @@ package io.quarkus.builder; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -176,6 +177,32 @@ public void execute(final BuildContext context) { assertFalse(ran.get()); } + @Test + public void testMissingProduces() { + final BuildChainBuilder builder = BuildChain.builder(); + BuildStepBuilder stepBuilder = builder.addBuildStep(new BuildStep() { + @Override + public void execute(final BuildContext context) { + context.produce(new DummyItem()); + } + + @Override + public String getId() { + return "myBuildStepId"; + } + }); + stepBuilder.consumes(DummyItem.class); + assertThatThrownBy(stepBuilder::build) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContainingAll( + "Build step 'myBuildStepId'", + "does not produce any build item and thus will never get executed", + "change the return type of the method to a build item type", + "add a parameter of type BuildProducer<[some build item type]>/Consumer<[some build item type]>", + "annotate the method with @Produces", + "Use @Produce(EmptyBuildItem.class) if you want to always execute this step"); + } + @Test public void testCircular() { final BuildChainBuilder builder = BuildChain.builder();