javamavenbukkit

Is there a way to save multiple config files in a sub-folder of the plugin-folder (Spigot & Maven)


I have multiple language files in one directory inside my project. Those language files are basically custom config-files, which I want to save as well inside a sub-folder in my plugin-folder.

I have edited my pom.xml to implement the .yml files into the compressed .jar

<resource>
    <directory>${basedir}/src/main/resources/languages</directory>
    <includes>
        <include>*.yml</include>
    </includes>
</resource>

This is working so far, but when I save the config-files,

File file = new File(Main.getPlugin().getDataFolder(),
                ConfigManager.getConf().getString("Settings.language") + "_lang.yml");
if (!file.exists()) {
   System.out.println("This language file does not exist!");
   file.getParentFile().mkdirs();
}
Main.getPlugin().saveResource(file.getName(), true);

conf = YamlConfiguration.loadConfiguration(file);

they won't be saved inside a subfolder, when the plugin loads.

I have tried to tell the File() function, that it should use a different location such as

File file = new File(Main.getPlugin().getDataFolder() + System.getProperty("file.separator") + "languages", 
ConfigManager.getConf().getString("Settings.language") + "_lang.yml");

but it didn't work.

I could use the current way, that the language-files won't get saved inside a sub-folder, but with the time and more lang-files, it'll get very confusing, if there are too many files.

If this method I'm using is "a stupid way", I'm fine, if somebody else could tell me a way how to write a better "language-switcher".


Solution

  • Okay, I found just found the solution by myself.

    I had to add an additional maven-plugin to copy the language files into the language-directory according to this stackoverflow post

    At the end, I will show you the changes I have made.

    <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.1</version>
        <executions>
            <execution>
                <id>copy-resources</id>
                <phase>validate</phase>
                <goals>
                    <goal>copy-resources</goal>
                </goals>
                <configuration>
                    <outputDirectory>${basedir}/target/classes/languages</outputDirectory>
                    <resources>
                        <resource>
                            <directory>${basedir}/src/main/resources/languages</directory>
                            <filtering>true</filtering>
                        </resource>
                    </resources>
                </configuration>
            </execution>
        </executions>
    </plugin>