How to deploy from Anypoint Studio using Maven

maven header
Jordan Schuetz
Jordan Schuetz
Developer Advocate
25 min read
Overview

The MuleSoft Maven plugin integrates the packaging and deployment of your mule application. When the maven commands are executed, your Anypoint Studio project is packaged into a deployable jar file which can then be deployed to any running mule runtime engine either in Cloudhub or OnPrem. For this tutorial, we will be covering how to set up your pom.xml and settings.xml file to deploy your first application on CloudHub using Maven. 

Don't forget to signup for a free trial by clicking the button below if you are new to MuleSoft!

Step 1: How to configure Maven on your computer

Make sure that you have all of the prerequisites before starting the tutorial:

  • Make sure that your system has the most recent JDK installed
  • Install Maven on your local machine

To check to see if Maven is installed on your local system, type the following command into the Terminal:

mvn -v

If you don't have Maven installed, you can install it using Homebrew. Simply type: brew install maven

When Maven is installed, run the command: mvn clean install

This command should create a local .m2 cellar folder that contains your settings.xml file which contains global settings for all Maven executions. This folder should be located at $USER_HOME/.m2/ and the folder called repository holds all of the local copies of various Maven artifacts, either caches of artifacts pulled down from remote repositories, such as Maven Central, or artifacts built by your local Maven builds. The artifacts are organized in there in folder structures that mirror the groupId's of the artifacts.

In order to successfully build using Maven, you will have to create and/or modify your settings.xml file. To access that folder on Mac, go to Finder -> Go and type: ~/.m2

If the folder doesn’t appear for you, feel free to create it. You can manually create the file by typing and then pasting the code below:

touch ~/.m2/settings.xml

Make sure to input your Anypoint Studio credentials in your settings.xml file then save the file.

You may get the below error when attempting to build your project using Maven if your JAVA_HOME environment variables aren’t configured correctly.

To configure your environment variables, type:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home

Make sure that you match up your JDK version with the one that is currently installed on your machine. To find the JDK version you have installed, simply navigate to that folder in your Finder by going to Finder -> GO.

For Windows users,  your .m2 folder is expected to be located under ${user.home} and you would see it under <root>\Users\<username>. The create a folder with the . prefix on Windows, simply go to Start -> Run, type cmd then in the command prompt type: md c:\Users\YourUsername\.m2 

Now that your machine is all set up, make sure to create an Anypoint Platform account below and download Anypoint Studio.

Step 2: How to configure Maven on Anypoint Studio

Next up, make sure to configure maven by overriding the user settings path. Go to Preferences -> Anypoint Studio -> Maven -> Global Settings and tick the “Override user settings path” then enter the path where the file is located that you just created. On the main Maven tab, make sure Use embedded Maven installation is checked. If you have a different version of Maven installed, you can define that by overriding the home directory path.

maven

Go to File -> New Mule Project. Name the project whatever you wish then drag an HTTP Listener into the scene. Name the endpoint /submit then drag a Set Payload component into the flow. Add the string “Maven upload completed” to the Set Payload component then click save.

httplistener
Step 3: Configuring your pom.xml file for Maven deployment

Go to your project folder, and look for the pom.xml file that is automatically generated when you create your project.

Under the groupID tag, you will have to replace that with your Anypoint Platform ID. You can find your Anypoint Platform ID by logging into Anypoint Platform, and if you look at your home page URL you will the long string such as this: 1018cee5-cd76-4f15-b89b-XXXXXXXXXXXX

Your pom.xml file should look similar to the one below. Make sure the replace <username> and <password> with your own Anypoint Platform user credentials. Then make sure to name your <applicationName> something unique. Additionally, make sure all of your Maven versions and Anypoint Platform versions match with your project specifications

Now that you have successfully configured your project and Maven, right-click on your project folder and view it in the System Explorer to get the path.

Go into your Terminal and type cd into your Anypoint Studio project directory with your project components:

cd /Users/YOUR-USER-NAME/AnypointStudio/studio-workspace/maven-tutorial

Now type the command to deploy your application to CloudHub:

mvn clean package deploy -DmuleDeploy

terminal

Once maven has completed the deployment to CloudHub, visit Runtime Manager and confirm that your application has been successfully deployed.

runtime manager

To test to see if your project works, go to the endpoint URL you defined and confirm to see if the string you entered on SetPayload is returned as the body response.

maven upload complete
Conclusion

Thank you for reading our tutorial on how to deploy on Anypoint Studio using Maven. We hope that you found this content helpful and that it reduces your overall deploy times. If you have any questions or comments about the tutorial, please tweet us @MuleDev and visit our developer tutorials homepage for more tutorials.

Try Anypoint Platform for free

Start free trial

Already have an account? Sign in.

Related tutorials