Search code examples
javamavengradlemaven-assembly-plugin

Gradle analogue for maven assembly plugin


I use assembly maven plugin in my project, to deliver jars, procrun executables and some scripts from src/main/scripts/ folder in single zip-file. How can I do it with gradle? I've saw gradle delivery and application plugins but I couldn't configure them in right way.


Solution

  • A Maven assembly can easily be replaced Gradle's distribution plugin. The configuration consists of two parts.

    Configuring the distribution

    The first part of the configuration is the declaration of the contents of the distribution:

    apply plugin: 'distribution'
    
    distributions {
      main {
       baseName = project.name
        contents {
          into('lib/') {  // Copy the following jars to the lib/ directory in the distribution archive
            from jar
            from configurations.runtimeClasspath
            dirMode = 0755
            fileMode = 0644
          }
          from('src/main/dist') {  // Contents of this directory are copied by default
            dirMode = 0755
            fileMode = 0644
          }
        }
      }
    }
    

    This contents section is just an example. Depending on your project you probably want to do different things here. A description of the configuration options can be found in the Javadocs of the AbstractCopyTask and the interfaces it implements.

    Files in src/main/dist are automatically added to the distribution. Even if there is no correspondent from statement in the publication. The second path element must match the name of the distribution (main in my example). I listed the directory in my example because I need to set the fileMode and dirMode properties on the copied files.

    Filtering files

    Filtering files is also possible using one of the filter() methods defined in the AbstractCopyTask.

    The example below uses the ReplaceTokens filter. It replaces ant-style place holders formatted like this: @placeholder@.

    import org.apache.tools.ant.filters.ReplaceTokens
    
    contents {  // contents section in distribution
      from('src/main/scripts') {
        filter(ReplaceTokens, tokens: [placeholder: 'replacement-string' ])
        filteringCharset = 'UTF-8'
      }
    }
    

    This will replace the string @placeholder@ in all files copied from the src/main/scripts directory with "replacement-string" in the distribution archive.

    The Javadocs for ContentFilterable describe some alternative ways to filter files.

    Configuring the file archives

    The second part of the configuration allows to manage file format specific features of the generated archive files. These settings are optional and only required if you are not happy with the format of the standard archives.

    By default Gradle does not compress the tar archive it produces. The example below configures the distTar task to compress the tar-file using gzip. As files are usually distributed as .tar.gz files and not just as tar files, it is a good idea to add this to the build:

    distTar {
      compression = Compression.GZIP
      extension = 'tar.gz'
      classifier = 'dist'  // Appends a suffix to the file name
    }
    

    See the Tar task for a description of all configuration options.

    The zip archive can be configured in the same way as the tar archive. This example simply adds a classifier string to the end of the file name:

    distZip {
      classifier = 'dist'
    }
    

    See the Tar task for a description of other configuration options.

    Build the distribution archives

    The distribution archives are created as part of the assemble task. Running gradle build or gradle assemble will produce them.