Reputation: 3987
When I am configuring -Dext.prop.dir
from eclipse Its run fine and and everything work. It runs on 8085 port and specified context "DBService"
.
But when i am running jar generated using maven from command line it wont read application.properties it by default tomcat is starting on 8080 and i am not able to identify the context. Every things else works fine.
In eclipse I have supplied : VM argument as : -Dext.prop.dir=E:/res/ -Dlog.name=D:/res/logback.xml
My post and question looks similar and i have refer this post already with that only i have refer application.properteis to configure custom context and port for spring boot application. I have refer this link as well but i could not figured out what is the problem when I am running jar from command line.
Same vm argument i am putting as below :
java -Dext.prop.dir=D:/res/ -jar com.drd.db.services-1.0.0.M1-jar-with-dependencies.jar
And I have kept my application.properteis as D:/res/application.properteis.
Below is my application startup class and application.properties .
package com.drd.application.configuration;
import static com.drd.db.service.util.ServicesConstants.EXT_PROP_DIR;
import static com.drd.db.service.util.ServicesConstants.LOG_BACK_XML_SYSTEM_PROPERTY;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import com.drd.db.service.util.ServicesConstants;
@ComponentScan(basePackages = { "com.drd.db" })
@Configuration
@PropertySource({"file:///${ext.prop.dir}application.properties"/*, "classpath:application.properties"*/})
@EnableAutoConfiguration
public class ApplicationRunner {
private static final Logger LOG = LoggerFactory.getLogger(ApplicationRunner.class);
public static void main(String[] args) throws Exception {
String extPropDir=System.getProperty(EXT_PROP_DIR);
if(StringUtils.isEmpty(extPropDir)){
LOG.warn("Could not resolve PropertyvSource placeholder 'ext.prop.dir' in string value file:///${ext.prop.dire}application.properties");
LOG.warn("Could not finnd {} System property Please specify valid path for it ",ServicesConstants.EXT_PROP_DIR);
return;
}
String logbackXMLPath=System.getProperty(LOG_BACK_XML_SYSTEM_PROPERTY);
if(StringUtils.isEmpty(logbackXMLPath)){
LOG.info("Could not finnd {} System property for loading logback.xml Please specify valid path for it proper loaggin ", LOG_BACK_XML_SYSTEM_PROPERTY);
}
LOG.info("ApplicationRunner Spring boot application start from this class");
SpringApplication.run(ApplicationRunner.class, args);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
return factory;
}
}
MY applicaion.properties :
#All application Label properties configure if any.
server.context-path=/DBService
server.port=8085
hibernate.prop.dir=D:/main/resources/
can any one know or share why those properties are not configured or read by spring when i am running from command line.
And one supersizing things is that if I am not putting application.properties in D:/res/ directory it throws excpeption as below , it mean it is reading that properties file:
Exception :
2016-08-07 14:40:09.435 INFO 18168 --- [ main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/D:com.drd.db.services-1.0.0.M1-jar-with-dependencies.jar]
2016-08-07 14:40:09.445 ERROR 18168 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.drd.application.configuration.ApplicationRunner; nested exception
is java.io.FileNotFoundException: D:\htl-properties\application.properties (The system cannot find the file specified)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:159)
...
at org.springframework.boot.SpringApplication.run(SpringApplication.java:944)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:933)
at com.drd.application.configuration.ApplicationRunner.main(ApplicationRunner.java:45)
Caused by: java.io.FileNotFoundException: D:\res\application.properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
Upvotes: 2
Views: 2690
Reputation: 721
As far as I understood you would like to use variables which are in application.properties.
The way I follow is the below one ;
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
After that insert following tag in your pom.xml as well
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
then build your -jar with "mvn install" or "mvn clean install".
I hope it works.
Upvotes: 2