I migrated my javafx recently to maven. It starts and works fine, but i cannot figure out where to place the config folder and how to say maven that it shell include it.
Before i converted the project to maven, the structure looked like this:
|-src
|-lib
|-config
Now the sructure looks like this:
|-src/main/java/my.super.program
|-src/main/resources/my.super.program
|-lib
|-config
I tried it with
<resource>
<directory>config</directory>
<includes>
<include>*.xml</include>
</includes>
</resource>
but that hasn't worked.
In the main.java i declare the needed config files. For example:
private final String configPath = 'config/config.xml';
private final String accountPath = 'config/account.xml';
How can I include the config folder and tell maven to use it?
By default everything that is placed inside the src/main/resources is added to the jar file that is built. Alternatively you can create your own maven module holding the configuration. Again you will have to place it under src/main/resources
If you decide to create a dedicated module holding the configuration you will have to add it to the dependencies of the previous module. Preferably it will be a compile time dependence(this is the scope).
I hope this answer doesn't confuse you, if it does, probably just seek a simpler solution.
Jars are not a good place for storing mutable configuration. You can only put a default configuration in the jar file. A jar file is not writable, so your program can't overwrite the configuration to update it if it is in the jar.
For apps that require user specific configuration, I'd be inclined to store the config in a $USER_HOME/.myapp directory. An advantage of using the file system for configuration rather than a database or the java preferences API, is that the config can be easily viewed and edited using a standard text editor.
Sample code to determine a home directory for your application's config:
String configHome = System.getProperty("myapp.home",
System.getProperty("user.home") + "/.myapp"
);
You could access the resources for the default configuration out of the jar file and copy them to files on the file system. Then a user could manually edit the files on the file system to configure your application. Or, you could provide a custom UI that you create for the user to modify the info in the files.
An example of this approach is in this OsiamHome class, which is one of the nicest ways I have seen to setup configuration for a Java application (that's a server app, but the general principle could be used for a client application). The osiam reference is a Spring application so that it benefits from some of the configuration support provided by Spring, in particular the PathMatchingResourcePatternResolver, that helps to recursively search a jar to find all default configuration resources for the application.
However, you probably don't need the additional facilities provided by the Spring based approach. Instead, you can just keep a list in code of all the files in your default configuration and place the files in a /resources/config directory in your source tree. The build process will copy the default configuration into your jar file for deployment. When the application is run, if the config directory on the filesystem does not exist, the application can read the default config resources from your jar file and copy the config resources to files on the filesystem. The config files can then be modified and changed as much as necessary.
Some other options you have for managing the configuration outside of your application jar are to:
Place the configuration information in a database OR
Use the Java preferences API OR
Use a packaging system, e.g., rpm or (perhaps) the javapackager tool or javafx maven plugin which bundles the default configuration into a native deployment package. Native OS deployment software can extract the configuration and copy it to a location you specify. Usually, this location will be a standard configuration location for the OS, such as /etc/myapp on Linux for system wide config or $USER_HOME/.myapp for user specific config.
Related
I compiled a Java application into a WAR file, there is a configuration file inside the WAR file. The configuration file is required to change something after deployed to the production server, because it still contains the UAT server parameters.
However, I don't know where to edit the configuration file after deployed in Tomcat. Please help. Thanks.
It doesn't sound like a correct design. You should -
Load configuration file based on some System parameter (e.g. -Denvironment=UAT or PROD). This will be the decision factor for loading the right configuration file.
Do not package the file inside war itself, if possible externalize it to some other directory where amending is lot easier.
When I launch an application from bndtools (using a file launch.bndrun), I can specify -runproperties: java.util.logging.config.file=jul-debug.properties, with jul-debug.properties being a file in the working directory. Everything is fine and works as expected, i.e. the java.util.logging properties are read from my configuration file.
When I create an export (launch.jar) using this lauch.bndrun, the configuration file jul-debug.properties is not included in the launch.jar. Can I add some instruction to launch.bndrun that causes the jul-debug.properties to be included in laucn.jar in such a way that jar -jar launch.jar will cause the configuration file to be evaluated?
I looked through all the -run... instructions for bnd, but couldn't find anything.
I am coding a Java web web application packaged as war and I would like to add logging and specify the log folder to write log files to (using configuration file, e.g. logback.xml)
Obviously, I would not like to configure the absolute path of the folder. Now I wonder how to configure the log folder in war. What are the best practices and recommended approaches to this?
We use to use relative paths in logback.xml but changed to using an env property. When the path was relative we could never tell the customer exactly where the log file was due to different Java EE server implementations. Using an absolute path with an env variable made it easier. For example
<file>${user.dir}/logs/my_web_app.log</file>
How can I load the configuration information for hibernate dynamically from a config file. Netbeans currently hard codes that information into an xml file that is then compiled into the jar. I'm a newbie to Java/Netbeans coming from PHP land and am use to a central bootstrap that pulls from a .ini or something similar, but netbeans tends to hardcode this information upon generation of the models,etc in an xml file that is then compiled in the jar. I'm looking for conventional methods of setting up configuration for various client machines using various database configurations. I don't want to have to compile the app on each machine it must be installed on.
The configuration file is read using the Configuration class. By default, it uses the hibernate.cfg.xml file found in the classpath, but you can use the configure method taking a file as parameter, and store the config file on the file system rather than in the jar.
You can also put the static mapping, which never changes between configs, in a file inside the jar, and put the varying config inside an external file. Look at the javadoc for Configuration to know how to add resources and config files to the configuration.
I have a java desktop app and the issue of config files is vexing me.
What I want is for my distributable application folder to look like this:
MyApp/Application.jar
MyApp/SpringConfig.xml
MyApp/OtherConfig.xml
MyApp/lib
But at the moment SpringConfig.xml is inside Application.jar and I can't even find OtherConfig.xml programmatically.
I don't care how I set up the various files in my compilation path, so long as they end up looking like the above.
So..
where do i put the files in my dev setup?
and how do i access them programmatically?
thanks
the spring config file is related to the code and wiring of your application, hence it'd better be inside the jar, and should be subject to change by the users
(new File(".")).getAbsolutePath(); returns the absolute path of your jar - then you can load the OtherConfig.xml by a simple FileInputStream
if the SpringConfig.xml contains configuration data like database credentials, put them in an external application.properties and use a custom PropertyPlaceholderConfigurer to load the external file.
Answering the question "where do I put the files in my dev setup" is not possible because we don't know your environment.
Actually, if you want to be able to edit the config yourself (and not necessarily end-users), you can open the jar with any zip software (WinRAR for instance) and edit the config file from within the jar.
Update: as it seems you can't make the config files to be places out of the jar. Well, for a start, you can do it manually - whenever the .jar is complete, just remove the config file from inside and place it outside.
I typically create a structure where I have a src/ directory and then other directories exist at the same level. Some of those directories include:
lib/ - External Libraries
config/ - Configuration Files
resources/ - Various resources I use (images, etc)
At that same level, I then create an Ant script to perform my build so that the appropriate config files, resources, lib, etc are copied into my JAR file upon build. It has worked great for me up to this point and is a fairly easy to understand organizational structure.
Update: Accessing my config files is done, typically, by knowing their location and opening them up and reading them in the code. Because I use Ant to build, I make sure that my config files are in a location that I expect. So, for example, in a recent application I created, when I compile, my JAR file is in the top level directory (relative to the application release structure). Then, there is a "main" config file at that same level. And there is a "theme" config file that is in a themes folder.
To read the various files, I just open them up as I would any other file and read them in and go from there. It's nothing particularly fancy but it works well and it makes it easy to manually change configurations if I need to do so.
In dev mode, put them in source dir and they will be copied to your classes folder, you can then access them using classloader.
Example:
URL url = ClassLoader.getSystemResource("test.properties");
Properties p = new Properties();
p.load(new FileInputStream(new File(url.getFile())));
In Prod mode, you can make them part of your jar.