Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize Maven command when deploying to Heroku

We generate documentation from our tests with Spring REST Docs. When deploying the application to Heroku, by default tests are skipped, as I can see clearly in the logs:

-----> Java app detected
-----> Installing JDK 1.8... done
-----> Installing Maven 3.3.9... done
-----> Executing: mvn -DskipTests clean dependency:list install
[INFO] Scanning for projects...
[INFO]   

How can I customize the Maven command that is executed by Heroku?

like image 752
KenCoenen Avatar asked Sep 20 '25 03:09

KenCoenen


1 Answers

After a day of searching, I found two possible ways to enable tests for the Heroku build:

  1. Configure Maven Surefire Plugin in your pom.xml
  2. Customize Heroku Java Buildpack

Maven Surefire Plugin

Configuration in your pom.xml overrides command-line arguments when your Maven process is started.

You can undo -DskipTests by disabling skipTests in the Maven Surefire Plugin configuration as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skipTests>false</skipTests>
    </configuration>
</plugin>

Please note that Heroku adds environment variables when running Maven. Make sure you use an in-memory database when running in the Spring testprofile, otherwise the build will fail.

In my case, I use Flyway for database migrations. For some weird reason, regardless the spring.datasource.url in my application-test.yml, Flyway used the environment variable value. If you have the same issue, please note that you can override them too with the argLine configuration property of the Maven Surefire Plugin.

<argLine>-DDATABASE_URL=jdbc:h2:mem:questflair -DJDBC_DATABASE_URL=jdbc:h2:mem:questflair -DSPRING_DATASOURCE_URL=jdbc:h2:mem:questflair</argLine>

Heroku Java Buildpack

According to the Heroku Java Buildpack documentation, you can customize Maven options and goals.

I didn't try this solution, see Customize Maven for more information.

like image 90
KenCoenen Avatar answered Sep 21 '25 19:09

KenCoenen