Search code examples
sqlmaven-3publishingartifact

Release a SQL artifact in Maven


How do I tell Maven to also publish the SQL artifact for the DBA?

Here's the thing: when we release every new version of our Maven application, we need to publish two artifacts:

  • The web application (e.g. app-1.2.0.war file) -- for the WebSphere guy.
  • The database changes for this version (e.g. dba-1.2.0.sql file) -- for the DBA.

The SQL changes file is currently src/main/database/dba.sql, but I can change that dir or file name if necessary.

As of now Maven publishes the war artifact automatically (mvn clean deploy) to the artifact repository, and that's perfect. However, I wanted it to publish the SQL file at the same time, in the same command as well... and it doesn't.

How can I do that?

I see that we can tell Maven to publish extra artifacts (e.g. sources, javadoc) at once, so I guess it should be possible to publish SQL files as well, but this is just a guess.


Solution

  • You can use the Build Helper plugin for that.

    But the file name is computed from artifactid, version, type and classifier.

    If you need to absolutely push a different name with a different artifactId, you will need either to mvn deploy:deploy-file ... (from a command in your CI or with an ant script in the pom) or create an additional pom file and launch maven against it.

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
              <execution>
                <id>attach-artifacts</id>
                <phase>package</phase>
                <goals>
                  <goal>attach-artifact</goal>
                </goals>
                <configuration>
                  <artifacts>
                    <artifact>
                      <file>src/main/database/dba.sql</file>
                      <type>sql</type>
                      <!-- <classifier>xxx</classifier> -->
                    </artifact>
                  </artifacts>
                </configuration>
              </execution>
            </executions>
          </plugin>
    

    link to the source: https://www.mojohaus.org/build-helper-maven-plugin/usage.html