0
# Build Profile Conditional Beans
1
2
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.
3
4
## Capabilities
5
6
### IfBuildProfile Annotation
7
8
Enables beans when specified build profiles are active during application build.
9
10
```java { .api }
11
/**
12
* When applied to a bean class or producer method (or field), the bean will only be enabled
13
* if the Quarkus build time profile matches the rules of the annotation values.
14
*/
15
@Retention(RetentionPolicy.RUNTIME)
16
@Target({ ElementType.METHOD, ElementType.TYPE, ElementType.FIELD })
17
public @interface IfBuildProfile {
18
/**
19
* A single profile name to enable a bean if a profile with the same name is active in Quarkus build time config.
20
*/
21
String value() default "";
22
23
/**
24
* Multiple profiles names to enable a bean if all the profile names are active in Quarkus build time config.
25
*/
26
String[] allOf() default {};
27
28
/**
29
* Multiple profiles names to enable a bean if any the profile names is active in Quarkus build time config.
30
*/
31
String[] anyOf() default {};
32
}
33
```
34
35
**Usage Examples:**
36
37
```java
38
import jakarta.enterprise.context.ApplicationScoped;
39
import io.quarkus.arc.profile.IfBuildProfile;
40
41
// Enabled when "dev" profile is active
42
@ApplicationScoped
43
@IfBuildProfile("dev")
44
public class DevBean {
45
public void process() {
46
System.out.println("Development mode processing");
47
}
48
}
49
50
// Enabled when both "build" and "dev" profiles are active
51
@ApplicationScoped
52
@IfBuildProfile(allOf = {"build", "dev"})
53
public class BuildDevBean {
54
public void configure() {
55
System.out.println("Build development configuration");
56
}
57
}
58
59
// Enabled if either "build" or "dev" profile is active
60
@ApplicationScoped
61
@IfBuildProfile(anyOf = {"build", "dev"})
62
public class FlexibleBean {
63
public void execute() {
64
System.out.println("Flexible execution");
65
}
66
}
67
68
// Complex condition: both "build" and "dev" profiles active AND either "test" or "prod" profile active
69
@ApplicationScoped
70
@IfBuildProfile(allOf = {"build", "dev"}, anyOf = {"test", "prod"})
71
public class ComplexConditionalBean {
72
public void complexOperation() {
73
System.out.println("Complex conditional operation");
74
}
75
}
76
```
77
78
### UnlessBuildProfile Annotation
79
80
Enables beans when specified build profiles are NOT active during application build (inverse of IfBuildProfile).
81
82
```java { .api }
83
/**
84
* When applied to a bean class or producer method (or field), the bean will only be enabled
85
* if the Quarkus build time profile does NOT match the rules of the annotation values.
86
*/
87
@Retention(RetentionPolicy.RUNTIME)
88
@Target({ ElementType.METHOD, ElementType.TYPE, ElementType.FIELD })
89
public @interface UnlessBuildProfile {
90
/**
91
* A single profile name to enable a bean if a profile with the same name is NOT active in Quarkus build time config.
92
*/
93
String value() default "";
94
95
/**
96
* Multiple profiles names to enable a bean if all the profile names are NOT active in Quarkus build time config.
97
*/
98
String[] allOf() default {};
99
100
/**
101
* Multiple profiles names to enable a bean if any the profile names is NOT active in Quarkus build time config.
102
*/
103
String[] anyOf() default {};
104
}
105
```
106
107
**Usage Examples:**
108
109
```java
110
import jakarta.enterprise.context.ApplicationScoped;
111
import io.quarkus.arc.profile.UnlessBuildProfile;
112
113
// Enabled when "dev" profile is NOT active (production/test modes)
114
@ApplicationScoped
115
@UnlessBuildProfile("dev")
116
public class NonDevBean {
117
public void productionProcess() {
118
System.out.println("Production mode processing");
119
}
120
}
121
122
// Enabled when both "build" and "dev" profiles are NOT active
123
@ApplicationScoped
124
@UnlessBuildProfile(allOf = {"build", "dev"})
125
public class NotBuildDevBean {
126
public void standardConfiguration() {
127
System.out.println("Standard configuration");
128
}
129
}
130
131
// Enabled if either "build" or "dev" profile is NOT active
132
@ApplicationScoped
133
@UnlessBuildProfile(anyOf = {"build", "dev"})
134
public class AlternativeBean {
135
public void alternativeExecution() {
136
System.out.println("Alternative execution path");
137
}
138
}
139
```
140
141
### Producer Method Usage
142
143
Both annotations can be applied to producer methods for conditional creation of specific bean instances.
144
145
```java
146
import jakarta.enterprise.context.ApplicationScoped;
147
import jakarta.enterprise.inject.Produces;
148
import io.quarkus.arc.profile.IfBuildProfile;
149
import io.quarkus.arc.profile.UnlessBuildProfile;
150
151
@ApplicationScoped
152
public class DatabaseConfigProducer {
153
154
@Produces
155
@IfBuildProfile("dev")
156
public DatabaseConfig devDatabaseConfig() {
157
return new DatabaseConfig("localhost", 5432, "devdb");
158
}
159
160
@Produces
161
@UnlessBuildProfile("dev")
162
public DatabaseConfig prodDatabaseConfig() {
163
return new DatabaseConfig("prod-server", 5432, "proddb");
164
}
165
}
166
167
class DatabaseConfig {
168
private final String host;
169
private final int port;
170
private final String database;
171
172
public DatabaseConfig(String host, int port, String database) {
173
this.host = host;
174
this.port = port;
175
this.database = database;
176
}
177
178
// getters...
179
}
180
```
181
182
### Field Producer Usage
183
184
Annotations can also be applied to producer fields for simple conditional values.
185
186
```java
187
import jakarta.enterprise.inject.Produces;
188
import io.quarkus.arc.profile.IfBuildProfile;
189
190
@ApplicationScoped
191
public class ConfigurationProducer {
192
193
@Produces
194
@IfBuildProfile("dev")
195
public final boolean debugMode = true;
196
197
@Produces
198
@UnlessBuildProfile("dev")
199
public final boolean debugMode = false;
200
}
201
```
202
203
## Profile Configuration
204
205
Build profiles are configured through various mechanisms:
206
207
### Application Properties
208
209
```properties
210
# Set active profile in application.properties
211
quarkus.profile=dev
212
```
213
214
### Command Line
215
216
```bash
217
# Set profile via command line
218
java -Dquarkus.profile=prod -jar application.jar
219
220
# Maven build with profile
221
mvn clean package -Dquarkus.profile=prod
222
```
223
224
### Environment Variable
225
226
```bash
227
export QUARKUS_PROFILE=test
228
```
229
230
## Common Patterns
231
232
### Environment-Specific Services
233
234
```java
235
// Development-only mock service
236
@ApplicationScoped
237
@IfBuildProfile("dev")
238
public class MockEmailService implements EmailService {
239
public void sendEmail(String to, String subject, String body) {
240
System.out.println("MOCK: Sending email to " + to);
241
}
242
}
243
244
// Production email service
245
@ApplicationScoped
246
@UnlessBuildProfile("dev")
247
public class SmtpEmailService implements EmailService {
248
public void sendEmail(String to, String subject, String body) {
249
// Real SMTP implementation
250
}
251
}
252
```
253
254
### Testing Configuration
255
256
```java
257
// Test-specific database initialization
258
@ApplicationScoped
259
@IfBuildProfile("test")
260
public class TestDataInitializer {
261
262
@EventObserver
263
void onStartup(@Observes StartupEvent event) {
264
// Initialize test data
265
}
266
}
267
268
// Production data migration
269
@ApplicationScoped
270
@UnlessBuildProfile(anyOf = {"dev", "test"})
271
public class ProductionMigrator {
272
273
@EventObserver
274
void onStartup(@Observes StartupEvent event) {
275
// Run production migrations
276
}
277
}
278
```