'how to add external file to maven package output jar during runtime?

I have a spring maven project. In that Project (root folder), I have folder called config which contains multiple folder like t1, t2, t3 etc, Inside of each folder, I have spring-config.xml (config differs to each test environments)

while I package my project using maven package, I need spring-config.xml to be available in classpath of my output jar.

(I can do this by copying corresponding config file to src/main/resources), But I want all of my config files to be available in root directory, and maven should pick correct config file while packaging.

Is this possible to do while runtime(during maven command or by using any maven pluggins)

Note: To create fat-jar I'm using maven-assembly-plugin

<plugin>
              <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>first</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <!-- ... -->
                            <finalName>${project.build.finalName}-${jarName}</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                            <archive>
                                <manifest>
                                    <mainClass>com.main.MainClass</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>

                        </configuration>
                    </execution>
                </executions>

            </plugin>


Solution 1:[1]

I've solved by using <resources>

<properties>
        <org.springframework.version>2.5.5</org.springframework.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
</properties>

 <build>
        <finalName>myApp</finalName>
        <resources>
            <resource>
                <directory>config/${folderName}</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        //... pluggins
</build>

while execute mvn command I can pass folderName (t1,t2,t3..etc)

mvn clean package -DfolderName=t1 mvn clean package -DfolderName=t2.. etc

Now it is locating correct file based on my inputs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1