Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LaunchMode injectable #5155

Merged
merged 1 commit into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/runtime/src/main/java/io/quarkus/runtime/LaunchMode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.runtime;

import io.quarkus.runtime.configuration.ProfileManager;

public enum LaunchMode {

/**
Expand Down Expand Up @@ -29,4 +31,12 @@ public boolean isDevOrTest() {
public String getDefaultProfile() {
return defaultProfile;
}

/**
*
* @return The current launch mode
*/
public static LaunchMode current() {
return ProfileManager.getLaunchMode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public static void setLaunchMode(LaunchMode mode) {
launchMode = mode;
}

public static LaunchMode getLaunchMode() {
return launchMode;
}

public static void setRuntimeDefaultProfile(final String profile) {
runtimeDefaultProfile = profile;
}
Expand Down
8 changes: 8 additions & 0 deletions docs/src/main/asciidoc/lifecycle.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,11 @@ When the application is stopped, the second log message is printed.

As usual, the application can be packaged using `./mvnw clean package` and executed using the `-runner.jar` file.
You can also generate the native executable using `./mvnw clean package -Pnative`.

== Launch Modes

Quarkus has 3 different launch modes, `NORMAL` (i.e. production), `DEVELOPMENT` and `TEST`. If you are running `quarkus:dev`
then the mode will be `DEVELOPMENT`, if you are running a JUnit test it will be `TEST`, otherwise it will be `NORMAL`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our Arquillian adapter supports TestNG and there the launch mode is TEST too, I'm not sure how much we want to discourage people from using Arquillian, but it is possible, so perhaps we shouldn't narrow this description to just JUnit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment we only support Arquillian for the MP TCK, this is not something we document for end users.


Your application can get the launch mode by injecting the `io.quarkus.runtime.LaunchMode` enum into a CDI bean,
or by invoking the static method `io.quarkus.runtime.LaunchMode.current()`.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.quarkus.arc.runtime.AdditionalBean;
import io.quarkus.arc.runtime.ArcRecorder;
import io.quarkus.arc.runtime.BeanContainer;
import io.quarkus.arc.runtime.LaunchModeProducer;
import io.quarkus.arc.runtime.LifecycleEventRunner;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.annotations.BuildProducer;
Expand Down Expand Up @@ -328,6 +329,11 @@ void setupExecutor(ExecutorBuildItem executor, ArcRecorder recorder) {
recorder.initExecutor(executor.getExecutorProxy());
}

@BuildStep
AdditionalBeanBuildItem launchMode() {
return new AdditionalBeanBuildItem(LaunchModeProducer.class);
}

private abstract static class AbstractCompositeApplicationClassesPredicate<T> implements Predicate<T> {

private final IndexView applicationClassesIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.runtime.LaunchMode;
import io.quarkus.test.QuarkusUnitTest;

public class SimpleBeanTest {
Expand All @@ -25,6 +26,9 @@ public class SimpleBeanTest {
@Inject
SimpleBean simpleBean;

@Inject
LaunchMode launchMode;

@Test
public void testSimpleBean() {
assertNotNull(simpleBean.getStartupEvent());
Expand All @@ -34,4 +38,9 @@ public void testSimpleBean() {
assertEquals("1", simpleBean.getBazProvider().get());
}

@Test
public void testLaunchModeInjection() {
assertEquals(LaunchMode.TEST, launchMode);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.arc.runtime;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;

import io.quarkus.runtime.LaunchMode;

@Dependent
public class LaunchModeProducer {

@Produces
LaunchMode mode() {
return LaunchMode.current();
}
}