groovymuleanypoint-studioapache-commons-lang3

Mule Anypoint Studio groovy.lang.MissingPropertyException: No such property: StringEscapeUtils for class


This is for Mule 4, on 4.3 EE Runtime

Updated

POM File holds standard dependency, and thanks to Aled's answer also included it as a shared library:

<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>
                    <sharedLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                    </sharedLibrary>
                <sharedLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                    </sharedLibrary>
                <sharedLibrary>
                        <groupId>org.springframework.security</groupId>
                        <artifactId>spring-security-core</artifactId>
                    </sharedLibrary>
                <sharedLibrary>
                        <groupId>org.springframework.security</groupId>
                        <artifactId>spring-security-config</artifactId>
                    </sharedLibrary>
                <sharedLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                    </sharedLibrary>
                </sharedLibraries>
            </configuration>
        </plugin>
    </plugins>
</build>


<dependencies>
    <dependency>
        <groupId>org.mule.connectors</groupId>
        <artifactId>mule-http-connector</artifactId>
        <version>1.5.6</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>com.mulesoft.modules</groupId>
        <artifactId>mule-secure-configuration-property-module</artifactId>
        <version>1.2.2</version>
        <classifier>mule-plugin</classifier>
    </dependency>
    <dependency>
        <groupId>com.mulesoft.connectors</groupId>
        <artifactId>mule-amazon-s3-connector</artifactId>
        <version>5.6.0</version>
        <classifier>mule-plugin</classifier>
    </dependency>
    <dependency>
        <groupId>com.mulesoft.connectors</groupId>
        <artifactId>mule-amazon-sns-connector</artifactId>
        <version>4.4.2</version>
        <classifier>mule-plugin</classifier>
    </dependency>
    <dependency>
        <groupId>org.mule.modules</groupId>
        <artifactId>mule-spring-module</artifactId>
        <version>1.3.3</version>
        <classifier>mule-plugin</classifier>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>5.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>5.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.mule.modules</groupId>
        <artifactId>mule-scripting-module</artifactId>
        <version>1.1.6</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</id>
        <name>Anypoint Exchange</name>
        <url>https://maven.anypoint.mulesoft.com/api/v1/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>

Test call of the method required:

<scripting:execute doc:name="test StringEscapeUtil" doc:id="3f3fafea-e42c-48d2-820b-69f683307b69" engine="groovy">
    <scripting:code >import org.apache.commons.lang.*;

    String test = &quot;This is a test ,string \&quot; to escape&quot;;

    log.info(StringEscapeUtils.escapeCsv(test))
    </scripting:code>
</scripting:execute>

Actual error:

org.mule.runtime.core.internal.exception.OnErrorPropagateHandler: 
********************************************************************************
Message               : groovy.lang.MissingPropertyException: No such property: StringEscapeUtils for class: Script2
Element               : test/processors/2 @ test:test/testScript.xml:47 (test StringEscapeUtil)
Element DSL           : <scripting:execute doc:name="test StringEscapeUtil" doc:id="3f3fafea-e42c-48d2-820b-69f683307b69" engine="groovy">
<scripting:code>import org.apache.commons.lang.*;

String test ="This is a test ,string \"to escape";

log.info(StringEscapeUtils.escapeCsv(test))</scripting:code>
</scripting:execute>
Error type            : SCRIPTING:EXECUTION
********************************************************************************

Original Issue

A scripting module running using Groovy Engine.

I've included the dependency in the mule maven pom file:

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

The groovy script component does call upon the apache commons lang3:

import org.apache.commons.lang3.*;

And the call itself just outright fails:

StringEscapeUtils.escapeCsv(csvText);

The actual error:

Message               : groovy.lang.MissingPropertyException: No such property: StringEscapeUtils for class: Script2

Solution

  • Because of Mule 4 classloading isolation you need to mark the Apache Commons Lang3 library as shared in the pom, so the scripting module can see it:

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