How to automate dependency management in Mendix using Maven

Menno de Haas
Mendix Community
4 min readFeb 3, 2020

--

A few weeks ago I decided to publish a new App Store module, the AmazonSQSConnector. The GitHub repo was already created more than a year ago, but I never took the time to prepare the module for publication. I opened the project and created a list of “to-dos”. The most important task was to update the AWS (Amazon Web Services) dependencies. Every time when I had to update dependencies in App Store modules, it was cumbersome to search for the right dependencies and include those in the module package. In this blog, I will show you how I automated dependency management for Mendix modules using Apache Maven.

What I was used to do, is downloading all (transitive) dependencies and add those to the /userlib folder in the Mendix project. Let’s investigate whether we can use Maven to compile the code, collect dependencies and export the module package. Several people I know use Gradle for their Mendix App Store modules, but I find myself more familiar with Maven.

Create a POM

The first step is the initiation of a Maven project including an empty POM.xml (project object model) file in the root directory. Before Maven could compile the code, the Mendix Java API libraries were added to the dependency list.

I used several properties to ensure that the configuration can easily be changed into new Mendix versions. It is possible to hard-code default values in the POM, but you can also pass the property values in the command line or Eclipse Maven plugin.

Specify directory structure

Afterwards, I looked into the Mendix project folder structure, which is different than the default Maven Java project. Source files are located in the /javasource folder and compiled sources in /deployment/run/bin.

Clean the project folder

Whenever the build was executed, I wanted to clean the project folder. Apache Maven provides the clean plugin that cleans the output and target directories. I wanted to add the /userlib folder to the configuration to make sure the folder is recreated each time the build is executed.

Copy dependencies

Originally, our goal was to manage dependencies for Mendix. I now had to copy dependencies into the /userlib folder, which can be done using the copy-dependencies plugin. I had to make sure that the Mendix dependencies were not copied as they are added to the classpath automatically by the runtime launcher. The copy-dependencies task was bound to the package phase.

Export module package

At this point, the application was compiling and running correctly. It would be nice to create the module package automatically using the right dependencies. Mendix comes with the mxutil.exe command-line tool, which offers the possibility to export module packages. The--filter-required-libs flag gives the instruction to include jars that have a corresponding .RequiredLib file. I had to search for a way to create files and execute commands using Maven. Apache Ant in combination with the Ant-Contrib library offered the features I was looking for. I had to add Ant-Contrib to the dependency list in the POM file.

Apache Ant uses XML files to execute tasks. I created a build.xml file in which I could specify the tasks. The build file has access to the variables that Maven exposes, for instance, the ${basedir}. Before I could start I had to define the Ant-Contrib tasks.

The first part of the build.xml file loops through the *.jar files in the /userlib folder (excluding the Ant dependencies) and creates the .RequiredLib files.

For each file in the /userlib folder the touchRequiredLibFile target is executed.

The second part of the file executes the mxutil.exe and copies the MPK-file to the release directory including its version number.

The build.xml file is ready for execution. I had to execute the tasks from the POM file in the right order using the antrun-plugin specifying the target names (createRequiredLibs and exportModulePackage). The execution was bound to the install phase.

Run!

Now I could compile the code and copy dependencies using the following Maven command. Of course, Maven should be installed and added to your path.

To create a module package, I had to add the install phase to the command.

After this journey, the build process of Mendix modules was completely automated (except the release to the App Store, feature request!), which will enable me to release updates easier and clearly mark dependencies. I hope to encourage more Mendix developers to publish high-quality App Store modules using this (or other) dependency management solutions. If you have any questions, please reach out to me!

Try it yourself and clone the GitHub repo below.

--

--