Click on "end of life", pay attention to the official account.
Daily technical dry goods, delivered at the first time!
I believe that as a Java developer, you've had enough of Maven's slow compilation, but you can't switch to other faster construction tools for the time being due to historical baggage, usage habits and other problems. Here, the author will introduce you to a faster Maven - maven mvnd.
1. Introduction
Maven mvnd is a faster build tool derived from Gradle and Takari by the Apache Maven team. Maven is embedded in mvnd, which is why we can seamlessly switch Maven to mvnd (there is no need to install Maven separately).
In terms of design, one or more daemons will be generated in mvnd to serve the construction request, so as to achieve the effect of parallel construction. In addition, in the choice of VM, mvnd uses GraalVM to replace the traditional JVM. Compared with GraalVM, it starts faster and occupies less memory.
According to the document description, mvnd has the following advantages over traditional Maven:
-
The JVM running the build does not need to be restarted for each build.
-
The class loader of Maven plug-in class is cached in multiple builds, and the plug-in jars will only be read and parsed once.
-
The native code generated by the JIT in the JVM is preserved. JIT compilation takes less time than Maven. During repeated builds, JIT optimized code is immediately available. This applies not only to code from the Maven plug-in and Maven kernel, but also to all code from the JDK itself.
By default, mvnd uses multiple CPU cores to build modules in parallel. The number of cores used is determined by the formula math max(Runtime.getRuntime(). Available processors () - 1, 1). If your source tree does not support parallel builds, pass - T1 on the command line to serialize your builds.
At the same time, the official gives the dynamic diagram of operation on the 24 nuclear machine:
2. Installation
For the installation of mvnd, the official document gives a very detailed tutorial. It is recommended to read it first: https://github.com/apache/maven-mvnd .
The author installed through Homebrew. Practice has proved that there is no problem with the installation and use of macOS m1. However, it should be noted that the mvnd version installed in this way is 0.7.1. After testing on ubuntu and macOS m1, it is found that this version does not support JDK8 (it may only be the problem of the author's computer), and the JDK11 shown in the official example is no problem. Running the mvnd command in JDK8 will produce the following errors:
~ % mvnd -v
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/mvndaemon/mvnd/client/DefaultClient has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:757)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:473)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:601)
It is estimated that the executable downloaded in this way is compiled through the higher version of JDK. It cannot run on the lower version because it lacks some methods or features. At a time when I was at a loss, the author found a Closed issues:Different java versions for mvnd and maven #512 from the update instructions of the latest version of maven mvnd. The author provided a solution to this problem_ The version specified by home is set to JDK11, and when running the mvnd command, add the parameter - dmaven compiler. Release = 8, i.e
mvnd -Dmaven.compiler.release=8 compile
In this way, the compiled code corresponding to JDK8 can be generated.
For issue #512, the minimum supported version of mvnd responded by the author is JDK8, but the author still reported the same error when trying from 0.5.2 Maybe there is a problem with the author's computer, because I see that the latest version of JDK8 can be installed and used by others in the posted result diagram. In addition, if it still doesn't work, maybe we can manually compile the source code to generate the executable file. The specific steps have been given in the official readme.
3. Use
The usage is exactly the same as Maven. Just change the command mvn to mvnd. In the actual measurement of the author's machine, compared with the traditional maven, the construction time of mvnd is 1 / 2 of the original.
4. Summary
In this paper, the author states the introduction, installation, use and some exceptions of maven mvnd. If readers want to know more details, they can read the official documents. Maybe the strengthened Maven is still no better than Gradle, but the strengthened Maven is still very fragrant under the background of historical burden and usage habits.
reference resources:
https://github.com/apache/maven-mvnd
https://github.com/apache/maven-mvnd/issues/512
https://mp.weixin.qq.com/s/TG_6vq0mgbej8F3Qvpb6Vw
PS: in case you can't find this article, you can collect some likes for easy browsing and searching