BAEL-6532: Define main class when using multiple profiles in a Spring Boot project (#14267)

Co-authored-by: Tapan Avasthi <tavasthi@Tapans-MacBook-Air.local>
This commit is contained in:
Tapan Avasthi 2023-06-24 07:40:42 +05:30 committed by GitHub
parent 7e07ee0bea
commit c54937de56
7 changed files with 70 additions and 7 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-basic-customization</artifactId> <artifactId>spring-boot-basic-customization</artifactId>
<name>spring-boot-basic-customization</name> <name>spring-boot-basic-customization</name>
@ -39,4 +39,50 @@
<start-class>com.baeldung.changeport.CustomApplication</start-class> <start-class>com.baeldung.changeport.CustomApplication</start-class>
</properties> </properties>
<profiles>
<profile>
<id>errorhandling</id>
<properties>
<spring.boot.mainclass>com.baeldung.errorhandling.ErrorHandlingApplication</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>filter</id>
<properties>
<spring.boot.mainclass>com.baeldung.bootcustomfilters.SpringBootFiltersApplication</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>customapplication</id>
<properties>
<spring.boot.mainclass>com.baeldung.changeport.CustomApplication</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>failureanalyzer</id>
<properties>
<spring.boot.mainclass>com.baeldung.failureanalyzer.FailureAnalyzerApplication</spring.boot.mainclass>
</properties>
</profile>
<profile>
<id>favicon</id>
<properties>
<spring.boot.mainclass>com.baeldung.favicon.FaviconApplication</spring.boot.mainclass>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.2.RELEASE</version>
<configuration>
<mainClass>${spring.boot.mainclass}</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -2,13 +2,15 @@ package com.baeldung.bootcustomfilters;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;
/** /**
* Boot application * Boot application
* @author hemant * @author hemant
* *
*/ */
@SpringBootApplication @Profile("filter")
@SpringBootApplication(scanBasePackages = "com.baeldung.bootcustomfilters.*")
public class SpringBootFiltersApplication { public class SpringBootFiltersApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -4,8 +4,10 @@ import java.util.Collections;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;
@SpringBootApplication @Profile("customapplication")
@SpringBootApplication(scanBasePackages = "com.baeldung.changeport")
public class CustomApplication { public class CustomApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -2,7 +2,9 @@ package com.baeldung.errorhandling;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;
@Profile("errorhandling")
@SpringBootApplication(scanBasePackages = "com.baeldung.errorhandling") @SpringBootApplication(scanBasePackages = "com.baeldung.errorhandling")
public class ErrorHandlingApplication { public class ErrorHandlingApplication {

View File

@ -2,10 +2,12 @@ package com.baeldung.failureanalyzer;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;
import javax.annotation.security.RolesAllowed; import javax.annotation.security.RolesAllowed;
@SpringBootApplication @Profile("failureanalyzer")
@SpringBootApplication(scanBasePackages = "com.baeldung.failureanalyzer")
public class FailureAnalyzerApplication { public class FailureAnalyzerApplication {
@RolesAllowed("*") @RolesAllowed("*")
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -2,8 +2,10 @@ package com.baeldung.favicon;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Profile;
@SpringBootApplication @Profile("favicon")
@SpringBootApplication(scanBasePackages = "com.baeldung.favicon")
public class FaviconApplication { public class FaviconApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -1,13 +1,16 @@
package com.baeldung.failureanalyzer; package com.baeldung.failureanalyzer;
import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.ILoggingEvent;
import com.baeldung.failureanalyzer.utils.ListAppender; import com.baeldung.failureanalyzer.utils.ListAppender;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import java.util.Collection; import java.util.Collection;
import java.util.Properties;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -27,7 +30,11 @@ public class FailureAnalyzerAppIntegrationTest {
@Test @Test
public void givenBeanCreationErrorInContext_whenContextLoaded_thenFailureAnalyzerLogsReport() { public void givenBeanCreationErrorInContext_whenContextLoaded_thenFailureAnalyzerLogsReport() {
try { try {
SpringApplication.run(FailureAnalyzerApplication.class); Properties properties = new Properties();
properties.setProperty("spring.profiles.active", "failureanalyzer");
SpringApplication app = new SpringApplication(FailureAnalyzerApplication.class);
app.setDefaultProperties(properties);
app.run();
} catch (BeanCreationException e) { } catch (BeanCreationException e) {
Collection<String> allLoggedEntries = ListAppender.getEvents() Collection<String> allLoggedEntries = ListAppender.getEvents()
.stream() .stream()