CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-quarkus--quarkus-arc

Build time CDI dependency injection framework for Quarkus applications with conditional bean support and context management

Pending
Overview
Eval results
Files

build-profiles.mddocs/

Build Profile Conditional Beans

Control bean activation based on Quarkus build profiles, enabling different implementations for development, testing, and production environments. These annotations are processed at build time to determine which beans should be included in the final application.

Capabilities

IfBuildProfile Annotation

Enables beans when specified build profiles are active during application build.

/**
 * When applied to a bean class or producer method (or field), the bean will only be enabled
 * if the Quarkus build time profile matches the rules of the annotation values.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE, ElementType.FIELD })
public @interface IfBuildProfile {
    /**
     * A single profile name to enable a bean if a profile with the same name is active in Quarkus build time config.
     */
    String value() default "";

    /**
     * Multiple profiles names to enable a bean if all the profile names are active in Quarkus build time config.
     */
    String[] allOf() default {};

    /**
     * Multiple profiles names to enable a bean if any the profile names is active in Quarkus build time config.
     */
    String[] anyOf() default {};
}

Usage Examples:

import jakarta.enterprise.context.ApplicationScoped;
import io.quarkus.arc.profile.IfBuildProfile;

// Enabled when "dev" profile is active
@ApplicationScoped
@IfBuildProfile("dev")
public class DevBean {
    public void process() {
        System.out.println("Development mode processing");
    }
}

// Enabled when both "build" and "dev" profiles are active
@ApplicationScoped
@IfBuildProfile(allOf = {"build", "dev"})
public class BuildDevBean {
    public void configure() {
        System.out.println("Build development configuration");
    }
}

// Enabled if either "build" or "dev" profile is active
@ApplicationScoped
@IfBuildProfile(anyOf = {"build", "dev"})
public class FlexibleBean {
    public void execute() {
        System.out.println("Flexible execution");
    }
}

// Complex condition: both "build" and "dev" profiles active AND either "test" or "prod" profile active
@ApplicationScoped
@IfBuildProfile(allOf = {"build", "dev"}, anyOf = {"test", "prod"})
public class ComplexConditionalBean {
    public void complexOperation() {
        System.out.println("Complex conditional operation");
    }
}

UnlessBuildProfile Annotation

Enables beans when specified build profiles are NOT active during application build (inverse of IfBuildProfile).

/**
 * When applied to a bean class or producer method (or field), the bean will only be enabled
 * if the Quarkus build time profile does NOT match the rules of the annotation values.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE, ElementType.FIELD })
public @interface UnlessBuildProfile {
    /**
     * A single profile name to enable a bean if a profile with the same name is NOT active in Quarkus build time config.
     */
    String value() default "";

    /**
     * Multiple profiles names to enable a bean if all the profile names are NOT active in Quarkus build time config.
     */
    String[] allOf() default {};

    /**
     * Multiple profiles names to enable a bean if any the profile names is NOT active in Quarkus build time config.
     */
    String[] anyOf() default {};
}

Usage Examples:

import jakarta.enterprise.context.ApplicationScoped;
import io.quarkus.arc.profile.UnlessBuildProfile;

// Enabled when "dev" profile is NOT active (production/test modes)
@ApplicationScoped
@UnlessBuildProfile("dev")
public class NonDevBean {
    public void productionProcess() {
        System.out.println("Production mode processing");
    }
}

// Enabled when both "build" and "dev" profiles are NOT active
@ApplicationScoped
@UnlessBuildProfile(allOf = {"build", "dev"})
public class NotBuildDevBean {
    public void standardConfiguration() {
        System.out.println("Standard configuration");
    }
}

// Enabled if either "build" or "dev" profile is NOT active
@ApplicationScoped
@UnlessBuildProfile(anyOf = {"build", "dev"})
public class AlternativeBean {
    public void alternativeExecution() {
        System.out.println("Alternative execution path");
    }
}

Producer Method Usage

Both annotations can be applied to producer methods for conditional creation of specific bean instances.

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import io.quarkus.arc.profile.IfBuildProfile;
import io.quarkus.arc.profile.UnlessBuildProfile;

@ApplicationScoped
public class DatabaseConfigProducer {
    
    @Produces
    @IfBuildProfile("dev")
    public DatabaseConfig devDatabaseConfig() {
        return new DatabaseConfig("localhost", 5432, "devdb");
    }
    
    @Produces
    @UnlessBuildProfile("dev")
    public DatabaseConfig prodDatabaseConfig() {
        return new DatabaseConfig("prod-server", 5432, "proddb");
    }
}

class DatabaseConfig {
    private final String host;
    private final int port;
    private final String database;
    
    public DatabaseConfig(String host, int port, String database) {
        this.host = host;
        this.port = port;
        this.database = database;
    }
    
    // getters...
}

Field Producer Usage

Annotations can also be applied to producer fields for simple conditional values.

import jakarta.enterprise.inject.Produces;
import io.quarkus.arc.profile.IfBuildProfile;

@ApplicationScoped
public class ConfigurationProducer {
    
    @Produces
    @IfBuildProfile("dev")
    public final boolean debugMode = true;
    
    @Produces
    @UnlessBuildProfile("dev")
    public final boolean debugMode = false;
}

Profile Configuration

Build profiles are configured through various mechanisms:

Application Properties

# Set active profile in application.properties
quarkus.profile=dev

Command Line

# Set profile via command line
java -Dquarkus.profile=prod -jar application.jar

# Maven build with profile
mvn clean package -Dquarkus.profile=prod

Environment Variable

export QUARKUS_PROFILE=test

Common Patterns

Environment-Specific Services

// Development-only mock service
@ApplicationScoped
@IfBuildProfile("dev")
public class MockEmailService implements EmailService {
    public void sendEmail(String to, String subject, String body) {
        System.out.println("MOCK: Sending email to " + to);
    }
}

// Production email service
@ApplicationScoped
@UnlessBuildProfile("dev")
public class SmtpEmailService implements EmailService {
    public void sendEmail(String to, String subject, String body) {
        // Real SMTP implementation
    }
}

Testing Configuration

// Test-specific database initialization
@ApplicationScoped  
@IfBuildProfile("test")
public class TestDataInitializer {
    
    @EventObserver
    void onStartup(@Observes StartupEvent event) {
        // Initialize test data
    }
}

// Production data migration
@ApplicationScoped
@UnlessBuildProfile(anyOf = {"dev", "test"})
public class ProductionMigrator {
    
    @EventObserver
    void onStartup(@Observes StartupEvent event) {
        // Run production migrations
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-io-quarkus--quarkus-arc

docs

bean-container.md

bean-invocation.md

build-profiles.md

build-properties.md

index.md

interceptor-integration.md

logger-injection.md

runtime-lookup.md

tile.json