How We Cut Maven Build Times by 50% with GoTemplate and Maven Flags
This article details how the Omega platform reduced compilation time by up to 60% by adding Maven -am -pl flags, leveraging GoTemplate custom functions, and adjusting GitLab‑CI variables, providing a step‑by‑step verification of each attempt and the final solution.
Background
The Omega platform hosts multiple applications in a single code repository, causing compilation to become increasingly slow as the repository grows. To address the slowdown, a focused analysis was performed.
Current Situation
Omega applications run compilation tasks via GitLab‑CI, which invoke the internal omega‑cli tool. This tool generates the compile job based on parameters defined in .omega.yml. The compile configuration lives in the preset project.
Verification Process
1. Thinking
The core of solving multi‑module build slowdown is to specify only the required sub‑modules. Maven provides the -am -pl flags for this purpose, so adding these flags after the mvn command in the preset shell should limit the build scope.
2. How to implement?
2.1 Linux environment variable attempt
The default preset command runs a full install regardless of sub‑modules:
An attempt was made to pass CHILD_MODULE="-am -pl" via GitLab variables ( variables[MAVEN_PROFILES]) and Docker -e flags, but spaces in the value caused errors, and quoting did not propagate correctly. The approach was abandoned.
2.2 GoTemplate attempt
Since runtime variable substitution failed, the script was modified to use Go's native template engine. Parameters like {{ .ENV }} in the preset script were replaced with values from .omega.yml:
Variables were then passed through .omega.yml:
The generated command matched the desired -am -pl usage and succeeded on the first try. However, if the CHILD_MODULE key was missing in .omega.yml, the template parsing failed, making this solution unsuitable for all repositories.
2.3 GoTemplate custom FunctionMap
The final solution added a custom function to the Go template that checks whether a key exists before using it. This avoids parsing errors when CHILD_MODULE is absent.
Unit‑test code (shown in the image) demonstrates the template handling both presence and absence of the parameter correctly.
Both scenarios behaved as expected.
Final Solution
Actual Effect
For applications suffering from slow compile feedback, the optimization reduced build time from 4 minutes 27 seconds to 2 minutes 26 seconds, an improvement of roughly 50‑60%.
Before adjustment: 4 min 27 s. After adjustment: 2 min 26 s.
Postscript
Personally, the work introduced new GoTemplate features, and the platform will now support more flexible, system‑level compile commands in the future.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
