catalina.base system property override in spring-boot?

Going Bananas picture Going Bananas · Aug 13, 2014 · Viewed 7.4k times · Source

My project uses the catalina.base system property to get resources relative to that property's folder location. When working in Eclipse, I pass -Dcatalina.base=. as a VM argument to the application's Main class.

My Main class is implemented like this:

@ComponentScan
@EnableAutoConfiguration
public class Main {

    private static final Logger logger = LogManager.getLogger();

    public static void main(String[] args) {

        if ( logger.isDebugEnabled() )  {
            String debugCatalinaBase = System.getProperty("catalina.base");
            // here catalinaBase is set to "." as expected.
            logger.debug("catalinaBase: {}", debugCatalinaBase);
        }

        SpringApplication.run(Main.class, args);
    }
}

As part of this project, I then have an ApplicationConfig class which is implemented like this:

@Configuration
public class ApplicationConfig {

    private static final Logger logger = LogManager.getLogger();

    @Value("${catalina.base}")
    private String catalinaBase;

    @Bean
    public Properties jndiProperties() {

        if ( logger.isDebugEnabled() )  {
            String debugCatalinaBase = System.getProperty("catalina.base");
            // here catalinaBase is set to "C:\Users\theUser\AppData\Local\Temp\tomcat.8558104871268204693.8081". NOT as expected!!!
            logger.debug("catalinaBase: {}", debugCatalinaBase);
        }

        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            CnsContextFactory.class.getName());
        properties.setProperty(Context.PROVIDER_URL,
            "file:" + catalinaBase + "\\conf\\jndi.properties");
        return properties;
    }

    @Bean( name = "propertyConfigurer" )
    public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = 
            new PropertySourcesPlaceholderConfigurer();
        return configurer;
    }
}

As you can see in the code snippet's comments, in ApplicationConfig the catalina.base system property has changed and I am not sure why or where this happened?

For info, my project uses spring-boot-starter-xxx:1.1.4.RELEASE jars, and spring-xxx:4.0.6.RELEASE jars.

Also, I have another project which follows pretty much the same overall pattern and uses the same jars and that project always sees catalina.base set to "." as expected. So I am wondering if the problem has something to do with the order in which spring configurations are loaded in the problematic project or something along those lines?

Thanks in advance for any help on this. PM

Answer

Phil Webb picture Phil Webb · Aug 13, 2014

String Boot calls Tomcat.setBaseDir(...) from TomcatEmbeddedServletContainerFactory using a temporary directory as the source. Tomcat actually sets the catalina.base property when the initBaseDir() method in org.apache.catalina.startup.Tomcat is called.

You need to add server.tomcat.basedir to your application.properties if you don't want Spring Boot to use a temp folder.