Skip to content

Command registration differs with @ActiveProfiles in tests vs jar spring.profiles.active= (4.0.1)Β #1327

Closed

Description

@fietser97

We have an application with 2 profiles. (this worked in version 3)

  1. The behavior between @activeprofiles and spring.profiles.active= is different. Where we would not expect this.
  2. When using spring.profiles.active= we get application failed to start because it tries to autoconfigure a command it shouldn't

When we run tests we start the application with @activeprofiles("profileName") this works like expected.
When we run the application with java jar spring.profiles.active="profileName" - jar app.jar we get the following exception. (see at the end)
Even though in our profile it should not try to load this command and this "beanName".

How to reproduce:

  • Create an spring shell application
  • Create 2 spring profiles that load separate packages
  • Try to run the jar with spring.profiles.active=

GOAL

  • Make @ActiveProfile and spring.profiles.active= behave the same way
  • Make spring.profiles.active= not load commands that are not within the active profiles components.

one profile config:

@Configuration
@Profile("profileName")
@ComponentScan(basePackages = "com.example.profileName")
public class profileNameConfiguration {
}

Test with profile:

@ActiveProfiles("profileName")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
        classes = {Application.class},
        args = "helloWorld")
@ExtendWith(OutputCaptureExtension.class)
class ProfileTest {

    @Test
    void testCommandOutput(CapturedOutput output) {
        assertThat(output).contains("Hello world!");
    }
}

Log when running with profile:

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
ERROR 21 --- [main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Method commandRegistry in org.springframework.shell.core.autoconfigure.CommandRegistryAutoConfiguration required a bean of type 'beanName' that could not be found.


Action:

Consider defining a bean of type 'BeanName in your configuration.

Activity

  1. changed the title [-]Command registration differs with @ActiveProfiles in tests vs jar spring.profiles.active=[/-] [+]Command registration differs with @ActiveProfiles in tests vs jar spring.profiles.active= (4.0.1)[/+] on Mar 4, 2026
  2. czpilar commented on Apr 3, 2026

    @czpilar
    Contributor

    Hello, I've just tried to reproduce this issue but I am unable to reproduce it. Here is the configuration I have tried:

    Project structure

    spring-shell-sample-spring-boot/
    β”œβ”€β”€ src/main/java/org/springframework/shell/samples/helloworld/
    β”‚   β”œβ”€β”€ boot/
    β”‚   β”‚   β”œβ”€β”€ SpringShellApplication.java
    β”‚   β”‚   └── ProfileNameConfiguration.java
    β”‚   └── profile/
    β”‚       └── ProfileCommands.java
    └── src/test/java/org/springframework/shell/samples/helloworld/
        └── profile/
            └── ProfileCommandsTest.java
    

    SpringShellApplication.java

    package org.springframework.shell.samples.helloworld.boot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.shell.core.command.annotation.Command;
    import org.springframework.shell.core.command.annotation.Option;
    
    @SpringBootApplication
    public class SpringShellApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(SpringShellApplication.class, args);
    	}
    
    	@Command(name = "hello", description = "Say hello to a given name", group = "Greetings",
    			help = "A command that greets the user with 'Hello ${name}!'. Usage: hello [-n | --name]=<name>")
    	public void sayHello(@Option(shortName = 'n', longName = "name", description = "the name of the person to greet",
    			defaultValue = "World") String name) {
    		System.out.println("Hello " + name + "!");
    	}
    
    }

    ProfileNameConfiguration.java

    package org.springframework.shell.samples.helloworld.boot;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    
    @Configuration
    @Profile("profileName")
    @ComponentScan(basePackages = "org.springframework.shell.samples.helloworld.profile")
    public class ProfileNameConfiguration {
    
    }

    ProfileCommands.java

    package org.springframework.shell.samples.helloworld.profile;
    
    import org.springframework.shell.core.command.annotation.Command;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ProfileCommands {
    
    	@Command(name = "profileCommand",
    			description = "A command that is only available when the 'profileName' profile is active")
    	public void profileCommand() {
    		System.out.println("This command is only available when the 'profileName' profile is active");
    	}
    
    }

    Running without profile - profileCommand is NOT available (as expected)

    ./mvnw -pl org.springframework.shell:spring-shell-sample-spring-boot spring-boot:run
    
    Image

    Running with profile - profileCommand IS available (as expected)

    ./mvnw -pl org.springframework.shell:spring-shell-sample-spring-boot spring-boot:run -Dspring-boot.run.profiles=profileName
    
    Image

    Or equivalently:

    ./mvnw -pl org.springframework.shell:spring-shell-sample-spring-boot spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=profileName"
    
    Image

    Test with @ActiveProfiles - works as expected

    package org.springframework.shell.samples.helloworld.profile;
    
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.system.CapturedOutput;
    import org.springframework.boot.test.system.OutputCaptureExtension;
    import org.springframework.shell.samples.helloworld.boot.SpringShellApplication;
    import org.springframework.test.context.ActiveProfiles;
    
    import static org.assertj.core.api.Assertions.assertThat;
    
    @ActiveProfiles("profileName")
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = { SpringShellApplication.class },
    		properties = "spring.shell.interactive.enabled=false", args = "profileCommand")
    @ExtendWith(OutputCaptureExtension.class)
    class ProfileCommandsTest {
    
    	@Test
    	void testCommandOutput(CapturedOutput output) {
    		assertThat(output).contains("This command is only available");
    	}
    
    }
    Image

    I am not sure if this is your precise configuration but from your description it seems like it is. Could you share a minimal reproducer project or the exact configuration/classes that cause the issue? That would help me pinpoint the problem.

  3. added and removed
    status/need-triageTeam needs to triage and take a first look
    on Apr 3, 2026
  4. fietser97 commented on Apr 7, 2026

    @fietser97
    Author

    sorry, my explanation wasn't clear enough.
    We have 2 profiles. So,

    profile 1

    package org.springframework.shell.samples.helloworld.boot;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    
    @Configuration
    @Profile("profileName")
    @ComponentScan(basePackages = "org.springframework.shell.samples.helloworld.profile")
    public class ProfileNameConfiguration {
    
    }
    

    and anotherProfile

    package org.springframework.shell.samples.helloworld.boot;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    
    @Configuration
    @Profile("anotherProfileName")
    @ComponentScan(basePackages = "org.springframework.shell.samples.helloworld.anotherProfile")
    public class anotherProfileNameConfiguration {
    
    }
    

    then have

    package org.springframework.shell.samples.helloworld.profile;
    
    import org.springframework.shell.core.command.annotation.Command;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ProfileCommands {
    
    	@Command(name = "profileCommand",
    			description = "A command that is only available when the 'profileName' profile is active")
    	public void profileCommand() {
    		System.out.println("This command is only available when the 'profileName' profile is active");
    	}
    
    }
    

    and

    package org.springframework.shell.samples.helloworld.profile;
    
    import org.springframework.shell.core.command.annotation.Command;
    import org.springframework.stereotype.Component;
    
    @Component
    public class AnotherProfileCommands {
    
    	@Command(name = "anotherCommand",
    			description = "A command that is only available when the 'anotherProfileName' profile is active")
    	public void anotherCommand() {
    		System.out.println("This command is only available when the 'anotherProfileName' profile is active");
    	}
    
    }
    

    Though we think that #1329 will fix our issue. Of having just 1 profile set. IF you set more profiles at once it might still break.
    After this version is released we can test it for you. If the above doesn't work out :)

  5. fmbenhassine commented on Apr 7, 2026

    @fmbenhassine
    Contributor

    @fietser97 I confirm the issue with 4.0.1: Here is a minimal reproducer:

    demo-gh1327-shell-4.0.1.zip

    However, I am not able to reproduce the issue with the latest main branch. In fact, only the commands of the active profile are registered. Here is the complete minimal sample:

    spring-shell-sample-spring-boot-gh1327.zip

    IF you set more profiles at once it might still break.

    Tested this as well, and things work as expected: I get the commands of both active profiles:

    14:47:50.138 [main] INFO  o.s.s.s.h.b.SpringShellApplication - Starting SpringShellApplication using Java 17.0.17 with PID 36511 (/Users/mbh/projects/spring-shell/spring-shell-samples/spring-shell-sample-spring-boot/target/classes started by mbh in /Users/mbh/projects/spring-shell)
    14:47:50.139 [main] INFO  o.s.s.s.h.b.SpringShellApplication - The following 2 profiles are active: "profileName", "anotherProfileName"
    14:47:50.346 [main] INFO  o.h.validator.internal.util.Version - HV000001: Hibernate Validator 9.0.1.Final
    14:47:50.775 [main] WARN  org.jline - Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)
    14:47:50.840 [main] INFO  o.s.s.s.h.b.SpringShellApplication - Started SpringShellApplication in 0.821 seconds (process running for 1.054)
    shell:>help
    AVAILABLE COMMANDS
    
    Another Profile Commands
    	anotherCommand: A command that is only available when the 'anotherProfileName' profile is active
    Built-In Commands
    	clear: Clear the terminal screen
    	help: Display help about available commands
    	history: Display or save the history of previously run commands
    	quit, exit: Exit the shell
    	script: Execute commands from a script file
    	version: Show version info
    Profile Commands
    	profileCommand: A command that is only available when the 'profileName' profile is active
    
    shell:>
    

    I believe this issue is fixed with the combination of 236dbc9 and 56cf581, which should be shipped in the upcoming 4.0.2.


    @czpilar Many thanks for the follow up on this!

  6. added and removed on Apr 7, 2026
  7. added this to the 4.0.2 milestone on Apr 7, 2026
  8. fietser97 commented on Apr 7, 2026

    @fietser97
    Author

    Perfect, thanks for letting me know. Happy that it's found and fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions