Provide a condition for detecting war deployments
Closes gh-19421
This commit is contained in:
@@ -10,6 +10,7 @@ dependencies {
|
||||
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web")) {
|
||||
exclude group: "org.hibernate.validator"
|
||||
}
|
||||
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
|
||||
|
||||
intTestImplementation(enforcedPlatform(project(path: ":spring-boot-project:spring-boot-parent")))
|
||||
intTestImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
|
||||
@@ -20,8 +21,6 @@ dependencies {
|
||||
intTestImplementation("org.springframework:spring-web")
|
||||
|
||||
providedRuntime(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-tomcat"))
|
||||
|
||||
runtimeOnly(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
|
||||
}
|
||||
|
||||
intTest {
|
||||
|
||||
@@ -66,6 +66,16 @@ class DeploymentIntegrationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("deployedApplications")
|
||||
void conditionalOnWarShouldBeTrue(DeployedApplication application) throws Exception {
|
||||
application.test((rest) -> {
|
||||
ResponseEntity<String> response = rest.getForEntity("/actuator/war", String.class);
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getBody()).isEqualTo("{\"hello\":\"world\"}");
|
||||
});
|
||||
}
|
||||
|
||||
static List<DeployedApplication> deployedApplications() {
|
||||
return Arrays.asList(new DeployedApplication("open-liberty:19.0.0.9-webProfile8", "/config/dropins", 9080),
|
||||
new DeployedApplication("tomcat:9.0.29-jdk8-openjdk", "/usr/local/tomcat/webapps", 8080),
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample;
|
||||
package sample.app;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample;
|
||||
package sample.app;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2012-2020 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 sample.autoconfig;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWarDeployment;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@ConditionalOnWarDeployment
|
||||
@Configuration
|
||||
public class ExampleAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public TestEndpoint testEndpoint() {
|
||||
return new TestEndpoint();
|
||||
}
|
||||
|
||||
@Endpoint(id = "war")
|
||||
static class TestEndpoint {
|
||||
|
||||
@ReadOperation
|
||||
String hello() {
|
||||
return "{\"hello\":\"world\"}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
sample.autoconfig.ExampleAutoConfiguration
|
||||
@@ -0,0 +1 @@
|
||||
management.endpoints.web.exposure.include: '*'
|
||||
@@ -11,6 +11,7 @@ configurations {
|
||||
|
||||
dependencies {
|
||||
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
|
||||
testImplementation(project(":spring-boot-project:spring-boot-actuator-autoconfigure"))
|
||||
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
|
||||
testImplementation("com.samskivert:jmustache")
|
||||
testImplementation("jakarta.servlet:jakarta.servlet-api")
|
||||
@@ -25,6 +26,7 @@ dependencies {
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-tools:spring-boot-maven-plugin", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-jetty", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-parent", configuration: "mavenRepository"))
|
||||
testRepository(project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter-tomcat", configuration: "mavenRepository"))
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2012-2020 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 com.autoconfig;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWarDeployment;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@ConditionalOnWarDeployment
|
||||
@Configuration
|
||||
public class ExampleAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public TestEndpoint testEndpoint() {
|
||||
return new TestEndpoint();
|
||||
}
|
||||
|
||||
@Endpoint(id = "war")
|
||||
static class TestEndpoint {
|
||||
|
||||
@ReadOperation
|
||||
String hello() {
|
||||
return "{\"hello\":\"world\"}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -146,6 +146,23 @@ class ApplicationBuilder {
|
||||
srcMainWebapp.mkdirs();
|
||||
FileCopyUtils.copy("webapp resource", new FileWriter(new File(srcMainWebapp, "webapp-resource.txt")));
|
||||
}
|
||||
copyAutoConfigurationFiles(appFolder);
|
||||
return;
|
||||
}
|
||||
|
||||
private void copyAutoConfigurationFiles(File appFolder) throws IOException {
|
||||
File autoConfigPackage = new File(appFolder, "src/main/java/com/autoconfig");
|
||||
autoConfigPackage.mkdirs();
|
||||
FileCopyUtils.copy(new File("src/test/java/com/autoconfig/ExampleAutoConfiguration.java"),
|
||||
new File(autoConfigPackage, "ExampleAutoConfiguration.java"));
|
||||
File srcMainResources = new File(appFolder, "src/main/resources");
|
||||
srcMainResources.mkdirs();
|
||||
File metaInf = new File(srcMainResources, "META-INF");
|
||||
metaInf.mkdirs();
|
||||
FileCopyUtils.copy(new File("src/test/resources/META-INF/spring.factories"),
|
||||
new File(metaInf, "spring.factories"));
|
||||
FileCopyUtils.copy(new File("src/test/resources/application.yml"),
|
||||
new File(srcMainResources, "application.yml"));
|
||||
}
|
||||
|
||||
private void packageApplication(File appFolder, File settingsXml) throws MavenInvocationException {
|
||||
|
||||
@@ -79,4 +79,10 @@ class EmbeddedServletContainerJarPackagingIntegrationTests {
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
|
||||
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -102,6 +102,12 @@ class EmbeddedServletContainerWarPackagingIntegrationTests {
|
||||
.noneMatch((resourcePath) -> resourcePath.startsWith("/org/springframework/boot/loader"));
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
void conditionalOnWarDeploymentBeanIsNotAvailableForEmbeddedServer(RestTemplate rest) {
|
||||
ResponseEntity<String> entity = rest.getForEntity("/actuator/war", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
private List<String> readLines(String input) {
|
||||
if (input == null) {
|
||||
return Collections.emptyList();
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.autoconfig.ExampleAutoConfiguration
|
||||
@@ -0,0 +1 @@
|
||||
management.endpoints.web.exposure.include: '*'
|
||||
@@ -18,7 +18,11 @@
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<artifactId>spring-boot-starter-json</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@@ -26,7 +30,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
|
||||
Reference in New Issue
Block a user