|
1 | 1 | /* |
2 | | - * Copyright 2012 - present the original author or authors. |
| 2 | + * Copyright 2012-2023 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package io.spring.start.site.extension.dependency.springamqp; |
18 | 18 |
|
| 19 | +import io.spring.initializr.generator.buildsystem.Build; |
19 | 20 | import io.spring.initializr.generator.condition.ConditionalOnPlatformVersion; |
20 | 21 | import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; |
21 | 22 | import io.spring.initializr.generator.project.ProjectGenerationConfiguration; |
|
34 | 35 | * Configuration for generation of projects that depend on Spring AMQP. |
35 | 36 | * |
36 | 37 | * @author Stephane Nicoll |
| 38 | + * @author Eddú Meléndez |
37 | 39 | */ |
38 | 40 | @ProjectGenerationConfiguration |
39 | 41 | class SpringAmqpProjectGenerationConfiguration { |
40 | 42 |
|
41 | 43 | @Configuration(proxyBeanMethods = false) |
42 | | - @ConditionalOnRequestedDependency("amqp") |
43 | 44 | static class AmqpConfiguration { |
44 | 45 |
|
45 | 46 | @Bean |
46 | 47 | @ConditionalOnPlatformVersion("[3.5.0,4.0.0-RC1]") |
| 48 | + @ConditionalOnRequestedDependency("amqp") |
47 | 49 | SpringRabbitTestBuildCustomizer springAmqpTestBuildCustomizer() { |
48 | 50 | return new SpringRabbitTestBuildCustomizer(); |
49 | 51 | } |
50 | 52 |
|
51 | 53 | @Bean |
52 | 54 | @ConditionalOnRequestedDependency("testcontainers") |
53 | | - ServiceConnectionsCustomizer rabbitServiceConnectionsCustomizer(DockerServiceResolver serviceResolver, |
54 | | - Testcontainers testcontainers) { |
55 | | - Container container = testcontainers.getContainer(SupportedContainer.RABBITMQ); |
56 | | - return (serviceConnections) -> serviceResolver |
57 | | - .doWith("rabbit", (service) -> serviceConnections.addServiceConnection( |
58 | | - ServiceConnection.ofContainer("rabbit", service, container.className(), container.generic()))); |
| 55 | + ServiceConnectionsCustomizer rabbitServiceConnectionsCustomizer(Build build, |
| 56 | + DockerServiceResolver serviceResolver, Testcontainers testcontainers) { |
| 57 | + return (serviceConnections) -> { |
| 58 | + if (isAmqpEnabled(build)) { |
| 59 | + Container container = testcontainers.getContainer(SupportedContainer.RABBITMQ); |
| 60 | + serviceResolver.doWith("rabbit", |
| 61 | + (service) -> serviceConnections.addServiceConnection(ServiceConnection.ofContainer("rabbit", |
| 62 | + service, container.className(), container.generic()))); |
| 63 | + } |
| 64 | + }; |
59 | 65 | } |
60 | 66 |
|
61 | 67 | @Bean |
62 | 68 | @ConditionalOnRequestedDependency("docker-compose") |
63 | | - ComposeFileCustomizer rabbitComposeFileCustomizer(DockerServiceResolver serviceResolver) { |
64 | | - return (composeFile) -> serviceResolver.doWith("rabbit", |
65 | | - (service) -> composeFile.services() |
| 69 | + ComposeFileCustomizer rabbitComposeFileCustomizer(Build build, DockerServiceResolver serviceResolver) { |
| 70 | + return (composeFile) -> { |
| 71 | + if (isAmqpEnabled(build)) { |
| 72 | + serviceResolver.doWith("rabbit", (service) -> composeFile.services() |
66 | 73 | .add("rabbitmq", |
67 | 74 | service.andThen((builder) -> builder.environment("RABBITMQ_DEFAULT_USER", "myuser") |
68 | 75 | .environment("RABBITMQ_DEFAULT_PASS", "secret") |
69 | 76 | .ports(5672)))); |
| 77 | + } |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + private boolean isAmqpEnabled(Build build) { |
| 82 | + return build.dependencies().has("amqp") || build.dependencies().has("amqp-streams"); |
70 | 83 | } |
71 | 84 |
|
72 | 85 | } |
|
0 commit comments