As plugin
is an element allowed in a Profile put your plugin inside the profile, and declare the executions
you want in the appropriate profile.
Executing maven tasks based on chosen profile
Question
I am currently developing my first JAX-RS based REST service and in order to access different databases based on the environment I am executing the service on, I added profiles to my pom.xml
. Based on the currently chosen profile I am able to create a configuration file, that lets me establish the connection to the local database.
However, I also use this information to create a database and tables and load some test data into it using dbunit. The problem is, that the database is created and dropped for all profiles, but I just want to recreate the database for test profiles. If I use production or development profiles the database should just be altered if there have been structural changes to the database tables.
So my question is how I could execute plugin actions based on the currently chosen profile. Therefore please consider the example below. How to alter the executions so they are just executed if I set the profile test
using maven clean install -P test
?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>${sql.maven.plugin.version}</version>
<!-- Specify the dependent jdbc driver -->
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.jdbc.version}</version>
</dependency>
</dependencies>
<!-- Common configuration shared by all executions -->
<configuration>
<driver>${database.jdbc.driverClass}</driver>
<url>${database.jdbc.connectionURL}</url>
<!-- You can comment out username/password configurations and have maven
to look them up in your settings.xml using ${settingsKey} -->
<username>${database.jdbc.username}</username>
<password>${database.jdbc.password}</password>
<settingsKey>postgres-db</settingsKey>
<!-- All executions are ignored if -Dmaven.test.skip=true -->
<skip>${maven.test.skip}</skip>
</configuration>
<executions>
<execution>
<id>drop-db-before-test-if-any</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<!-- need another database to drop the targeted one -->
<url>jdbc:postgresql://localhost:5432</url>
<autocommit>true</autocommit>
<sqlCommand>DROP DATABASE ${database.jdbc.dbName};</sqlCommand>
<!-- ignore error when database is not available -->
<onError>continue</onError>
</configuration>
</execution>
<execution>
<id>create-db</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<url>jdbc:postgresql://localhost:5432</url>
<!-- no transaction -->
<autocommit>true</autocommit>
<sqlCommand>CREATE DATABASE ${database.jdbc.dbName};</sqlCommand>
</configuration>
</execution>
<execution>
<id>create-tables</id>
<phase>process-test-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<autocommit>true</autocommit>
<delimiterType>row</delimiterType>
<srcFiles>
<srcFile>src/main/sql/spozz-schema.sql</srcFile>
</srcFiles>
</configuration>
</execution>
</executions>
</plugin>
Solution