Search code examples
mavenspring-bootmaven-profiles

Activate a Maven profile by setting VM arguments


I am currently writing an application in Spring Boot with STS.

Now I have different dependencies which I want to filter in case of:

  1. running the application locally inside STS as a spring boot application.

  2. building an artifact for deployment on a server

I split the dependencies with profiles in the pom.xml like this:

<profiles>
  <profile>
    <id>local</id>
    <activation>
      <activateByDefault>false</activateByDefault>
    </activation>
    <dependencies ...
  </profile>
  
  <profile>
    <id>server</id>
    <activation>
      <activateByDefault>true</activateByDefault>
    </activation>
    <dependencies ...
 </profile>

My question is how can i activate the local profile in the run configuration as vm arguments when i start the local spring boot application? Because of the activateByDefault true setting at the other profile it cannot start the app because some dependencies for the local start are excluded.

edit: I want to change the profile in the launch configuration of the app. There is no maven build so i cannot use the -P flag.

Adding

<activation>
  <property>
    <name>act</name>
    <value>local</value>
  </property>
</activation>

and start with vm arguments -Dact=local does not work.

run configuration


Solution

  • You cannot activate different build profile (as expressed in pom.xml) during run time. By the time you run your application it is too late, since it has been built before it runs.

    In other words you can use maven profiles to create different builds.

    You can alter your application's run time behaviour (as suggested by @pvpkiran) by using Spring profiles.