Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

configure maven profiles in spring boot @activeprofile annotation

I have been reading a lot and can't find a solution that tells me how to inlucde the maven profiles in @activeprofiles annotation . Is it possible or not?

The issue that I am trying to solve to get H2 and flyway start before my tests execute which is not happening. The configuration is decscribed in maven profiles in pom.xml. When the tests run in teamcity it picks the maven profile and execute but as standalone they can't find the configuration for H2 and flyway and fail on starting.

like image 378
worrynerd Avatar asked Jun 19 '15 23:06

worrynerd


1 Answers

... can't find a solution that tells me how to inlucde the maven profiles in @activeprofiles annotation.

Do you mean to active Spring profiles based on maven profile, if so, you can configure it like so in your pom.xml:

<profiles>
  <profile>
    <id>mvnprofile</id>
    <properties>
      <spring.profiles.active>springprofile</spring.profiles.active>
    </properties>
    <dependencies>
      <dependency>
      </dependency> 
    </dependencies>
  </profile>
  ...
</profiles>

In your test class configure the profiles it should run on ..

@Runwith(..)
@SpringApplicationConfiguration(...)
@ActiveProfiles("springprofile")
public class YourTest { ... }

For profile specific properties, create an application-springprofile.properties in addition to your application.properties, Spring Boot will first load application.properties then load application-springprofile.properties -- overriding any properties previously configured from application.properties.

Finally, you set the maven profile with -P flag

$mvn test -P mvnprofile 
like image 110
ikumen Avatar answered Oct 05 '22 23:10

ikumen