Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using liquibase file paths via both maven and spring

I update scheme and initial data in spring context using the following beean:

<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
    <property name="dataSource" ref="dataSource" />
    <property name="changeLog" value="classpath:db/changelog/db.changelog-master.xml" />
    <property name="dropFirst" value="true" />
</bean>

I also use Maven liquibase plugin to generate sql scripts in order to see what tables are created and etc.

 <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>2.0.5</version>
                <configuration>
                    <!--mvn initialize liquibase:updateSQL-->
                    <propertyFile>src/main/resources/db/config/liquibase-gensql-data-access.properties</propertyFile>
                    <changeLogFile>src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>

                </configuration>
           </plugin>

The db.changelog-master.xml file includes child liquibase changelog files. The problem, how to refer to them from the master. When I use Spring I have to use the following path via classpath:

<include file="classpath:/db/changelog/db.changelog-1.0.xml"/>

When Maven is used, the path is:

<include file="src/main/resources/db/changelog/db.changelog-1.0.xml"/>

I'd like to have the same configuration for both cases. How can I archive it?

like image 557
Alexandr Avatar asked May 17 '13 08:05

Alexandr


People also ask

What is Liquibase in Maven?

Liquibase is an open-source library for managing database schema changes; it's database independent, so it works with any supported database engine. Liquibase provides a Maven plugin to run database operations during the build process.


2 Answers

I think if you change your Maven path from

<changeLogFile>src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile>

to

<changeLogFile>db/changelog/db.changelog-master.xml</changeLogFile>

and update db.changelog-master.xml file for all included files to use path relative to src/main/resources directory, it will fix the problem.

I solved this problem by using the same path to changeLog files in Spring, maven and integration test which call Liquibase. All my changelog files are located under /src/main/resources/db directory in one of the Maven modules within a project.

Maven profile which runs Liquibase, notice path: db/masterChangeLog.xml

<plugin>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-maven-plugin</artifactId>
                    <version>3.0.2</version>

                    <executions>
                        <execution>
                            <id>*** Install a last major release version of db ***</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>update</goal>
                            </goals>
                            <configuration>
                                <changeLogFile>db/masterChangeLog.xml</changeLogFile>
                                <contexts>dbBuildContext, dmlDevContext</contexts>
                                <propertyFile>db/liquibase-${user.name}.properties</propertyFile>
                                <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                                <logging>debug</logging>
                            </configuration>
                        </execution>

db/masterChangeLog.xml file includes these files:

<include file="db/install.xml"/>
<include file="db/update.xml"/>

db/install.xml file includes other changelog files (so does update.xml):

<includeAll path="db/install/seq"/>
<includeAll path="db/install/tab"/>
<includeAll path="db/install/cst"/>
<includeAll path="db/latest/vw"  />

Spring context executes the same set of db scripts upon app startup as follows:

<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
    <property name="dataSource" ref="baseCostManagementDataSource" />
    <property name="changeLog" value="classpath:db/masterChangelog.xml" />
    <property name="contexts" value="dbBuildContext, dmlDevContext" />
</bean>
like image 119
Igor Vash Avatar answered Sep 20 '22 15:09

Igor Vash


I commented on Igor's answer, his solution does not seem to work.

In order to solve this, I just pushed a patch to Liquibase: https://github.com/liquibase/liquibase/pull/187. This should be merged in 3.0.6-SNAPSHOT and therefore shortly available in 3.0.6.

With this change, you can now configure SpringLiquibase with this additional line:

<property name="ignoringClasspathPrefix" value="true" />

Another example/usecase requiring this change can be found here: https://github.com/LateralThoughts/spring-liquibase-extensions.

like image 40
fbiville Avatar answered Sep 16 '22 15:09

fbiville