There are more than one ways and it depends on if you are using spring-boot for example or not:
- In application.properties/yml file:
server.servlet.context-path=/newName
- Java System Property:
You can also set the context path as a Java system property before even the context is initialized:
public static void main(String[] args)
{
System.setProperty("server.servlet.context-path", "/newName");
SpringApplication.run(Application.class, args);
}
- OS Environment Variable:
Linux:
export SERVER_SERVLET_CONTEXT_PATH=/newName
Windows:
set SERVER_SERVLET_CONTEXT_PATH=/newName
The above environment variable is for Spring Boot 2.x.x, If we have 1.x.x, the variable is SERVER_CONTEXT_PATH.
- Command Line Arguments
We can set the properties dynamically via command line arguments as well:
java -jar app.jar --server.servlet.context-path=/newName
- Using Java Config
With Spring Boot 2, we can use WebServerFactoryCustomizer:
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
return factory -> factory.setContextPath("/newName");
}
With Spring Boot 1, we can create an instance of EmbeddedServletContainerCustomizer:
@Bean
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer() {
return container -> container.setContextPath("/newName");
}
Eclipse + Maven
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<wtpversion>2.0</wtpversion>
<wtpContextName>newName</wtpContextName>
</configuration>
</plugin>
- Eclipse + Gradle
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
eclipse {
wtp {
component {
contextPath = 'newName'
}
}
}
Following links might be helpful: