Wednesday, January 24

Migrating from Maven to Gradle - maven-assembly-plugin

When you have maven-assembly-plugin and you need to migrate it to Gradle you will probably use copy or zip tasks.

This is the Maven code we want to convert
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<configuration>
<finalName>mqm-distribution</finalName>
<descriptors>
<descriptor>assembly/assemblyDescriptor.xml</descriptor>
</descriptors>
</configuration>
<id>assemble for basic distribution template</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
view raw gistfile1.txt hosted with ❤ by GitHub

This is the assemblyDescriptior.xml file
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>template</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<includes>
<include>conf/**/*</include>
<include>server/**/*</include>
<include>wrapper/**/*</include>
<include>install/**/*</include>
<include>webapps/**/*</include>
<!--<include>webapps/portal/**/*</include>
<include>webapps/root/**/*</include>-->
<!--<include>HP_ALM_11_50_Permanent_Unlimited.dat</include>-->
</includes>
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>
view raw gistfile1.txt hosted with ❤ by GitHub



This is the resulting Gradle code
configurations {
deploymentZip
}
/**
* deployment-conf simply package the files in `content` directory in zip file.
* That zip file is later consumed by other module to build the installers.
*/
task packageZip(type: Zip) {
// A list of all the folders which needs to be packaged into the output zip.
def folders = [
'conf',
'install',
'server',
'webapps',
'wrapper',
]
// Package each folder into the zip
folders.forEach({folder ->
from (folder) {
into folder
}
})
baseName = 'distribution-template'
}
artifacts {
/**
* We publish the zip file as artifact under the `deploymentZip` configuration.
* This allows other modules to consume this zip file.
*/
deploymentZip packageZip
}
view raw build.gradle hosted with ❤ by GitHub


Let's break it down.
In my case all the assembly plugin did was to collect files from several directories and zip them all together.

I've used the ability to Gradle to run Groovy code and created a Zip task which copy files from each of the directories specified in the assemblyDescriptor.xml file.

The real work is done in line 23-24 where we take each folder and put it inside the zip file.

Finally in line 21-37 we provide the zip file as an artifact of this project so it can be consumed by other projects. This allow us to consume it as Gradle dependency in the following way:

dependencies {
    deploymentConf project(path: ':mqm-server-root:mqm-pom:mqm-deployment-conf', configuration: 'deploymentZip')
}

Notice the configuration part at the end.

No comments:

Post a Comment