Import Mule Application as Project Libraries For Shared Resources
How to import one Mule application A into another Mule application B to utilize its shared resources as project libraries by adding it as maven dependencies.
Join the DZone community and get the full member experience.Join For Free
Use Case: If we want to use shared resources or common global configuration from one of Mule application into another Mule application then this article could guide us to achieve this use case.
In Mule 4, Anypoint Studio’s built-in Maven support for any Mule Projects has provided a great feature where any Mule applications can be packaged into a deployable JAR file. This package contains the application and all its dependencies. It can be used as JAR file or as a mule plugin.We can also used it as a utility application referencing through project libraries. In our use case we are going to use one Mule application as utility application into another Mule application.
Note: We have a concept called Mule Domain Project for same use case where we can defined common global configuration to be shared among other applications. But there is one impact of Mule Domain Project that it need to be deployed under same domain and in cloudhub we dont have support for it as each Mule application deployed into their own Mule runtime. It would not be possible to use a domain to share anything because domain can only share resources in the same runtime.
- The properties file ( prop-config.yaml) must be declared or get loaded through declaring it in configuration property inside Global elements.
- If developers want to shared any common resources to the multiple project then we can declare it through creating respective configuration in Global elements.
For ex: create a http listener configuration and provide a unique name to it .Also we can define JMS or Database or any endpoint configuration as shared resources.
- Deploy the common-property-configuration application. After successful deployment just export as Mule project.
- Now we can create a test demo project to utilize the shared resources and common property configurations. For this we need to import common-property-configuration.jar into demo project as maven dependency.
Follow the steps specified below to add the .jar file as a maven dependency :-
- The next pop-up window will ask us to browse the .jar file to install it into our local repository.
- Click install and finish adding it as maven dependency. The .jar will be successfully installed in our local repository. To verify that we can check .m2 folder in respective groupId.
- We can refresh the project and can check pom.xml ,the dependency of common-property-conifuation.jar will be added automatically.
- We can also check project libraries to verify the if the jar is added under libraries to refer to common-property-conifuation application.
- Before creating any sample flow , we can declare the utility application file by using Import configuration inside Global elements to references the files with all the configurations to import. The files will be loaded from the classpath of the artifact. With this we can reference to any common flows and configurations which we can utilize or access as shared resources. We can import multiple files.
Sample Flow In Application B:
We can create a simple flow by using http listener and a logger.
When we drop down the http listener connector configuration we can see the configuration of http listener from shared resources application is accessible and there no need to declare the manual configuration of http listener in Global elements.
In logger we can simply define the http.port property in message to print the propery value from common shared resource application .
Deploy and test:-
We can deploy this project into cloudhub:
Triggered the Request:-
Note:- As the application B contains snapshot dependencies so if there would be any change in the dependency may not impact in the cloudhub deployed application. Need to update the dependency and restart.
Opinions expressed by DZone contributors are their own.