diff --git a/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java new file mode 100644 index 000000000..353f820c1 --- /dev/null +++ b/src/main/java/org/openrewrite/java/spring/SeparateApplicationPropertiesByProfile.java @@ -0,0 +1,237 @@ +/* + * Copyright 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 org.openrewrite.java.spring;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+import org.jspecify.annotations.Nullable;
+import org.openrewrite.*;
+import org.openrewrite.java.marker.JavaProject;
+import org.openrewrite.marker.Markers;
+import org.openrewrite.properties.CreatePropertiesFile;
+import org.openrewrite.properties.PropertiesVisitor;
+import org.openrewrite.properties.tree.Properties;
+
+import java.util.*;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+@Value
+@EqualsAndHashCode(callSuper = false)
+public class SeparateApplicationPropertiesByProfile extends ScanningRecipe
+ * 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 org.openrewrite.java.spring;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.mavenProject;
+import static org.openrewrite.java.Assertions.srcMainResources;
+import static org.openrewrite.properties.Assertions.properties;
+
+class SeparateApplicationPropertiesByProfileTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(new SeparateApplicationPropertiesByProfile());
+ }
+
+ @Test
+ void noApplicationProperties() {
+ rewriteRun(
+ mavenProject("parent",
+ srcMainResources(
+ properties(
+ //language=properties
+ """
+ spring.application.name=Openrewrite-PR-Service
+ #PR-Service
+
+ base-url.PR-services=http://my.url.com
+ exchange-token=1234567890
+ exchange-tokens=${base-url.PR-services}/exchange-token
+ """,
+ sourceSpecs -> sourceSpecs.path("application-dev.properties"))
+ )
+ )
+ );
+ }
+
+ @Test
+ void noSeparateProfile() {
+ rewriteRun(
+ mavenProject("parent",
+ srcMainResources(
+ properties(
+ //language=properties
+ """
+ spring.application.name=Openrewrite-PR-Service
+ #PR-Service
+
+ base-url.PR-services=http://my.url.com
+ exchange-token=1234567890
+ exchange-tokens=${base-url.PR-services}/exchange-token
+ """,
+ sourceSpecs -> sourceSpecs.path("application.properties"))
+ )
+ )
+ );
+ }
+
+ @DocumentExample
+ @Test
+ void separateProfileWithAppend() {
+ rewriteRun(
+ mavenProject("parent",
+ srcMainResources(
+ properties(
+ //language=properties
+ """
+ line1=line1
+ """,
+ //language=properties
+ """
+ line1=line1
+ oauth2.clientId=9999999999999999999999
+ service.domainUrl= https://this.is.my.dev.url.com
+ app.config.currentEnvironment=DEV
+ """,
+ sourceSpecs -> sourceSpecs.path("application-dev.properties")
+ ),
+ properties(
+ //language=properties
+ """
+ spring.application.name=Openrewrite-PR-Service
+ #PR-Service
+ base-url.PR-services=http://my.url.com
+ exchange-token=1234567890
+ exchange-tokens=${base-url.PR-services}/exchange-token
+ !---
+ spring.config.activate.on-profile=dev
+ oauth2.clientId=9999999999999999999999
+ service.domainUrl= https://this.is.my.dev.url.com
+ app.config.currentEnvironment=DEV
+ #---
+ spring.config.activate.on-profile=local
+ app.config.currentEnvironment=LOCAL
+
+
+ #---
+ #### XX Configuration ####
+ spring.config.activate.on-profile=prod
+ oauth2.clientId=77777777777777
+ service.domainUrl=https://this.is.my.prod.url.com
+ """,
+ //language=properties
+ """
+ spring.application.name=Openrewrite-PR-Service
+ #PR-Service
+ base-url.PR-services=http://my.url.com
+ exchange-token=1234567890
+ exchange-tokens=${base-url.PR-services}/exchange-token
+ """,
+ sourceSpecs -> sourceSpecs.path("application.properties")
+ ),
+ properties(
+ null,
+ """
+ app.config.currentEnvironment=LOCAL
+ """,
+ sourceSpecs -> sourceSpecs.path("application-local.properties")
+ ),
+ properties(
+ null,
+ //language=properties
+ """
+ #### XX Configuration ####
+ oauth2.clientId=77777777777777
+ service.domainUrl=https://this.is.my.prod.url.com
+ """,
+ sourceSpecs -> sourceSpecs.path("application-prod.properties")
+ )
+ )
+ )
+ );
+ }
+
+ @Test
+ void separateProfileWithoutAppend() {
+ rewriteRun(
+ mavenProject("parent",
+ srcMainResources(
+ properties(
+ null,
+ //language=properties
+ """
+ oauth2.clientId=9999999999999999999999
+ service.domainUrl= https://this.is.my.dev.url.com
+ app.config.currentEnvironment=DEV
+ """,
+ sourceSpecs -> sourceSpecs.path("application-dev.properties")
+ ),
+ properties(
+ //language=properties
+ """
+ spring.application.name=Openrewrite-PR-Service
+ #PR-Service
+ base-url.PR-services=http://my.url.com
+ exchange-token=1234567890
+ exchange-tokens=${base-url.PR-services}/exchange-token
+ !---
+ spring.config.activate.on-profile=dev
+ oauth2.clientId=9999999999999999999999
+ service.domainUrl= https://this.is.my.dev.url.com
+ app.config.currentEnvironment=DEV
+ #---
+ spring.config.activate.on-profile=local
+ app.config.currentEnvironment=LOCAL
+
+
+ #---
+ #### XX Configuration ####
+ spring.config.activate.on-profile=prod
+ oauth2.clientId=77777777777777
+ service.domainUrl=https://this.is.my.prod.url.com
+ """,
+ //language=properties
+ """
+ spring.application.name=Openrewrite-PR-Service
+ #PR-Service
+ base-url.PR-services=http://my.url.com
+ exchange-token=1234567890
+ exchange-tokens=${base-url.PR-services}/exchange-token
+ """,
+ sourceSpecs -> sourceSpecs.path("application.properties")
+ ),
+ properties(
+ null,
+ //language=properties
+ """
+ app.config.currentEnvironment=LOCAL
+ """,
+ sourceSpecs -> sourceSpecs.path("application-local.properties")
+ ),
+ properties(
+ null,
+ //language=properties
+ """
+ #### XX Configuration ####
+ oauth2.clientId=77777777777777
+ service.domainUrl=https://this.is.my.prod.url.com
+ """,
+ sourceSpecs -> sourceSpecs.path("application-prod.properties")
+ )
+ )
+ )
+ );
+ }
+
+ @Test
+ void pathToApplicationProperties() {
+ rewriteRun(
+ mavenProject("parent",
+ srcMainResources(
+ properties(
+ //language=properties
+ """
+ line1=line1
+ """,
+ //language=properties
+ """
+ line1=line1
+ oauth2.clientId=9999999999999999999999
+ service.domainUrl= https://this.is.my.dev.url.com
+ app.config.currentEnvironment=DEV
+ """,
+ sourceSpecs -> sourceSpecs.path("folder1/folder2/application-dev.properties")
+ ),
+ properties(
+ //language=properties
+ """
+ spring.application.name=Openrewrite-PR-Service
+ #PR-Service
+ base-url.PR-services=http://my.url.com
+ exchange-token=1234567890
+ exchange-tokens=${base-url.PR-services}/exchange-token
+ !---
+ spring.config.activate.on-profile=dev
+ oauth2.clientId=9999999999999999999999
+ service.domainUrl= https://this.is.my.dev.url.com
+ app.config.currentEnvironment=DEV
+ #---
+ spring.config.activate.on-profile=local
+ app.config.currentEnvironment=LOCAL
+
+
+ #---
+ #### XX Configuration ####
+ spring.config.activate.on-profile=prod
+ oauth2.clientId=77777777777777
+ service.domainUrl=https://this.is.my.prod.url.com
+ """,
+ //language=properties
+ """
+ spring.application.name=Openrewrite-PR-Service
+ #PR-Service
+ base-url.PR-services=http://my.url.com
+ exchange-token=1234567890
+ exchange-tokens=${base-url.PR-services}/exchange-token
+ """,
+ sourceSpecs -> sourceSpecs.path("folder1/folder2/application.properties")
+ ),
+ properties(
+ null,
+ //language=properties
+ """
+ app.config.currentEnvironment=LOCAL
+ """,
+ sourceSpecs -> sourceSpecs.path("folder1/folder2/application-local.properties")
+ ),
+ properties(
+ null,
+ //language=properties
+ """
+ #### XX Configuration ####
+ oauth2.clientId=77777777777777
+ service.domainUrl=https://this.is.my.prod.url.com
+ """,
+ sourceSpecs -> sourceSpecs.path("folder1/folder2/application-prod.properties")
+ )
+ )
+ )
+ );
+ }
+
+ @Test
+ void multiModuleProject() {
+ rewriteRun(
+ mavenProject("parent",
+ mavenProject("service",
+ srcMainResources(
+ properties(
+ """
+ global.service=true
+ !---
+ spring.config.activate.on-profile=dev
+ dev.service=true
+ """,
+ """
+ global.service=true
+ """,
+ spec -> spec.path("application.properties")
+ ),
+ properties(
+ null,
+ """
+ dev.service=true
+ """,
+ spec -> spec.path("application-dev.properties")
+ )
+ )
+ ),
+ mavenProject("client",
+ srcMainResources(
+ properties("""
+ global.client=true
+ !---
+ spring.config.activate.on-profile=dev
+ dev.client=true
+ """,
+ """
+ global.client=true
+ """,
+ spec -> spec.path("application.properties")
+ ),
+ properties(
+ """
+ dev.existing=true
+ """,
+ """
+ dev.existing=true
+ dev.client=true
+ """,
+ spec -> spec.path("application-dev.properties")
+ )
+ )
+ )
+ )
+ );
+ }
+}