0

We have a maven project which has only yaml files and relevant text files under src/main/resource folder. We are packing this as Jar file. The structure is as:

src\main\resource
application-configone.yml
application-configtwo.yml
license.txt 

application-configone.yml file has entry for license.txt file

license
 path: src\main\resource\license.txt

Now we import this Jar artifact in another web application. In web application yml file, we are importing the yml file from Jar as

spring.active.profile = configone, configtwo

The issue that we are getting is that web application fails to start as it is not able to read/get license.txt file defined in application-configone file.

How can we read/access license.txt file which is packaged in a jar in our web application

greenHorn
  • 497
  • 1
  • 5
  • 16
  • hi, interesting, not sure if this might be of interest https://stackoverflow.com/questions/36768656/spring-refering-the-resources-static-folder – IronMan May 14 '20 at 20:20
  • @IronMan: thanks for the link, but we dont want to have any class files in the project we are packing as jar, its objective is to hold only config files.I am afraid the suggestion in the shared link would not help. – greenHorn May 14 '20 at 20:25
  • So you only have the main class inside this jar? – CodeScale May 14 '20 at 21:22
  • no main class, just the yml files – greenHorn May 15 '20 at 04:51

1 Answers1

0

The easiest way is to put the file under resources directory. In project where read, create a

appContext=new ClassPathXmlApplicationContext()

and use that to reach the file. Use relative path from resources folder (in your example you use resource without s on the end, maven default is resources). You can obtain any files on your classpath to this anyway.

res = appContext.getResource("classpath:licence.txt")

It will give back a Resource object. You can use it's inputStream now as you want.

Take a look inside the jar which contains the resources (appconfs and txt), so you can be sure it was built properly, and the files was copied into the classpath.

zlaval
  • 1,941
  • 1
  • 10
  • 11