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.
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.
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.
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.
A Maven assembly can easily be replaced Gradle's distribution plugin. The configuration consists of two parts.
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 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.
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.
The distribution archives are created as part of the assemble
task. Running gradle build
or gradle assemble
will produce them.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With