Exclude application.properties when creating a war using spring boot and spring-boot-maven-plugin

I am using web application development with spring boot and want to generate war instead of jar.

It works very well, using the jar to war conversion described here: http://spring.io/guides/gs/convert-jar-to-war/

But I want to exclude application.properties from the war, because I use @PropertySource(value = "file:${OPENSHIFT_DATA_DIR}/application.properties") to get the file path in the production environment.

  • This method works when creating my war, but in eclipse I cannot run the application because application.properties is not copied at all for the purposes / classes:

    <build> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>application.properties</exclude> </excludes> </resource> </resources> </build>

  • This method does not work at all, I think spring-boot-maven-plugin does not support packageExcludes:

    <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <packagingExcludes>WEB-INF/classes/application.properties</packagingExcludes> </configuration> </plugin> </plugins> </build>

Do you have any other suggestion?

Thnks

+5
source share
6 answers

Try using the solution below. This will work:

 <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>**/*.properties</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> 

If you use the solution above, when you start the project in the Eclipse IDE, you may receive an error message that the properties file was not found. To get rid of this, you need to add the resource folder to the "Run from configuration". (Run configurations ... β†’ Classpath β†’ User entries β†’ Advanced ... β†’ Add folders)

+5
source

The solution I added is to unzip my packaged war, delete the application.properties file and create a new war called ROOT.war using maven-antrun-plugin and run some ant tasks.

this is what i added to my plugins in pom.xml:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <id>package</id> <phase>package</phase> <configuration> <target> <unzip src="target/${artifactId}-${version}.${packaging}" dest="target/ROOT/" /> <delete file="target/ROOT/WEB-INF/classes/application.properties"/> <zip destfile="target/ROOT.war" basedir="target/ROOT" encoding="UTF-8"/> <delete dir="target/ROOT"/> </target> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> 

I named my target war as ROOT.war because I use tomcat in openhift PaaS, so I just copy my ROOT.war and click on my openbift repo. What he

+2
source

As I understand from your question, you want to use application.properties for your development. But you do not want to use it for production.

I would suggest using Spring profiles to achieve this. In the properties file

  • Create a profile for development. Put all your development properties in it.
  • Do not create a profile to create in the properties file.
  • When you are developing, set the active profile for development so that properties are loaded from your application.properties file.
  • When you run it in the production process, set the active profile to "Production". Although application.properties will be loaded into your WAR, since there is no profile for production, none of the properties will be loaded.

I did something similar using YML. I am sure there should be a way to do the same using the .properties file.

 spring: profiles.active: development -- spring: profiles: development something: location: USA unit1: Test1 unit2: Test2 

You can change the profile at runtime using

 -Dspring.profiles.active=production 
+2
source

Try using this solution:

 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring.version}</version> <configuration> <addResources>false</addResources> </configuration> </plugin> 

<addResources> false </addResources> will save properties when starting mvn spring-boot: run

+1
source

When starting in Eclipse in your startup configuration, you need to specify the path to the Spring Boot proposition:

 --spring.config.location=${workspace_loc:/YOURPROYECTNAME}/src/main/resources/ 
+1
source

Preserves application.properites resource even after creating jar package even with <addResources>false</addResources>

0
source

Source: https://habr.com/ru/post/1206194/


All Articles