Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

If you've ever worked on a PL/SQL project or any project with a good amount of database related work you probably have had to deal with multiple SQL files and their deployment across systems. This is exactly what I've come across recently. Even in the early stages of my PL/SQL project I had 11 separate SQL files that would need to be deployed to system and performance test environments, then propagated to production, production support, etc. To help with managing all this I decided that building a single 'super SQL' file would work best and I ended up using Maven to do it.
sqldev_ddl.png


Maven isn't exactly suitable for PL/SQL or SQL development, but it is possible to reuse its concepts and apply them in this case anyway. I went about creating the Maven directory structure as per usual approach, my SQL files went into ${project.basedir}/src/main/sql and then I had a pom.xml in the project root directory.

I didn't actually want to do anything too smart with Maven. All I was after was the ability to merge the individual SQL files into one big super SQL file. This file would be the distributable that would get applied across various environments.

After lots of reading about the easiest way to merge files in Maven I hit a bit of a roadblock since most of the focus was on merging ZIP, JAR or Property files, not what I was after. Eventually I found the Maven Merge Plugin which did exactly what I wanted, a simple merge (concatenation actually) of files. So that's what I went with.

I had a look at the source code for the plugin and didn't really like its 'how to run' part so ended up doing something different in my pom.xml file. I simply bound the plugin 'merge' goal to the 'package' phase so to run it all I had to do was...
 Maven
mvn clean package




This is what my pom.xml looked like...
 Maven POM
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>net.igorkromin</groupId>
<artifactId>SqlMergeExample</artifactId>
<name>${project.artifactId}</name>
<version>1.0</version>
<description>SqlMergeExample</description>
<packaging>pom</packaging>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}</outputDirectory>
<plugins>
<plugin>
<groupId>org.zcore.maven</groupId>
<artifactId>merge-maven-plugin</artifactId>
<version>0.0.3</version>
<executions>
<execution>
<id>merge</id>
<phase>package</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<mergers>
<merger>
<target>${project.build.outputDirectory}/super.sql</target>
<sources>
<source>src/main/sql/tbl_mytable1.sql</source>
<source>src/main/sql/pkg_mypackcage1.sql</source>
...
</sources>
</merger>
</mergers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>


You can see that packaging is set to POM, that is because we are not compiling any Java, etc. The rest is fairly straight forward, there is no dependency section like described in the plugin's readme, that's not required - Maven will figure it out for you.

The main part is really all those lines. The files are concatenated together using the order that they appear in the pom.xml file.

That's all there is to it really, just list all the files, run the package phase and you will have a super SQL file in the usual target directory.

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.