Deploying mule application through Maven

 

Let’s see how we can Deploy mule application on Cloudhub through Maven

Please check JAVA and MAVEN specific environment variables are properly configured

Run java command and check JAVA_HOME is pointing to correct JDK

Deploy on Cloudhub through Maven

 

Run mvn command and check M2_HOME is pointing to correct Maven directory

Deploy on Cloudhub through Maven

 

We have add below plugin to our application pom.xml

<plugin>
  <groupId>org.mule.tools.maven</groupId>
  <artifactId>mule-maven-plugin</artifactId>
  <version>3.3.2</version>
  <extensions>true</extensions>
  <configuration>
    <cloudHubDeployment>
      <uri>https://anypoint.mulesoft.com</uri>
      <muleVersion>${app.runtime}</muleVersion>
      <username>${username}</username>
      <password>${password}</password>
      <applicationName>${cloudhub.application.name}</applicationName>
      <environment>${environment}</environment>
      <workerType>${workerType}</workerType>
      <properties>
        <key>value</key>
      </properties>
    </cloudHubDeployment>
  </configuration>
</plugin>

 

After adding to pom.xml it will look like

<?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>hello-world-maven</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>mule-application</packaging>

  <name>hello-world-maven</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>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.mule.tools.maven</groupId>
        <artifactId>mule-maven-plugin</artifactId>
        <version>3.3.2</version>
        <extensions>true</extensions>
        <configuration>
        <cloudHubDeployment>
          <uri>https://anypoint.mulesoft.com</uri>
          <muleVersion>4.2.2</muleVersion>
          <username>muleisy</username>
          <password>*****</password>
          <applicationName>hello-world-maven</applicationName>
          <environment>Sandbox</environment>
          <workerType>MICRO</workerType>
          <properties>
          <key>value</key>
          </properties>
        </cloudHubDeployment>
        </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>
  </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>

Now run the below maven command to deploy the application to Cloudhub

mvn clean package deploy -DmuleDeploy

There is a chance you might get below error

<pre>  [INFO] --- mule-maven-plugin:3.3.2:deploy (default-deploy) @ hello-world-maven ---
  [INFO] Deploying artifact hello-world-maven
  [ERROR] Failed to deploy hello-world-maven: Failed to deploy application hello-world-maven
  org.mule.tools.client.core.exception.DeploymentException: Failed to deploy application hello-world-maven
      at org.mule.tools.deployment.cloudhub.CloudHubApplicationDeployer.deploy (CloudHubApplicationDeployer.java:46)
      at org.mule.tools.deployment.DefaultDeployer.deploy (DefaultDeployer.java:53)
      at org.mule.tools.maven.mojo.deploy.DeployMojo.doExecute (DeployMojo.java:46)
      at org.mule.tools.maven.mojo.deploy.AbstractMuleDeployerMojo.execute (AbstractMuleDeployerMojo.java:91)
      at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
      at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
      at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
      at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
      at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
      at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
      at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
      at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
      at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
      at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
      at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
      at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
      at java.lang.reflect.Method.invoke (Unknown Source)
      at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
      at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
      at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
      at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
  <strong>Caused by: org.mule.tools.client.core.exception.ClientException: 403 Forbidden: {"status":403,"message":"User unauthorized to access requested resource."}</strong>
      at org.mule.tools.client.core.AbstractClient.checkResponseStatus (AbstractClient.java:188)

</pre>

This error occurs when the application name we are defining in pom.xml is not globally unique

Change the application name accordingly

<applicationName>hello-world-maven-muleisy</applicationName>

 

Similarly we can get below error

      at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
      at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
  Caused by: org.mule.tools.client.core.exception.ClientException: 400 Bad Request: {"status":400,"message":"{name=null, workerVal=null, cpu=null, memory=null} is not a valid value for worker.type.name."}
      at org.mule.tools.client.core.AbstractClient.checkResponseStatus (AbstractClient.java:188)
      at org.mule.tools.client.cloudhub.CloudHubClient.createApplication (CloudHubClient.java:114)
      at org.mule.tools.deployment.cloudhub.CloudHubArtifactDeployer.createApplication (CloudHubArtifactDeployer.java:135)


We have to include below configuration property in pom plugin

<workerType>MICRO</workerType>

 

Now when we run the command it will deploy the application to Cloudhub

D:\work\workspace\hello-world-maven>mvn clean package deploy -DmuleDeploy

[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.mycompany:hello-world-maven:mule-application:1.0.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.mule.tools.maven:mule-maven-plugin @ line 31, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------< com.mycompany:hello-world-maven >-------------------
[INFO] Building hello-world-maven 1.0.0-SNAPSHOT
[INFO] --------------------------[ mule-application ]--------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean-1) @ hello-world-maven ---
[INFO] Deleting D:\work\workspace\hello-world-maven\target
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:clean (default-clean) @ hello-world-maven ---
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:validate (default-validate) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:initialize (default-initialize) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:generate-sources (default-generate-sources) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:process-sources (default-process-sources) @ hello-world-maven ---
[INFO] About to fetch required dependencies for artifact: com.mycompany:hello-world-maven:pom:1.0.0-SNAPSHOT. This may take a while...
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ hello-world-maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:process-resources (default-process-resources) @ hello-world-maven ---
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ hello-world-maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:compile (default-compile) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:process-classes (default-process-classes) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:generate-test-sources (default-generate-test-sources) @ hello-world-maven ---
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ hello-world-maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:generate-test-resources (default-generate-test-resources) @ hello-world-maven ---
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ hello-world-maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:test-compile (default-test-compile) @ hello-world-maven ---
[INFO]
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:package (default-package) @ hello-world-maven ---
[INFO] Building zip: D:\work\workspace\hello-world-maven\target\hello-world-maven-1.0.0-SNAPSHOT-mule-application.jar
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:validate (default-validate) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:initialize (default-initialize) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:generate-sources (default-generate-sources) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:process-sources (default-process-sources) @ hello-world-maven ---
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ hello-world-maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:process-resources (default-process-resources) @ hello-world-maven ---
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ hello-world-maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:compile (default-compile) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:process-classes (default-process-classes) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:generate-test-sources (default-generate-test-sources) @ hello-world-maven ---
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ hello-world-maven ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:generate-test-resources (default-generate-test-resources) @ hello-world-maven ---
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ hello-world-maven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:test-compile (default-test-compile) @ hello-world-maven ---
[INFO]
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ hello-world-maven ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:package (default-package) @ hello-world-maven ---
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:verify (default-verify) @ hello-world-maven ---
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ hello-world-maven ---
[INFO] No primary artifact to install, installing attached artifacts instead.
[INFO] Installing D:\work\workspace\hello-world-maven\pom.xml to C:\Users\abc\.m2\repository\com\mycompany\hello-world-maven\1.0.0-SNAPSHOT\hello-world-maven-1.0.0-SNAPSHOT.pom
[INFO] Installing D:\work\workspace\hello-world-maven\target\hello-world-maven-1.0.0-SNAPSHOT-mule-application.jar to C:\Users\abc\.m2\repository\com\mycompany\hello-world-maven\1.0.0-SNAPSHOT\hello-world-maven-1.0.0-SNAPSHOT-mule-application.jar
[INFO]
[INFO] --- mule-maven-plugin:3.3.2:deploy (default-deploy) @ hello-world-maven ---
[INFO] Deploying artifact hello-world-maven-muleisy
[INFO] Creating application: hello-world-maven-muleisy
[INFO] Starting application: hello-world-maven-muleisy
[INFO] Checking if application: hello-world-maven-muleisy has started
[INFO] Artifact hello-world-maven-muleisy deployed
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:14 min
[INFO] Finished at: 2020-03-18T11:40:19+05:30
[INFO] ------------------------------------------------------------------------

 

Application can be seen on Cloudhub

 

Test the API

 

More information can be seen regarding different properties and options

https://docs.mulesoft.com/mule-runtime/4.2/deploy-to-cloudhub

  
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
0 Comments
Inline Feedbacks
View all comments