Ever thought, you wanted to use a tool but not take the pain of installing and uninstalling it. Or be lazy like me to install it, use it once and move on with your life. This post will help you out if you feel the same pain and believe me all you need is, Docker.
docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn clean install
Let’s take a scenario where you have a java project to build. Ideally (historically) what you would do is google, “install maven on mac“. Google will do its magic and show you about 1,05,00,000 results in 0.52 seconds. Clicking on the first link, would take you to the official Maven website which has the installation instructions for all the platforms.
Another way would be to google “install maven on mac brew“, which would again give you About 3,42,000 results in 0.52 seconds. Clicking on the first link, would take you to the our favourite website, stackoverflow, there you would find a simple brew command to install maven. Easy enough, huh?
Now if you use any of the above methods for building the java project just once of twice, and never uninstall, it would be just like adding crap to your local system.
Another way that I prefer would be to use docker for this purpose. Docker helps to get tools / apps and its dependencies on your machine without taking into account of the base OS. Moreover the cleaning up is even easier, you’ll see. Just enter the below command :
$ docker run -it --rm --name my-maven-project -w /usr/src/mymaven maven:3.3-jdk-8 mvn --version Unable to find image 'maven:3.3-jdk-8' locally 3.3-jdk-8: Pulling from library/maven 6d827a3ef358: Pull complete 2726297beaf1: Pull complete 7d27bd3d7fec: Pull complete e61641c845ed: Pull complete cce4cca5b76b: Pull complete 6826227500b0: Pull complete c03b117ffd91: Pull complete 821a1547b435: Pull complete 2bd47f6b1b42: Pull complete e4cf3e9f705c: Pull complete 3733107c5c01: Pull complete Digest: sha256:18e8bd367c73c93e29d62571ee235e106b18bf6718aeb235c7a07840328bba71 Status: Downloaded newer image for maven:3.3-jdk-8 Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00) Maven home: /usr/share/maven Java version: 1.8.0_121, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre Default locale: en, platform encoding: UTF-8 OS name: "linux", version: "5.10.25-linuxkit", arch: "amd64", family: "unix"
If you have run the docker command for the first time, this will be the output. What essentially happened is, docker pulled the
maven:3.3-jdk-8 image locally and created a container out of it.
To read more about docker internals, check this out.
Now, you must be thinking, how would I be using the maven command inside of the container to build a project locally. The answer is docker volumes :
$ docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn --version Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00) Maven home: /usr/share/maven Java version: 1.8.0_121, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre Default locale: en, platform encoding: UTF-8 OS name: "linux", version: "5.10.25-linuxkit", arch: "amd64", family: "unix"
Take a good look at this part of the command
-v "$(pwd)":/usr/src/mymaven. This mounts the present working directory (
pwd) to the workspace of the container, which makes you feel like the
mvn is installed right there in your local. Moreover, you can see the build output in the same working directory in your local after the build is successful, even if the container is no more running. MAGIC of docker volumes.
mvn --version in the end of the command could be updated like below to build a project :
$ docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn clean install [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for test:project:jar:1.0-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 38, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building project 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom (4 KB at 2.1 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/22/maven-plugins-22.pom Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/22/maven-plugins-22.pom (13 KB at 21.4 KB/sec) Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/21/maven-parent-21.pom ... ... ...
Now that I have been bragging about how easy it is to clean, I must tell you we have already done it, the
--rm flag used in the above command has already deleted the stopped container, however image is still there which can easily be cleaned by running docker prune command once a month (that’s how I do it).
$ docker image prune -a WARNING! This will remove all images without at least one container associated to them. Are you sure you want to continue? [y/N] y untagged: maven:3.3-jdk-8 untagged: maven@sha256:18e8bd367c73c93e29d62571ee235e106b18bf6718aeb235c7a07840328bba71 deleted: sha256:9997d8483b2fc521a4159feab922546dda0c5c22b5084f86dfab48f123ae4364 deleted: sha256:4670a3a3ccbe1fa08dbcd29629e833086949809a5bf6fe1ac4e336b65f0c814c deleted: sha256:4501fab525c27427b85718fc477889071ec8a7adf2c3629bcff4246e4d1df787 deleted: sha256:32babe003c3a531aebcdb501deb957c9933f7c90beabfab5d8805f62a084f398 deleted: sha256:7d628fad6450f42db382a12b2d5470166b63fce76f6b1865f39718ad99accfab deleted: sha256:6bc44dc0c6c47463f610e82d75246a0f804c27f86bdf46a8ae1563bb3cc77142 deleted: sha256:73d13c48b7fe3dbbf513351da9e439a2f38332c4cfcc149c6e7368dafc31e2d9 deleted: sha256:dcd168f76c182f075b2dcdafe6a80dcb6fa4180588214df23322b706dc2b9536 deleted: sha256:33690a5a483b7684bc809f4c4d8992d946f063b23fc99f68e661383953d910a8 deleted: sha256:2ecb867048da2f6e2740814c177ca71c959ab2c91acaefbe827149302b20076f deleted: sha256:763ac6a64f068afd061160ff77b7ab41f61aba49b554bfe39ca033fcbf4f2d3c deleted: sha256:5d6cbe0dbcf9a675e86aa0fbedf7ed8756d557c7468d6a7c64bde7fa9e029636
As you can see how easy it is, now this was the example of maven, you could do the same thing with any of the runtime or tool, as long as its docker image is available. For example – awscli, pip, ant, docker in docker, msbuild for .net projects, etc, without actually installing it on your local machine.
Thats it for this one, thank you for reading this post. I hope this is useful, feel free to put your queries and suggestions.