Usage

Configuring the plugin

To start using the plugin you will need to configure the repository location for the plugin.

<project>
  ...
  <pluginRepositories>
    <pluginRepository>
      <id>croche-releases</id>
      <url>http://croche.googlecode.com/svn/repository/releases</url>
    </pluginRepository>
  </pluginRepositories>
  ...
</project>

Merge Configuration

To configure the merged files you have to add one or more merges which represents the merge of a set of files into a single target file.

  • Within each merge you should add a targetFile which is the location of the end result file where all files being merged are written to. This target file will be recreated each time the plugin is ran.
  • You will also need to add one or more sourceDir which is the directory where it should scan for files to merge. NOTE that it will scan into not just the source directory but also all of its subdirectories too.
  • You can optionally add one or more include which are the sub strings the file name must contain in order to be included in the files that get appended. If you do not specify any includes then it will append all files found in the source directories and their sub-directories.
  • If you want to add a separator to be added above each merged file then you can add a separator. In the separator 3 variables are supported: \n will translate into a line break in the output file, #{file.name} will be replaced with the name of the file that is being appended into the target file, #{parent.name} will be replaced with the directory name containing the file that is being appended into the target file.
  • You can control the order that files found are appended to the target file. To do this add the nameContainsOrderings configuration. Each nameContainsOrdering defines a substring that the file name must contain to group files in order relative to each other nameContainsOrdering in the list. Any found files that do not match any of the nameContainsOrdering will be added last. For example if your list of nameContainsOrderings contained intro, body and footer in that order then any files with intro in their name would be appended first, followed by any files with body in their name and finally followed by any files with footer in their name. Any files that had neither intro, body or footer in their name would be appended last.
  • The default encoding used to read and write the files is UTF-8 but if you want to change this then you can add the encoding element.

Basic Merge Configuration Example

<project>
  ...
 <build>
  <plugins>
   <plugin>
        <groupId>croche.maven</groupId>
        <artifactId>maven-merge-file-plugin</artifactId>
        <executions>
                <execution>
                        <id>merge-release-notes</id>
                        <phase>process-resources</phase>
                        <goals>
                                <goal>merge</goal>
                        </goals>
                        <configuration>
                                <merges>
                                        <merge>
                                                <targetFile>docs/all-release-notes.txt</targetFile>
                                                <sourceDirs>
                                                        <sourceDir>docs</sourceDir>
                                                </sourceDirs>
                                                <includes>
                                                        <include>release-notes.txt</include>
                                                </includes>
                                        </merge>
                                </merges>
                        </configuration>
                </execution>
        </executions>
        </plugin>
   </plugins>
 </build>
  ...
</project>

The example configuration above merges release notes files in the docs directory into a single release notes file all-release-notes.txt, for example if there were files called component1-release-notes.txt, component2-release-notes.txt, component3-release-notes.txt in this directory it would append them together.

Detailed Merge Configuration Example

<project>
  ...
 <build>
  <plugins>
   <plugin>
        <groupId>croche.maven</groupId>
        <artifactId>maven-merge-file-plugin</artifactId>
        <executions>
                <execution>
                        <id>merge-sql</id>
                        <phase>process-resources</phase>
                        <goals>
                                <goal>merge</goal>
                        </goals>
                        <configuration>
                                <merges>
                                        <merge>
                                                <targetFile>database/install-all.sql</targetFile>
                                                <sourceDirs>
                                                        <sourceDir>database</sourceDir>
                                                </sourceDirs>
                                                <nameContainsOrderings>
                                                        <nameContainsOrdering>recreate-schema.sql</nameContainsOrdering>
                                                        <nameContainsOrdering>create-schema.sql</nameContainsOrdering>
                                                        <nameContainsOrdering>schema-objects</nameContainsOrdering>
                                                        <nameContainsOrdering>indices</nameContainsOrdering>
                                                        <nameContainsOrdering>data</nameContainsOrdering>
                                                </nameContainsOrderings>
                                                <includes>
                                                        <include>.sql</include>
                                                </includes>
                                                <separator>-- ***** SQL File: #\{parent.name\}/#\{file.name\} *****\\n\\n</separator>
                                                <encoding>UTF-8</encoding>
                                        </merge>
                                </merges>
                        </configuration>
                </execution>
        </executions>
        </plugin>
   </plugins>
 </build>
  ...
</project>

The example configuration above merges any SQL files in the ${project.build.directory}/database and any of its subdirectories into a single SQL file ${project.build.directory}/database/install-all.sql. It would first append any files with recreate-schema.sql in their name, followed by create-schema.sql in their name, followed by schema-objects in their name, followed by indices in their name and finally followed by data in their name. It adds a SQL comment at the top of each appended file. I use this where I have several EAR projects that main their own SQL scripts for creating a schema that they use. Within each of these projects they have a script to drop/create the database schema, the objects (tables, views, functions, procedures), additional column indices, and some initial static data. The configuration above merges them into a single install SQL script that I can use to setup the database from scratch where I want the drop/create schema queries to run first, followed by the create tables, then the create indices and finally the static data inserts.