I am trying to deploy a koin application. So far I am using fileProperties pointing to a location in src/main/resources called local.properties.
Now I am deploying a jar and would like to tell Koin to look in a particular folder for the server properties, eg /usr/share/my-app/config/
Unfortunately every attempt to run results in this exception
org.koin.core.error.NoPropertyFileFoundException: No properties found for file
I have tried including the file specifically on the class path of the jar command but without any luck.
Is this by design? That the file properties should be within the jar? Or am I doing something totally wrong...
snippets below
install(Koin) {
fileProperties(propertiesFileName)
modules(properties, monitoring, etc)
}
with various run configs
val propertiesFileName = "/usr/share/my-app/config/app.properties"
val propertiesFileName = "file:///usr/share/my-app/config/app.properties"
val propertiesFileName = "/app.properties" //with -cp including the file specifically
Related
I have a gradle kotlin project , where I want to load a file from my resources directory.
My kotlin classes are in src/main/kotlin/packagenames and my resources are in src/main/resources
After gradle build including kotlin plugin (kotlin("jvm") version "1.7.20") my classes are copied to build/classes/kotlin/main/packagenames and my resources to build/resources/main
Now I try to read this files from my kotlin code
object {}.javaClass.getResourceAsStream("/resources/filename")
or
object {}.javaClass.classloader.getResourceAsStream("/resources/filename")
but the files are not found.
When I try to see which directory it looks into
println(this.javaClass.getResource("/"))
results in build/classes/kotlin and with javaClass.classLoader it returns null accessing '/'
This means normally I would have to go up two directories and then access resources directory, but this doesn't work as build/classes/kotlin seems to be the root.
Any Idea why this happens.
Remove /resources/ from the input string and instead try
object {}.javaClass.getResourceAsStream("/filename")
This should work as expected.
I'm developing an application in Dropwizard 0.9, using IntelliJ Idea as my IDE. I have a run configuration configured, and it passes the command line arguments "server ./path/to/config.yml" to specify the configuration file to run with.
When I try to run my unit tests, however, I cannot determine how to pass this config file path to the server. The Run/Debug Configuration dialog has a "Program Arguments" field, but it is permanently disabled (and the edit button adjacent to it does nothing).
How can I enable program arguments, or, alternatively, is there another way to specify the config file for tests?
if your config.yml is in the project's root folder, just set server config.yml in the IDE. Take a look at this video.
If you would like to access the config.yml from the project's root folder, just pass the name of the file to the rule.
#ClassRule
public static final DropwizardAppRule<DWGettingStartedConfiguration> RULE
= new DropwizardAppRule<>(DWGettingStartedApplication.class,
"config.yml");
Also, it is possible to store the configuration file for integration tests in the src/test/resources folder. In this case, the file can be accessed using the following code:
/**
* A path to test configuration file.
*/
private static final String CONFIG_PATH
= ResourceHelpers.resourceFilePath("test-config.yml");
/**
* Start the application before all test methods.
*/
#ClassRule
public static final DropwizardAppRule<DropBookmarksConfiguration> RULE
= new DropwizardAppRule<>(
DropBookmarksApplication.class,
CONFIG_PATH);
In addition, please take a look at my example projects here, here and here.
I have a property file placed in the root of the web project in Java. I am using Struts 2.
My code is unable to read properties file. Where should I keep my properties file?
I have checked default path , it is where my Eclipse in installed. But I want that system should read file from project folder itself.
Usually you should put the properties file to the src folder so your application is able to read the properties file when you do run your application the properties file is copied from the src folder to the classes folder. As far as you know the classes folder should be the project output folder, so it will be used as a classpath folder and application is able to load the properties file if it is on the classpath.
An example to get properties from the classpath:
Properties prop = new Properties();
try {
//load properties from the class path
prop.load(this.getClass().getClassLoader().getResourceAsStream("myproperties.properties"));
//get the property
System.out.println(prop.getProperty("mykey"));
} catch (IOException ex) {
ex.printStackTrace();
throw ex;
}
However, you can load properties if you know the path to the file on the filesystem, in this case use
prop.load(new FileInputStream("/path/to/myproperties.properties"));
If you are talking about struts.properties
The framework uses a number of properties that can be changed to fit
your needs. To change any of these properties, specify the property
key and value in an struts.properties file. The properties file can be
locate anywhere on the classpath, but it is typically found under
/WEB-INF/classes.
If you are looking for Message Resource properties it could be configured in the struts.properties or struts.xml the later is proffered.
<constant name="struts.custom.i18n.resources" value="path/to/resources/MessageResources"/>
the value is a filepath src/path/to/resources/MessageResources.properties
If you are looking for the proper way to configure your application consider the choice to use EasyConf.
Property files will generally either go:
on the the classpath, e.g., for opening as a resource, or
at a location inaccessible to clients, e.g., under /WEB-INF
Which is more appropriate depends on your needs. Classpath-based files allow bundled default property files without configuration. For example, Log4J will look for log4j.properties at the root of the classpath as its default configuration file.
This can occasionally lead to issues, however, depending on class loading order: sometimes the system can pick up a "stray" config file. Configuring it yourself may still be preferable; I still tend towards the classpath, but config files are also commonly found in WEB-INF. Either method works, and both styles can be configured using JNDI, init params, environment variables, or system variables (e.g., -D).
Keep your myPropertyFile.properties file in src folder (after build project you will find it in WEB-INF/classes) and access them using this code:
Properties prop = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
prop.load(classLoader.getResourceAsStream("/myPropertyFile.properties"));
Ideally, you can save properties files in:
/src/com/cft/web/bundle/.
like,
/src/com/cft/web/bundle/LabelResources.properties
OR
/src/com/cft/web/bundle/Applicationresources.properties.
in fact its up to you to give whatever the path you like.
Just remember to add the correct complete path in web.xml/struts-config.xml
for ex=g.
1. in web.xml :
<description>Application Resources</description>
<env-entry-name>ApplicationResources</env-entry-name>
<env-entry-value>com.cft.web.bundle.ApplicationResources</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
In Struts-config.xml
<message-resources parameter="com.cft.web.bundle.LabelResources" key="yourPropertiesFileName"/>
I'm looking for the best way to use an external properties file with an application that is going to be deployed on Weblogic 10.3 server. I read a number of articles on the site but I don't want to hard-code the path to the properties file or put the file in the domains/mydomain folder.
Is there a dynamic way of doing this so when the application is deployed the properties file is also installed for example under the deployments folder and read from there?
Many thanks
Another alternative that does not require putting the file in a place other applications will read it is to use the Generic File Loading Overrides:
http://download.oracle.com/docs/cd/E21764_01/web.1111/e13702/config.htm#i1066493
This involves creating a directory that will be the root directory of your deployment, let's call it FooApplication that has FooApplication.ear and FooWeb.war. This is called the Application Installation Directory. Your application goes in the FooApplication/app sub-directory whether it is an archive (like .ear, .war, jar) or whether it is an exploded version of one of those archives. Your optional deployment plan (you must have one to use this feature, it could be a plan that does not do much beyond specifying a config-root element and values as described in the documentation) goes in the FooApplication/plan. You can put your properties that you want to override ones in the application in FooApplication/plan/AppFileOverrides directory structure.
http://download.oracle.com/docs/cd/E21764_01/web.1111/e13702/deployunits.htm#sthref9
Once that style of deployment is done, you write code like this from your application and the contents of myApp.properties get read from the FooApplication/plan/AppFileOverrides/FooWeb.war/myApp.properties will be the actual file that is read in.
Properties myAppProps = new Properties();
InputStream iostream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("myCfg/myApp.properties");
myAppProps.load(iostream);
This is accomplished by adding a classloader to your application as explained in the docs. It might seem tedious to initially configure, but it is a feature that directly satisfies the original question and only for that particular application.
You can set a directory on the classpath and Place your custom properties file in that folder/directory. So that, the entire directory along with property file will be on classpath.
To set the directory on the classpath in weblogic 10.3.x
Create a folder in %DOMAIN_HOME%\config\ folder. example appConfig.
Place your custom property file (Let's say config.properties) in appConfig directory/folder.
Modify the setDomainEnv.cmd (Windows) to include appConfig in the classpath by setting %DOMAIN_HOME%\config\appConfig as value to EXT_POST_CLASSPATH(this variable is already defined in the setDomainEnv.cmd file) variable as below:
set EXT_POST_CLASSPATH=%EXT_POST_CLASSPATH%;%DOMAIN_HOME%\config\appConfig
You can access that file in you java code as below:
InputStream inputStream = Thread.currentThread ().getContextClassLoader().getResourceAsStream ("config.properties");
Properties prop = new Properties();
prop.load(inputStream);
String value = prop.getProperty("key");
Hope this helps.
Approach #2
Use Weblogic shared library
http://download.oracle.com/docs/cd/E12840_01/wls/docs103/programming/libraries.html
Follow below steps
Package all your configuration as separate JAR during the build process
Deploy configuration JAR as shared library
Reference above shared library from your EAR/WAR
Deploy EAR/WAR (Configurations will be available in classpath)
When you say " I read a number of articles on the site but I don't want to hard-code the path to the properties file" I assume you are saying you don't want to hard code it in your Java code. If that is so, then please see below
Answered here:
There are ways to read properties file in Java from weblogic classpath
One (Properties file located in the weblogic domain): Drop the properties file inside the Domain directory. This way the properties file is added to the weblogic classpath automatically and we can read from Java using resourceAsStream.
Two (Properties file from a User defined location):The advantage with this approach is that the property file can reside outside the JAR or EAR file and can be modified conveniently.
package com.test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyFileExample {
private static Properties prop;
public static void myMethod() {
InputStream is = null;
try {
prop = new Properties();
String propFilePath = System.getProperty(“propFileLocation“);
InputStream iStream = PropertyFileExample.class.getClassLoader().getResourceAsStream(propFilePath);
prop.load(iStream);
prop.getProperty(“dbuser”);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the weblogic setDomainEnv(under bin) we need to pass the location of the property file as a -D argument to JAVA_OPTIONS
set JAVA_OPTIONS=%JAVA_OPTIONS% -DpropFileLocation =/dev/file/properties/some.properties
Hope it helps!
Approach #1
Update your server startup script to pass below system variable to JVM (below is example on Windows OS)
call "%DOMAIN_HOME%\bin\startWebLogic.cmd" "-Dcom.mycompany.myapp.EXTERNAL_CONFIG_PATH=/mycompany/myapp/config" %*
Using this variable which points to your configuration directories, read configurations from there. You will need to make this setting on each server where you want to deploy your application.
Hopefully should be a simple question...
I have an integration test module which contains the default directory structure:
src
|-main
|-test
|-java
|-resources
Then within my resources directory I have an xxxx.xml and xxxx.xsd file, and I need to load these files in as part of my test:
#Test
public void should_do_some_stuff_with_xml_and_xsd() // not actual test name
{
File xmlFile = new File("xxxx.xml");
File xsdFile = new File("xxxx.xsd");
...
}
It keeps failing trying to load the file, now I presumed it was down to me needing to give it a relative path from the project root or something. I need this test to run externally to my IDE so I can run the tests on the build server when it gets there...
So my question is, how do I target these files?
The classpath mechanism does not work for files. Relative file paths are resolved from the current directory, not from the classpath elements.
So you could just do
File xmlFile = new File("target/test-classes/xxxx.xml");
File xsdFile = new File("target/test-classes/xxxx.xsd");
However, a much cleaner solution would be to work with InputStreams instead of files. Almost every library that supports File parameters also supports InputStream parameters. And that way you can use ClassLoader magic without manually specifying any paths:
ClassLoader cldr = Thread.currentThread().getContextClassLoader();
InputStream xmlStream = cldr.getResourceAsStream("xxxx.xml");
InputStream xsdStream = cldr.getResourceAsStream("xxxx.xsd");
Reference:
Byte Streams (Sun Java Tutorial)
ClassLoader (javadoc)
InputStream (javadoc)