External Jar Class in Dataweave

 

We can reuse the existing External Jar Class in Dataweave without writing the java code in dataweave again. One of the external jar usage can be extending StringUtils method in Dataweave as currently Dataweave doesn’t support all the String functions as Java StringUtils does

To do so we have to perform following steps

Add required dependency and shared lib tags in pom.xml

Dependency

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.10</version>
</dependency>

Shared Library

<configuration>
	<sharedLibraries>
		<sharedLibrary>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</sharedLibrary>
	</sharedLibraries>
</configuration>

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.mycompany</groupId>
	<artifactId>dataweave-sample</artifactId>
	<version>1.0.0-SNAPSHOT</version>
	<packaging>mule-application</packaging>

	<name>dataweave-sample</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

		<app.runtime>4.2.2</app.runtime>
		<mule.maven.plugin.version>3.3.5</mule.maven.plugin.version>
	</properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.mule.tools.maven</groupId>
				<artifactId>mule-maven-plugin</artifactId>
				<version>${mule.maven.plugin.version}</version>
				<extensions>true</extensions>
				<configuration>
					<sharedLibraries>
						<sharedLibrary>
							<groupId>org.apache.commons</groupId>
							<artifactId>commons-lang3</artifactId>
						</sharedLibrary>					
					</sharedLibraries>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<dependencies>
		<dependency>
			<groupId>org.mule.connectors</groupId>
			<artifactId>mule-http-connector</artifactId>
			<version>1.5.11</version>
			<classifier>mule-plugin</classifier>
		</dependency>
		<dependency>
			<groupId>org.mule.connectors</groupId>
			<artifactId>mule-sockets-connector</artifactId>
			<version>1.1.5</version>
			<classifier>mule-plugin</classifier>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.10</version>
		</dependency>
	</dependencies>

	<repositories>
		  <repository>
			<id>anypoint-exchange-v2</id>
			<name>Anypoint Exchange</name>
			<url>https://maven.anypoint.mulesoft.com/api/v2/maven</url>
			<layout>default</layout>
		</repository>
		<repository>
			<id>mulesoft-releases</id>
			<name>MuleSoft Releases Repository</name>
			<url>https://repository.mulesoft.org/releases/</url>
			<layout>default</layout>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>mulesoft-releases</id>
			<name>mulesoft release repository</name>
			<layout>default</layout>
			<url>https://repository.mulesoft.org/releases/</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>
</project>

Add classLoaderModelLoaderDescriptor element in

mule-artifact.json

{
	"minMuleVersion": "4.2.2",
	"classLoaderModelLoaderDescriptor": {
		"id": "mule",
		"attributes": {
			"exportedPackages": [
			"org.apache.commons.lang3"
			]
		}
	}
}

Import the Class and use it in Dataweave

Dataweave

%dw 2.0
import java!org::apache::commons::lang3::StringUtils
output application/json
---
{
	a: StringUtils::substring("MYSTRING",1,2)
}

or

%dw 2.0
output application/json
---
{
	a: java!org::apache::commons::lang3::StringUtils::substring("MYSTRING",1,2)
}

Sample Application – dataweave-sample

 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Suraj Deshmukh
Suraj Deshmukh
4 years ago

I followed the above steps but getting error as Unable to resolve reference of: StringUtils::substring. So the only difference I see is that im using 4.3 Mule Runtime.