I have migrated to Maven project (using Spring) where I don't need to use my old *.xml config files.
The adaptation was easy but what I still can't figure out is: how to let know to my project that I have properties file and wanna use it?
(If you know how to register properties file in POM, please add some brief description how to load data from this file. (if there is any significant difference in using )
EDIT:
In old project, I had just spring-mvc-dmo-servlet.xml config file, where I put every configs (like "scan all components in my projects, cus I don't wanna declare them explicitly"...
If I wanted to let my spring project know I have special file (In this case countries.properties with content what I wanna use - I had to put following code into the config file -
<util:properties id="countryOptions" location="classpath:../countries.properties" />
... only after that, i could wire the content from the file with fields in my classes...
like this
#Value("#{countryOptions}")
private Map<String, String> countryOptions;
(but now I don't have any spring-mvc-dmo-servlet.xml anymore, now I have only POM.xml and in POM my old expression doesnt work ofc )
So my question is:
What I have to put into my POM to say my spring project "HEY THERE IS SPECIAL FILE !"
And how to wire it (#{countryOptions} or ${countryOptions} or something else)
You need to use a maven plugin to load the properties from file
One of them is
http://www.mojohaus.org/properties-maven-plugin/usage.html
EDIT:
In one of our spring boot projects, the path to a property file is passed trough command line using the option:
--spring.config.location=$DIRECTORY/application.properties
Then to get the values:
/** The property to get. */
#Value("${property.to.get}")
private String propertyToGet;
You can also take a look at #PropertySource annotation
#PropertySource("classpath:config.properties")
See https://www.mkyong.com/spring/spring-propertysources-example/
Related
I have created custom SpringApplicationRunListener and added in Meta-INF of project file as org.springframework.boot.SpringApplicationRunListener=
com.dt.testExec
Now,i want to ignore the pre defined property inside .m2/local/org/springframework/boot/spring-boot/2.1.0.RELEASE/spring-boot-2.1.0.RELEASE-sources.jar!/META-INF/spring.factories
org.springframework.boot.SpringApplicationRunListener=
org.springframework.boot.context.event.EventPublishingRunListener
As, when i am running the project, 2 runListener are coming when context gets loaded but i want my custom one i.e "testExec" and not EventPublishingRunListener ?
Or any other way to remove/stop this "EventPublishingRunListener" from "List listeners" when project gets started?
It would be great if anyone can help.
Iam using spring boot org.springframework.boot.loader.JarLauncher to run my self executable spring boot app jar.
Now, I have my self executable jar packed like this (For brevity Iam just adding the files only that are needed to show the problem):
Main.jar
---META-INF
---lib
---a.jar
---com
---comp
---Abc.class
---folder1
---1.txt
---2.txt
---b.jar
and so on.
In my Abc.class Iam trying to load the resource 1.txt which is working fine when I run it in eclipse; but the story starts when I run using command line as self executable jar. I was not able to read that resource and ends up with null pointer error.
After reading few threads, I learnt that my IDEs does some magic on Class Loaders and so it was working fine, which will not be the case when I run it as executable jar
This is how I was Loading the files and the all the possible options I have tried to no luck.
Option 1:
Abc.class.getResourceAsStream("\folder1\1.txt")
Option 2:
Abc.class.getClassLoader().getResourceAsStream("folder1\1.txt")
Option 3: Reading thread, I have tried considered the current thread class loader context as below too, But to no luck again
Thread.currentThread().getContextClassLoader().getResourceAsStream("folder1\1.txt")
Can any one advise. How should I write my code to be able to read my resource that is in the jar and by the class that is in the same jar ?
PS: Spring boot is not adding class-path entry to MANIFEST.MF and if I have to do something around that, how do I do that ? In-fact I tried -cp when running my jar setting it to current directory, but that have not worked either
In Spring, the best way to access a Resource is via Resource APIs. For a Classpath resource what you should use a ClassPathResource and it would look something like this:
Resource resource = new ClassPathResource("/my/resource.json", this.getClass());
After obtaining a Resource you can either get a File reference via getFile() or get an InputStream straight off by calling getInputStream().
Spring provides quite a few different implementations of the Resource interface, take a look at the list of known implementations in the docs
Use Spring's class ClassPathResource to load file from classpath.
For example you can read file into String like this from classpath:
String fileContent = FileUtils.readFileToString(
new ClassPathResource("folder1" + File.separator + "1.txt").getFile());
It doesn't matter what kind of build tool or project structure you are using as long as that file is on classpath.
I want to set a property in a .properties file. It should reference another package or project. I am using spring / spring boot
For example:
some_file: ${another_package_name}/src/main/resources/some_file.txt
Where you say ${another_package_name} that is where the project would be for a gradle or maven project so I'm going to assume you are attempting to cross reference resources between projects.
The location of
${another_package_name}/src/main/resources/some_file.txt
could also be called
classpath:some_file.txt
So then you would just need to make sure that both projects are on the classpath and then some_file.txt should be available to the project as a whole.
Reference
http://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/resources.html
What you are asking for is not possible.
Java properties do not have any semantic values; they are just dumb strings with key / value. If you want such logic, you have to build yourself; like some "processor" that checks for the presence of certain properties and that then "interprets" the value as file name of another property file.
You might want to check if there are any libraries out there that provide such logic.
I am writing a TeamCity plugin and I'm trying to figure out what the best place to put a .properties file would be. I was under the impression that the data/config dir would be on the CLASSPATH, but this doesn't seem to be the case.
Could somebody please give me some examples of existing plugins which are configurable via a .properties file on the file system and how this all should work?
Many thanks in advance!
You can inject a #NotNull ServerPaths serverPaths (see Javadoc) as a parameter to your constructor. Then you can do:
File propertiesFile = new File(serverPaths.getConfigDir() + "/my-plugin.properties");
... and then load up the properties file using a FileInputStream.
I am having issues with properties file when I try to make my standalone Java aplication a runnable jar.
I have 2 properties file, depending upon the machine where its running one gets initialized.
Inside eclipse it was working fine. I was using:
Properties configProps = new Properties();
....
if(machine1)
....
configProps.load(Config.class.getResourceAsStream("machine1.properties"));
else
configProps.load(Config.class.getResourceAsStream("machine2.properties"));
It was working as Config.java and properties were in the same package.
On top of that I have log4j properties located on the root of the project.
That is also not working whne i made Jar.
How to handle the current sutuation.I know putting properties file outside jar is good idea.
How do I accomplish this.
Please help.
Putting the properties file outside of the jar is only a good idea if you need to write to that property file (it's for configuration). Given your naming, I assume it is for configuration.
For reading only, your method is fine if the properties file is properly being packaged in the Jar. Is it? Have you peaked at the contents using jar tf MyJar.jar? Does it show your properties file at the correct path?
Where to store configuration files is a broader issue. Here's a good SO article that examines a few aspects of it (namely where to put it): What is the proper way to store app's conf data in Java?
It seems to me you want to choose a location (see the above article). Once you've done that, the first time the application is run you should load the default properties from your Jar file as you are trying to do, then immediately save them to the location you've chosen. Then, and afterwards, read from and write to that location instead. You will need to use a FileInputStream/FileOutputStream.
Try adding a manifest to your JAR's META-INF with Class-Path set appropriately.