简体   繁体   中英

Migrate from jars in flyway maven plugin

Is it possible to migrate from jars in maven flyway plugin? I have no problems with sqls and java (compiled to class) but no success with jars. Classpath is set correctly.

Ok, i've debugged the source code. Jar needs a special protocol that is being provided to it when it is placed in /jars catalog in flyway command line tool. There is no such an equivalent in a flyway maven plugin.

This is a slight workaround to the limitation of the flyway-maven-plugin executing from a jar artifact file containing multiple flyway SQL files.

Create a profile

  1. Use the 'maven-dependency-plugin:unpack' to explode the content of your jar file to specific directory.
  2. Run 'flyway-maven-plugin' with a 'location' limited to the extracted directory.
  3. Not very pretty but works.

This is my sample profile

<profiles>
    <profile>
        <id>flyway</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>3.1.1</version>
                    <executions>
                        <execution>
                            <id>copy</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>unpack</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.abc</groupId>
                                <artifactId>flyway</artifactId>
                                <version>1.0.0-SNAPSHOT</version>
                                <type>jar</type>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.directory}/jars</outputDirectory>
                                <destFileName>my-flyway.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>true</overWriteSnapshots>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.flywaydb</groupId>
                    <artifactId>flyway-maven-plugin</artifactId>
                    <version>${flyway.version}</version>
                    <configuration>
                        <sqlMigrationSeparator>__</sqlMigrationSeparator>
                        <locations>
                            <location>filesystem:./target/jars/my-flyway.jar</location>
                        </locations>
                        <url>${flyway.url}</url>
                        <user>${flyway.user}</user>
                        <password>${flyway.password}</password>
                        <schemas>
                            <schema>my_schema</schema>
                        </schemas>
                        <baselineOnMigrate>true</baselineOnMigrate>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.postgresql</groupId>
                            <artifactId>postgresql</artifactId>
                            <version>${postgresql.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

The maven command line is then

mvn -P flyway clean process-resources flyway:migrate 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM