0

The service will be started from eclipse, but the same thing doesn't doesn't work if I start using the jar command. It only works if I give absolute path in the property source. I want the property source to work with relative path.

Below are the logs, main class of the service and file structure of service

java -jar review-service.jar  --spring.profiles.active=dev
2018-01-03T23:17:15.883+0530 [background-preinit] INFO  org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 6.0.4.Final
2018-01-03T23:17:15.909+0530 [main] INFO  org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5c7fa833: startup date [Wed Jan 03 23:17:15 IST 2018]; root of context hierarchy
2018-01-03T23:17:16.349+0530 [main] INFO  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-01-03T23:17:16.521+0530 [main] INFO  org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.retry.annotation.RetryConfiguration' of type [org.springframework.retry.annotation.RetryConfiguration$$EnhancerBySpringCGLIB$$e6c8a335] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-01-03T23:17:16.533+0530 [main] INFO  org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$d1f0e0d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::             (v2.0.0.M6)

2018-01-03T23:17:28.078+0530 [main] INFO  org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration - Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/review-service,dev/'}, ConsulPropertySource {name='config/review-service/'}, ConsulPropertySource {name='config/application,dev/'}, ConsulPropertySource {name='config/application/'}]}
2018-01-03T23:17:28.087+0530 [main] INFO  com.ns.services.review.main.ReviewServiceLauncher - The following profiles are active: dev
2018-01-03T23:17:28.111+0530 [main] INFO  org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48aaecc3: startup date [Wed Jan 03 23:17:28 IST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@5c7fa833
2018-01-03T23:17:28.128+0530 [main] WARN  org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.ns.services.review.main.ReviewServiceLauncher]; nested exception is java.io.FileNotFoundException: config/review-service-dev.properties (No such file or directory)
2018-01-03T23:17:28.129+0530 [main] ERROR org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroy method on bean with name 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' threw an exception
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48aaecc3: startup date [Wed Jan 03 23:17:28 IST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@5c7fa833
 at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:420) ~[spring-context-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:95) ~[spring-context-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:240) ~[spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:576) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:552) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:953) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:521) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.destroySingletons(FactoryBeanRegistrySupport.java:227) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:960) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1035) [spring-context-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:562) [spring-context-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122) [spring-boot-2.0.0.M6.jar!/:2.0.0.M6]
 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:751) [spring-boot-2.0.0.M6.jar!/:2.0.0.M6]
 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:387) [spring-boot-2.0.0.M6.jar!/:2.0.0.M6]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M6.jar!/:2.0.0.M6]
 at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:136) [spring-boot-2.0.0.M6.jar!/:2.0.0.M6]
 at com.ns.services.review.main.ReviewServiceLauncher.main(ReviewServiceLauncher.java:35) [classes!/:?]
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_144]
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_144]
 at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_144]
 at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [review-service.jar:?]
 at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [review-service.jar:?]
 at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [review-service.jar:?]
 at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [review-service.jar:?]

Main Class

package com.ns.services.review.main;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;

import com.ns.commons.application.BaseSpringBootApplication;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableDiscoveryClient
//@ComponentScan(basePackages = { "com.ns.services.review", "com.ns.commons", "com.ns.api" })

@ComponentScan(
  basePackages = { "com.ns.services.review", "com.ns.commons",
  "com.ns.api" }
 )
@EnableSwagger2
@PropertySource({"file:config/review-service-${spring.profiles.active}.properties"})
@EnableFeignClients(basePackages = { "com.ns.services.review", "com.ns.api.clients" })
public class ReviewServiceLauncher extends BaseSpringBootApplication {

 public static void main(String... args) {
  new SpringApplicationBuilder(ReviewServiceLauncher.class).web(WebApplicationType.SERVLET).run(args);
 }

 @Bean
 public Docket api() {
  return new Docket(DocumentationType.SWAGGER_2).select()
    .apis(RequestHandlerSelectors.basePackage("com.ns.services.review")).paths(PathSelectors.any()).build();
 }
}

Snapshot of file structure

enter image description here

shoaib1992
  • 410
  • 1
  • 8
  • 26

1 Answers1

0

config directory should be where you have your review-service.jar file. Put it next to that jar file and it will be fine.

It works from eclipse, because in context of eclipse, application's root is in your project root. On standalone version, application root is the location of jar file (or rather current working directory)

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Hey how can I make sure this config folder automatically goes to review-service.jar location. My jar is generated in review-service/build/libs folder – shoaib1992 Jan 03 '18 at 18:06
  • You can use maven assemply plugin to compy some non project resources into target directory or something. From my perspective you should rather put those into resources allow to optionally use external files. – Antoniossss Jan 03 '18 at 18:12
  • Ok Thanks, In the resources folder shall I move the config folder itself or the contents of config folder ? – shoaib1992 Jan 03 '18 at 18:13
  • Does not matter – Antoniossss Jan 03 '18 at 18:14
  • I have move the config contents to resource folder. But seeing the below exception while starting the service --> Error creating bean with name 'ratingAttributeController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'bye.message' in value "${bye.message}" – shoaib1992 Jan 03 '18 at 18:22
  • Got it working using this: https://stackoverflow.com/a/28307313/3387490 – shoaib1992 Jan 03 '18 at 18:26
  • Changing `file:` to `classpath:` was obvious requirement after moving your files into app resources (thus to the classpath). – Antoniossss Jan 03 '18 at 18:34
  • One more question, now that the things are working I can see that In -> review-service/build/resources/main/config I see that review-service-qa.properties is copied. But I when try to change the property values of this file and restart the service, I don't see the changes getting reflected. My aim is to able to change the properties without building the service. – shoaib1992 Jan 03 '18 at 18:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162451/discussion-between-shoaib627-and-antoniossss). – shoaib1992 Jan 03 '18 at 19:24
  • You need to use external file for that. Its a common practice to have defaults on classpath and override them with external config. – Antoniossss Jan 03 '18 at 19:34
  • Im facing an issue while starting the service using eclipse. It gives me file not found exception. Although the same exact thing works when i start using java -jar *** from commmand line. – shoaib1992 Jan 12 '18 at 12:59