Guys,
In this post, I will share a bit about amazing things we can do using Maven.
There are good plugins in Maven that we rarely hear about it. Sad.
Maven Enforcer Plugin
is one of them.
Well,
Working with Maven we deal with many configs, plugins, dependencies, and your versions.
Doing the management of it, sometimes, can make your head hurt.
We can work alone, or, we can work in a team.
And people are incredibly stunning, each one with your own style.
When I say style, it’s in a full context(IDE, numbers of space in tabs(2 of 4?, =D), versions of software, and so on).
Let’s begin with the more simple one.
Everybody knows that Java is free, open-source, etc.
We can download it, create a program, make it works, lunch that in Production…. huh… Nah!
But, maybe, you could forget that Oracle JDK it’s not allowed to be put in Production without a license - yeah.
Now, imagine, a member of your team, using that one to compile, create an artifact, and push it to Nexus, running pipeline and Voilà
it’s there on Production, and we have a huge problem now.
Note: I know there’s OpenJDK version, but, that’s not the point, OK?
Thanks for understanding, kisses/hugs, and going on. (=
Let’s imagine we are working on a team using only AWS stack.
At that moment, after talking with a team, we’ve decided to use only Amazon Vendor JDK Corretto.
Only just for better compatibility and, you know, it has improvements performing to the cloud.
How can we ensure those two points:
- Do not put Oracle JDK binaries without license in Production
- Only use the vendor-specific to our architecture
Using Maven Enforcer Plugin
!
Step one
is declare the plugin and setup the version:
1 |
|
Step two
is setup the plugin:
1 |
|
In this example I’m using the wrong Oracle’s JDK:
1 |
|
But, OK, we can confirm that JVM is from Oracle Corporation that easy way:
1 |
|
Oh, you notice? JShell said bye to me. Oh, my lovely Java. So polite. <3
When I try to compile, or, use any other Maven phase, it will fail like that:
1 |
|
The interesting in here is, that constraint verification runs at the first goal in Maven Lifecycle: validation.
That’s super cool, isn’t it?
Bonus… we can do more!
Imagine there’s already a Maven version 3.9 and you want force that specific one.
Mine’s completely wrong at this momento, right?
1 |
|
Running Maven:
1 |
|
Also you can specify the Java Version ignoring the vendor at this moment!
1 |
|
But, for now, i want only be able to use Java 11.
Running Maven:
1 |
|
There are more cooll stuffs.
But, in my opinion, that three constraints are the essentials:
- Maven version
- JDK vendor
- JDK version
That’s all for today.
The codebase is on my GitHub.
xoff.