This project purpose is to be a Work Sample - A tiny lib to load/save configurations from different types of repositories.
- Settings are represented by ConfigurationEntry-Objects
- Class
TextFileRepository
for load/save Configuration-Settings inside a TextFile.
- Java 17
- Junit 5.9.1
- Mockito 5.4.0
You can install the ConfigFileManager-.jar file to your local Maven repository. From there, maven can get the lib the same way as from the official repository. This makes it easy to define dependency inside the pom.xml file.
-
Install ConfigFileManager to your local Maven repository
To install the lib-jar to your local maven repository, you have to use
org.apache.maven.plugins:maven-install-plugin:2.5.2
plugin. You need to use theinstall-file
goal to copy the jar into your local maven repository. Exact command will only work if the .jar file was created by using maven'spackage
lifecycle. By using it, a pom.xml file is getting stored inside the jar'sMETA-INF
folder, containing the necessary information forinstall-file
goal to work.mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile="/path/to/ConfigFileManager-0.3.0.jar"
-
Add the ConfigFileManager dependency to your projects pom.xml
<dependency> <groupId>de.mme</groupId> <artifactId>ConfigFileManager</artifactId> <version>0.3.0</version> </dependency>
-
Type CLI-command for reloading Mavens project dependencies
mvn -f <PathToProjectsPomXml> dependency:resolve
The Configuration class handles all settings for you. It will collect all settings. A setting has a name and a value and is represented by a ConfigurationEntry object. A Repositories save method takes a configuration and stores its values inside the repo.
// Create a Configuration Object. This will manage all ConfigurationEntry for you
Configuration config = new UniqueConfiguration();
config.setEntry("MySetting","MyValue");
config.setEntry("MySetting2","MyValue2");
// Write the Configuration-Object to a repository.
ConfigurationRepository fsr = new TextFileRepository(Path.of("exampleConfig.cfg"));
fsr.save(config);
// Create textfile repository to get access to the configuration textfile
ConfigurationRepository fsr = new TextFileRepository(Path.of("exampleConfig.cfg"));
Configuration config = fsr.load();
// Use some settings
String settingName = config.getEntry("MySetting2").getName();
String settingValue = config.getEntry("MySetting2").getValue();
System.out.println("Setting Name is " + settingName + " and it's value is " + settingValue);
If you want to build the ConfigfileManager .jar file by yourself, its necessary to use
mavens package
lifecycle. It will install a pom.xml file inside the jar's META-INF Folder.
This pom.xml file is necessary for installing the lib to the local
maven repository as described in the Get Started chapter before.
# Comment lines start with hash-symbol
settings.audio=Values can be any String.
settting.video=Values have to end with normal Linebreak \n
# Empty lines are also allowed
MySetting=MyValue
OnlyKeysReturnNull=
=OnlyValuesAreNotAccessible
DoubleKeys=If key exists more than once,...
DoubleKeys=...the last value will be taken.