根据所选的配置文件执行maven任务(Executing maven tasks based on chosen profile)

编程入门 行业动态 更新时间:2024-10-26 15:13:09
根据所选的配置文件执行maven任务(Executing maven tasks based on chosen profile)

我目前正在开发我的第一个基于JAX-RS的REST服务,为了根据我正在执行服务的环境访问不同的数据库,我将配置文件添加到我的pom.xml 。 基于当前选择的配置文件,我能够创建配置文件,这使我可以建立与本地数据库的连接。

但是,我还使用此信息创建数据库和表,并使用dbunit将一些测试数据加载到其中。 问题是,为所有配置文件创建和删除数据库,但我只是想为测试配置文件重新创建数据库。 如果我使用生产或开发配置文件,则只有在对数据库表进行结构更改时才应更改数据库。

所以我的问题是如何根据当前选择的配置文件执行插件操作。 因此,请考虑以下示例。 如果我使用maven clean install -P test设置配置文件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>

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>

最满意答案

由于plugin是Profile中允许的元素,因此将插件放在配置文件中,并在相应的配置文件中声明所需的executions 。

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.

更多推荐

本文发布于:2023-08-06 22:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:配置文件   所选   maven   Executing   profile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!