Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot exclude properties file and provide it to jar on execution

I have multiple set of properties file smtp, db, common settings under src/main/resources directory and my project is maven project jar packaging. I want to exclude all these properties file from jar building and place all these properties files to target/conf folder. Now i want to provide all these configuration file to my jar when i execute application jar. How to achieve this? I have done couple of things in pom.xml file to exclude all these properties and now unable to provide them to the generated jar. This is for exclude src/main/resources content

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <excludes>
            <exclude>*.properties</exclude>
        </excludes>
        <filtering>false</filtering>
    </resource>
</resources>

Spring boot Plugin configuration

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>

Maven resources plugin to place all the resources files under target/conf folder

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>target/conf</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>
like image 805
Umair Avatar asked Dec 12 '25 05:12

Umair


1 Answers

As far as I know spring-boot-maven-plugin doesn't support copy/move files (check out the documentation), you can do that using maven-resources-plugin-3.0.2.

With the following example you can exclude all properties files from src/main/resources and move them to target/conf. Here the example:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>*.properties</exclude>
            </excludes>
            <filtering>false</filtering>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/conf</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

UPDATE

Spring boot provide a set of environment properties (spring boot documentation), you can add spring-boot-maven-plugin artifact to the plugins.

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    ...
</plugins>

Each property file is separated by commas ,. Here is the syntax to run from the command line:

java -jar myJar.jar --spring.config.location=file:/path/location/prop1.properties,file:/path/location/prop2.properties

or

java -jar myJar.jar --spring.config.location=classpath:prop1.properties,classpath:prop2.properties

For example, the final instruction with full path would be:

java -jar MY_GENERATED.jar --spring.config.location=file:C:\Users\Admin\workspace\myProject\target\conf\application.properties,file:C:\Users\Admin\workspace\myProject\target\conf\jdbc.properties

If you run from target folder isn't necessary specify the full path:

java -jar MY_GENERATED.jar --spring.config.location=file:conf\application.properties,file:conf\jdbc.properties

For more info check out this

like image 136
JUAN CALVOPINA M Avatar answered Dec 14 '25 19:12

JUAN CALVOPINA M