7

We have a Spring Boot application, and in it we have both an application.properties and a application-dev.properties. The application properties file just has some across the board default values, and the application-dev properties has some really rudimentary credentials in it.

When we deploy our jar, the application gets its properties from the environment its deployed to so, we don't utilize the profiles feature except when we want to develop locally against our 'dev' environment.

Our secops department has told us that they no longer want us to include that dev properties file from the final jar that's deployed to production, but they have ok'd us leaving the file in version control so we can at least track changes and build/run locally.

I've been reading through the spring-boot-maven-plugin configuration, and I can't see a way to just tell maven to exclude that file when building the final jar while still copying it into the target directory so our team can still do their hacking locally.

4
  • Possible duplicate of Maven: excluding java files in compilation Commented Mar 21, 2019 at 20:26
  • @jordiburgos Not quite, I still want maven to copy the file into the /target directory so when I'm running the application locally I can utilize spring's profile feature. I just want it excluded from the generated package (currently jar) Commented Mar 21, 2019 at 20:29
  • You can combine the exclusion of files and profiles (one for dev and one for deployment) Commented Mar 21, 2019 at 20:30
  • Is that the simplest way to do it? I don't mind utilizing maven profiles, which would definitely get the problem solved but I'd honestly rather not if I can just say "don't package file this in the jar". Most of our team isn't very CLI savvy and prefer various UI configurations through their IDE, which isn't always consistent (one dev might click a checkbox and forget which breaks that person's build for the better part of a day until we find out what they did). I might have to face the music and do that anyway, but I was hoping there was a way to do this. Commented Mar 21, 2019 at 20:39

1 Answer 1

17

I messed around with profiles for a bit, but I found that it was too confusing to explain to others and became a little cumbersome as additional permutations of scenarios came about.

Ultimately the simplest thing is to configure the jar plugin to exclude the file. Here is a snippet from our pom.

<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.1</version> <configuration> <excludes> <!-- This is where the exclusion occurs --> <exclude>**/application-*.properties</exclude> </excludes> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 
Sign up to request clarification or add additional context in comments.

3 Comments

Exactly what is was looking for i have to exclude application-dev.properties files from final jar packaging, so after this setting when i am running application from intellij dev is included but when i build with mvn clean install dev is excluded. Great Work!!
How should I exclude some package of my app and application classes?
I combined your approach with my approach using profiles, so that this exclusion only happens when I use my dedicated "prod" profile for building the prod version of my artifact. This way a default build using mvn install also creates a runnable jar because the application-dev ist still part of the jar (for the case that someone wants to run the app purely using maven). Anyway, great solution you provided and exactly what I was looking for. Thank you so much for sharing! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.