Custom Policy in Mule 4
Implementation of custom policy in Mule 4 is a bit different from Mule 3.
First of all, install Maven, if you don't have one.
Put settings.xml in,m2 folder of the c drive.
In settings.xml, in servers tag, put repository id, Anypoint platform username, and password as shown below:
<servers>
<server>
<id>Repository</id>
<username>Anypoint username</username>
<password>Anypoint Password</password>
</server>
</servers>
Creating a policy requires creating several files like <policy>.yaml and template.xml, etc. We also need to have a proper structure for the policy project.
The easiest way to create the proper structure along with the files is by using the maven archetype provided by Mulesoft.
Include the following section in your settings.xml to make the archetype available:
<profiles>
<profile>
<id>archetype-repository</id>
<repositories>
<repository>
<id>archetype</id>
<name>Mule Repository</name>
<url>https://repository-master.mulesoft.org/nexus/content/repositories/public</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
To generate the skeleton using the archetype, use the following command:
mvn -Parchetype-repository archetype:generate -DarchetypeGroupId=org.mule.tools -DarchetypeArtifactId=api-gateway-custom-policy-archetype -DarchetypeVersion=1.1.0 -DgroupId=${orgId} -DartifactId=${policyName} -Dversion=1.0.0 -Dpackage=mule-policy
In place of ${orgId} write your organization Id.
for Organization id, you can find it by navigating to:
Access Management > Organization > Organization Id
In place of ${policyName} write the name of your custom policy
Once the command executed successfully, it will generate
1. pom.xml
2. YAML file
3. template file
In pom.xml, the repository id should be the same as of settings.xml
I already made the changes with the color blue in pom.xml
Refer below 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>e38d5c94-0642-45a9-aca0-2c2e36d4242e</groupId>
<artifactId>custpol</artifactId>
<version>1.0.0</version>
<name>custpol</name>
<description>custpol</description>
<packaging>mule-policy</packaging>
<properties>
<mule.maven.plugin.version>3.2.7</mule.maven.plugin.version>
<exchange.url>https://maven.anypoint.mulesoft.com/api/v1/organizations/e38d5c94-0642-45a9-aca0-2c2e36d4242e/maven</exchange.url>
</properties>
<repositories>
<repository>
<id>Repository</id>
<name>MuleSoft Exchange Environment</name>
<url>${exchange.url}</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>${mule.maven.plugin.version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<id>upload-template</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<repositoryId>exchange-server</repositoryId>
<url>${exchange.url}</url>
<file>${project.basedir}/${project.artifactId}.yaml</file>
<generatePom>false</generatePom>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>yaml</packaging>
<classifier>policy-definition</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>Repository</id>
<name>Corporate Repository</name>
<url>${exchange.url}</url>
<layout>default</layout>
</repository>
</distributionManagement>
<pluginRepositories>
<pluginRepository>
<id>mule-plugin</id>
<name>Mule Repository</name>
<url>https://repository.mulesoft.org/nexus/content/repositories/public/</url>
</pluginRepository>
</pluginRepositories>
</project>
Leave YAML file and template file as it is.
This policy will print Hello World! on successful execution.(This configuration you can see in src/main/mule/template.xml)
for packaging of policy project:
execute maven command: mvn clean install
On successful execution of command target folder is created with jar file in it.
Now for deployment to Anypoint Exchange execute maven command: mvn deploy
On successful execution of the command, you can see policy project in the Exchange portal.
Now, you can see your custom policy in the policy section and you can apply the policy in your project.