From a587d368db873cafd6a25f46a27d54dd66c0e3d5 Mon Sep 17 00:00:00 2001 From: Muyao Date: Wed, 18 Dec 2024 15:23:47 +0800 Subject: [PATCH 1/2] Add help section and Maven plugin when azure-support is selected See gh-1676 --- .../SpringAzureMavenBuildCustomizer.java | 64 +++++++++++++++++++ .../SpringAzureModuleRegistry.java | 28 +++++++- ...ngAzureProjectGenerationConfiguration.java | 8 +++ .../SpringAzureMavenBuildCustomizerTests.java | 40 ++++++++++++ ...reProjectGenerationConfigurationTests.java | 6 ++ 5 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizer.java create mode 100644 start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizerTests.java diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizer.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizer.java new file mode 100644 index 00000000000..7d1b49816a3 --- /dev/null +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizer.java @@ -0,0 +1,64 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.start.site.extension.dependency.springazure; + +import io.spring.initializr.generator.buildsystem.maven.MavenBuild; +import io.spring.initializr.generator.project.ProjectDescription; +import io.spring.initializr.generator.spring.build.BuildCustomizer; + +/** + * A {@link BuildCustomizer} that adds a maven plugin when azure-support is selected. + * + * @author Muyao Feng + */ +class SpringAzureMavenBuildCustomizer implements BuildCustomizer { + + private final ProjectDescription projectDescription; + + SpringAzureMavenBuildCustomizer(ProjectDescription projectDescription) { + this.projectDescription = projectDescription; + } + + @Override + public void customize(MavenBuild build) { + build.plugins().add("com.microsoft.azure", "azure-container-apps-maven-plugin", (plugin) -> { + plugin.version("0.1.0"); + plugin.configuration((configuration) -> { + configuration.add("subscriptionId", "your-subscription-id"); + configuration.add("resourceGroup", "your-resource-group"); + configuration.add("appEnvironmentName", "your-app-environment-name"); + configuration.add("region", "your-region"); + configuration.add("appName", this.projectDescription.getName()); + configuration.add("containers", (containers) -> { + containers.add("container", (container) -> { + container.add("type", "code"); + container.add("directory", "${project.basedir}"); + }); + }); + configuration.add("ingress", (ingress) -> { + ingress.add("external", "true"); + ingress.add("targetPort", "8080"); + }); + configuration.add("scale", (scale) -> { + scale.add("minReplicas", "0"); + scale.add("maxReplicas", "10"); + }); + }); + }); + } + +} diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureModuleRegistry.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureModuleRegistry.java index 095d60c5a76..cac73cda77c 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureModuleRegistry.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureModuleRegistry.java @@ -37,7 +37,7 @@ abstract class SpringAzureModuleRegistry { static Iterable createSpringBootRegistry() { - return create( + return create(onDependencies("azure-support").customizeHelpDocument(addDeploySection()), onDependencies("actuator").customizeBuild(addDependency("spring-cloud-azure-starter-actuator")) .customizeHelpDocument(addReferenceLink("actuator", "Azure Actuator")), onDependencies("integration", "azure-storage") @@ -81,4 +81,30 @@ private static Consumer addReferenceLink(String id, String descrip }; } + private static Consumer addDeploySection() { + return (helpDocument) -> { + helpDocument.addSection((writer) -> { + writer.println("### Deploy to Azure"); + writer.println(); + writer.println("This project can be deployed to Azure with maven support."); + writer.println(); + writer.println( + "In your `pom.xml`, replace the following placeholder variables with your specific Azure details:"); + writer.println("- `subscriptionId`"); + writer.println("- `resourceGroup`"); + writer.println("- `appEnvironmentName`"); + writer.println("- `region`"); + writer.println(); + writer.println("Deploy with:"); + writer.println(""" + ```bash + mvn azure-container-apps:deploy + ``` + """); + writer.println( + "Learn more about [Java on Azure Container Apps](https://learn.microsoft.com/azure/container-apps/java-overview)."); + }); + }; + } + } diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfiguration.java index ddf690a22cf..f8357c469fb 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfiguration.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfiguration.java @@ -17,6 +17,8 @@ package io.spring.start.site.extension.dependency.springazure; import io.spring.initializr.generator.buildsystem.Build; +import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; +import io.spring.initializr.generator.project.ProjectDescription; import io.spring.initializr.generator.project.ProjectGenerationConfiguration; import io.spring.start.site.support.implicit.ImplicitDependency; import io.spring.start.site.support.implicit.ImplicitDependencyBuildCustomizer; @@ -50,4 +52,10 @@ ImplicitDependencyHelpDocumentCustomizer azureDependencyHelpDocumentCustomizer(B return new ImplicitDependencyHelpDocumentCustomizer(this.azureDependencies, build); } + @Bean + @ConditionalOnRequestedDependency("azure-support") + SpringAzureMavenBuildCustomizer azureDependencyMavenBuildCustomizer(ProjectDescription projectDescription) { + return new SpringAzureMavenBuildCustomizer(projectDescription); + } + } diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizerTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizerTests.java new file mode 100644 index 00000000000..2b20a7393eb --- /dev/null +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizerTests.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.start.site.extension.dependency.springazure; + +import io.spring.initializr.web.project.ProjectRequest; +import io.spring.start.site.extension.AbstractExtensionTests; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link SpringAzureMavenBuildCustomizer}. + * + * @author Muyao Feng + */ +class SpringAzureMavenBuildCustomizerTests extends AbstractExtensionTests { + + @Test + void azureContainerAppsMavenPluginAddedWhenAzureSupportPresent() { + ProjectRequest request = createProjectRequest("azure-support"); + assertThat(mavenPom(request)).hasText("/project/build/plugins/plugin[1]/groupId", "com.microsoft.azure"); + assertThat(mavenPom(request)).hasText("/project/build/plugins/plugin[1]/artifactId", + "azure-container-apps-maven-plugin"); + } + +} diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfigurationTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfigurationTests.java index 97b1a9f311f..405b5e06dbb 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfigurationTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfigurationTests.java @@ -118,6 +118,12 @@ void azureJdbcWithPostgresql() { .doesNotContain("https://aka.ms/spring/msdocs/mysql"); } + @Test + void DeploytoAzureSectionAddedWhenAzureSupportPresent() { + ProjectStructure project = generateProject("azure-support"); + assertThatHelpDocumentOf(project).contains("Deploy to Azure"); + } + private static Stream azureDependencies() { return Stream.of(Arguments.of("azure-active-directory"), Arguments.of("azure-keyvault"), Arguments.of("azure-storage"), Arguments.of("azure-support")); From 32ba6e1ad42daff43cacc75c8f1d1c05e037b570 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 19 Dec 2024 09:00:46 +0100 Subject: [PATCH 2/2] Polish "Add help section and Maven plugin when azure-support is selected" See gh-1676 --- .../springazure/SpringAzureMavenBuildCustomizer.java | 6 ++++-- .../springazure/SpringAzureModuleRegistry.java | 9 +++++---- .../SpringAzureProjectGenerationConfiguration.java | 5 ++++- .../SpringAzureMavenBuildCustomizerTests.java | 12 +++++++++--- ...ringAzureProjectGenerationConfigurationTests.java | 2 +- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizer.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizer.java index 7d1b49816a3..1cc42bae4b6 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizer.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizer.java @@ -21,12 +21,14 @@ import io.spring.initializr.generator.spring.build.BuildCustomizer; /** - * A {@link BuildCustomizer} that adds a maven plugin when azure-support is selected. + * A {@link BuildCustomizer} that adds a Maven plugin when azure-support is selected. * * @author Muyao Feng */ class SpringAzureMavenBuildCustomizer implements BuildCustomizer { + private static final String PLUGIN_VERSION = "0.1.0"; + private final ProjectDescription projectDescription; SpringAzureMavenBuildCustomizer(ProjectDescription projectDescription) { @@ -36,7 +38,7 @@ class SpringAzureMavenBuildCustomizer implements BuildCustomizer { @Override public void customize(MavenBuild build) { build.plugins().add("com.microsoft.azure", "azure-container-apps-maven-plugin", (plugin) -> { - plugin.version("0.1.0"); + plugin.version(PLUGIN_VERSION); plugin.configuration((configuration) -> { configuration.add("subscriptionId", "your-subscription-id"); configuration.add("resourceGroup", "your-resource-group"); diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureModuleRegistry.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureModuleRegistry.java index cac73cda77c..c208fc57ab1 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureModuleRegistry.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureModuleRegistry.java @@ -86,19 +86,20 @@ private static Consumer addDeploySection() { helpDocument.addSection((writer) -> { writer.println("### Deploy to Azure"); writer.println(); - writer.println("This project can be deployed to Azure with maven support."); + writer.println("This project can be deployed to Azure with Maven."); writer.println(); writer.println( - "In your `pom.xml`, replace the following placeholder variables with your specific Azure details:"); + "To get started, replace the following placeholder in your `pom.xml` with your specific Azure details:"); + writer.println(); writer.println("- `subscriptionId`"); writer.println("- `resourceGroup`"); writer.println("- `appEnvironmentName`"); writer.println("- `region`"); writer.println(); - writer.println("Deploy with:"); + writer.println("Now you can deploy your application:"); writer.println(""" ```bash - mvn azure-container-apps:deploy + ./mvnw azure-container-apps:deploy ``` """); writer.println( diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfiguration.java index f8357c469fb..edb867178d8 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfiguration.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,8 @@ package io.spring.start.site.extension.dependency.springazure; import io.spring.initializr.generator.buildsystem.Build; +import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem; +import io.spring.initializr.generator.condition.ConditionalOnBuildSystem; import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; import io.spring.initializr.generator.project.ProjectDescription; import io.spring.initializr.generator.project.ProjectGenerationConfiguration; @@ -54,6 +56,7 @@ ImplicitDependencyHelpDocumentCustomizer azureDependencyHelpDocumentCustomizer(B @Bean @ConditionalOnRequestedDependency("azure-support") + @ConditionalOnBuildSystem(MavenBuildSystem.ID) SpringAzureMavenBuildCustomizer azureDependencyMavenBuildCustomizer(ProjectDescription projectDescription) { return new SpringAzureMavenBuildCustomizer(projectDescription); } diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizerTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizerTests.java index 2b20a7393eb..c0740ba3a14 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizerTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureMavenBuildCustomizerTests.java @@ -26,15 +26,21 @@ * Tests for {@link SpringAzureMavenBuildCustomizer}. * * @author Muyao Feng + * @author Moritz Halbritter */ class SpringAzureMavenBuildCustomizerTests extends AbstractExtensionTests { + @Test + void shouldDoNothingIfAzureSupportIsntSelected() { + ProjectRequest request = createProjectRequest("web"); + assertThat(mavenPom(request)).doesNotContain("azure-container-apps-maven-plugin"); + } + @Test void azureContainerAppsMavenPluginAddedWhenAzureSupportPresent() { ProjectRequest request = createProjectRequest("azure-support"); - assertThat(mavenPom(request)).hasText("/project/build/plugins/plugin[1]/groupId", "com.microsoft.azure"); - assertThat(mavenPom(request)).hasText("/project/build/plugins/plugin[1]/artifactId", - "azure-container-apps-maven-plugin"); + assertThat(mavenPom(request)).hasText("/project/build/plugins/plugin[1]/groupId", "com.microsoft.azure") + .hasText("/project/build/plugins/plugin[1]/artifactId", "azure-container-apps-maven-plugin"); } } diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfigurationTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfigurationTests.java index 405b5e06dbb..1d71361684e 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfigurationTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springazure/SpringAzureProjectGenerationConfigurationTests.java @@ -121,7 +121,7 @@ void azureJdbcWithPostgresql() { @Test void DeploytoAzureSectionAddedWhenAzureSupportPresent() { ProjectStructure project = generateProject("azure-support"); - assertThatHelpDocumentOf(project).contains("Deploy to Azure"); + assertThatHelpDocumentOf(project).contains("### Deploy to Azure"); } private static Stream azureDependencies() {