0

I have a Maven project with Liquibase that I want to run by executing the jar file. Currently, I can only make it work during the build phase using version 3.10.3 of the Liquibase Maven plugin. I tried using newer versions of the plugin, but I couldn't make it work.

Here's my current pom.xml file:

<project>
  <!-- project details -->
  
  <properties>
    <liquibase.propertyFile>${project.basedir}/src/main/resources/liquibase.properties</liquibase.propertyFile>
  </properties>
  
  <dependencies>
    <!-- dependencies -->
  </dependencies>
    
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <!-- assembly plugin details -->
      </plugin>
      <plugin>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-maven-plugin</artifactId>
        <version>3.10.3</version>
        <configuration>
          <propertyFile>${liquibase.propertyFile}</propertyFile>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>update</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>3.1.2</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

How can I make Liquibase work when I execute the jar file instead of during the build phase? And is there a way to make it work with newer versions of the Liquibase Maven plugin?

I believe it can be done using exec-maven-plugin, but so far I haven't been able to do it.

Thank you in advance for your help!

veljkost
  • 1,748
  • 21
  • 25

1 Answers1

1

My mistake was using the liquibase-maven-plugin plugin. You only need to use the core of Liquibase in the dependencies section and manage the updates defined in db.changelog.xml programmatically using the APIs provided by Liquibase.

public static void main(String[] args) {

    Database database;
    try {


        String url = getLiquibasePropertiesInfo().getUrl();
        String username = getLiquibasePropertiesInfo().getUsername();
        String password = getLiquibasePropertiesInfo().getPassword();
        String driver = getLiquibasePropertiesInfo().getDriver();
        String changeLogFile = getLiquibasePropertiesInfo().getChangeLogFile();

        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            Class.forName(driver);
            // Create the Liquibase instance and update the database
            database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
            Liquibase liquibase = new Liquibase(changeLogFile, new ClassLoaderResourceAccessor(), database);
            liquibase.update("");
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83