Skip to content
Software Development

Putting the Mendix SDK to work: building a simple userlib checker

Those damn libs…

With its 10.3 release, Mendix has (finally!) introduced dependency management for the Java libraries that almost every Mendix project uses. It is hard to understate how wanted that feature was and, while we still need to find out how well it works, it will very likely make many Mendix engineers’ lives a lot easier. No more manually sifting through a projects’ userlib folder to find multiple versions of the same library when you’re doing some upgrading, hurrah!

Coincidentally I was working on a different way to make this process easier: the Trives Userlib Checker. I will explain how it works in further detail, but in a nutshell it uses the Mendix Platform and Model SDK’s to find and return all Java libraries of which there is more than one version in your userlib folder. While the Mendix 10.3 release doesn’t store most Java libraries in that folder anymore, all older releases still do, so this tool should be helpful for some time to come.

Introducing the Mendix SDK

I decided to find a solution using the Mendix SDK. This past year, I have been diving into the Mendix Platform and Model SDK’s and I am really impressed with the ease with which you can automate all kinds of things. Once you have your environment set up and know the basics of TypeScript, almost everything in your Mendix project is available for you to analyze and modify. This includes the entire file system, so things like Cascading Style Sheets, JavaScript snippets, widgets and the Java libraries (jars) within the userlib folder. This project serves as an example of how easy it can be to automate management tasks for your Mendix Project(s).

So, how does it work?

The main issue that can arise with your Java user libraries is that when multiple versions are present, Mendix can become confused as to which version to use, resulting in often hard-to-decipher errors. Therefore, we want to prevent multiple versions of a library existing within the same project. The Userlib Checker produces a list of ‘double’ libraries in 4 distinct steps:

Step 1: Set up your Mendix SDK client and open your project

The Mendix SDK allows you to inspect any of your Mendix applications. To get access to a specific project, you need to provide a Platform Access Token, the unique app ID (in Sprintr under settings) and the branch you want to inspect. Make sure to at least check ‘mx:modelrepository:repo:read’ when generating your PAT. We then provide all these to our tool using a config.json file. In the Userlib Checker this looks as follows:

Step 2: Get all the files in the userlib folder with a .jar extension

Once we have read our Mendix application, we can get all the files in the project as a list of strings. Each filename includes its folder path, e.g., ‘userlib/stax2-api-3.1.4.jar’ or ‘theme/web/login.html’. In this case, we are only interested in files that are in the userlib folder and have a .jar extension. In TypeScript, we can easily filter out all other files by ‘chaining’ those two filters:

Step 3: For each file, get the library name and version. If both are found, add it to the library list

Once we have isolated the files we are interested in in step 2, we can start building a list of libraries. Each of these will be accompanied by a list of the versions we have found in the project. In short, we do this by:

  1. Finding the part of the filename that is its version. A regular expression is used to find any sequence of numbers separated by at least one dot. This seems to match most version numbers present in file names. Examples of these are 1.2, 1.2.3 or even 2023.10.07.
  2. Defining the library name as anything that comes before the version.
  3. Trying to find the library name in a list of names we found earlier. If not found, we add it.
  4. Adding the version number we found to a list of the library’s found versions.

In TypeScript, this will look as follows (including some very basic error handling):

Step 4: Export a list of libraries with more than one version to a JSON file

Once we have compiled the list of libraries, each accompanied by a list of the versions we found for that library, we can then simply filter out any library that does not have at least two versions. In TypeScript, that looks like this, including an alphabetic sort on the library name for good measure:

The end result

If successful and indeed multiple versions are found, (the beginning of) your output.json file should look something like this:

Downloading and using the checker

You can find the Userlib Checker on our Github: https://github.com/Trives-IT/trives-userlib-checker. Check the readme for more detailed instructions on how to set it up and use it. It should be fairly straightforward but please let me know if you run into any issues.

The Trives Mendix SDK starter template

In building the Userlib Checker, I have used the Trives Mendix SDK starter template that is free to use and you can find on Github as well: https://github.com/Trives-IT/trives-mendixsdk-starter. You can use this to easily start your own experiments with the SDK, as it will guide you through the initial steps to get you up and running quickly.

Future developments…?

While I kept this example (relatively…) simple on purpose, this checker can be expanded upon to for example:

  • Check not one project but all your Mendix projects in one go;
  • Check against a list of known dangerous libraries, e.g. log4j;
  • Check for the presence of other files in other folders (such as Mendix Dojo widgets, that are going to become obsolete soon);
  • Check the contents of files for specific phrases or words;
  • Delete or modify any file in your project (just be careful, OK?).

If you would like to contribute to this project, feel free to add some improvements and/or expansions. Soon, I plan to publish some additional experiments/projects that make use of the Mendix SDK. If you have some cool ideas, let me know! If you’re running a business or have a Mendix landscape and are curious to see how we as Trives could assist you using this technology, don’t hesitate to contact me or my colleague Marien Krouwel.

Latest articles

Discussion

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *