Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 741
glebiuskv Avatar asked Aug 20 '14 07:08

glebiuskv


People also ask

Can I use a Gradle plugin in Maven?

Gradle ships with a Maven plugin, which adds support to convert a Gradle file to a Maven POM file. It can also deploy artifacts to Maven repositories. The plugin uses the group and the version present in the Gradle file and adds them to the POM file. Also, it automatically takes the artifactId from the directory name.

How do I convert Maven plugins to Gradle?

To convert Maven to Gradle, the only step is to run gradle init in the directory containing the POM. This will convert the Maven build to a Gradle build, generating a settings. gradle file and one or more build. gradle files.

What is equivalent to Gradle task in Maven?

A Gradle task is the equivalent of a Maven goal. Just a unit of work to get executed in the build. Gradle keeps track of each task's inputs and outputs. Only when these change does the task get rerun.


2 Answers

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.

like image 144
Christoph Böhme Avatar answered Sep 23 '22 19:09

Christoph Böhme


Check out the Copy and Zip tasks, in my experience they can do more than assembly plugin with less ceremony

https://docs.gradle.org/3.5/javadoc/org/gradle/api/tasks/Copy.html https://docs.gradle.org/3.5/javadoc/org/gradle/api/tasks/bundling/Zip.html

like image 21
David Soroko Avatar answered Sep 25 '22 19:09

David Soroko