Good afternoon guys,
I have been busy with some projects in my job and I forgot to write on the blog. But I am back again :).
Today I want to share some information about a tool that is been really useful for me. And that tool is called Maven. The first time that I heard about it, was when I was in college in my software design class. And to be honest, at the beginning I did not understand what was about. But now, with some side-projects and experience with my actual job, I know what does Maven is about. And I am going to explain you about it.
Maven is a tool that was created with the purpose of improving how the developers manage and build their Java projects. Maybe you heard about something called Apache Ant, if so, Maven is something like that. From my perspective is so much simpler and is based in XML format.
Maven uses a Project Object Model (POM) in order to describe the project, the dependencies, and elements among other stuff. Here in the POM file is where you specify the objectives such as the package and the compilation of the code.
One of the key characteristics of Maven is that is ready to use in a network. Because you can download plugins from a central repository. In this article, i am referring to the Maven repository (you can create your own repository on your own server). So what does this means? It means, that for example Peter is developing a web app about monitoring resources and I want to review the app and the code, Peter only has to give me the code with the POM file so I can execute the web app on my computer.
So what does this means? It means, that for example Peter is developing a web app about monitoring resources and I want to review the app and the code, Peter only has to give me the code with the POM file so I can execute the web app on my computer. I have to remark that I must have Maven installed in my computer.
After Peter gave me his web app code, I need to run some commands to generate the file (it can be a jar or war file for example). But what are these commands? And the answer is the life cycle commands. Also known, as the Maven life cycle. So here are those commands:
- compile. Generate the .class files.
- test. Execute automatically the JUnit test that exists in our code.
- package. Generate the .jar file with the .class compiled.
- install. Copy the .jar file to a directory where Maven storage all our .jar files.
- deploy. Copy the .jar file to a remote server so we can access to that project in the remote server.
But you may ask, how to execute those commands. Here is the answer: for example. The code of Peter gave me, in my CMD I have to switch my directory to Peter’s code and once that I am the location of Peter’s code I have to execute the following command
- mvn compile.
- mvn test.
- mvn package.
- mvn install.
- mvn deploy.
One quick tip is that if you execute mvn install you will execute the life cycle phases from the beginning to the one that you executed, in this case, was mvn install.
There are other commands that complement Maven, but these ones are not from the life cycle.
- mvn clean. Eliminate all the .class and .jar generated previously. After this command, we can have a scratch compilation phase.
- mvn assembly. Generate a .zip file with all the necessary for the installation of our app.
- mvn site. Generate a website with the information of our project. This information must be written in the pom.xml and the .apt file.
- mvn site-deploy. Deploy the website or our app to the server that we configured.
From my perspective and my experience all of these commands can be resumed into this one:
- mvn clean install
The reason is because as I said if you execute mvn install you will execute almost all the phases of the life cycle and with the mvn clean command, you will erase everything that you had.
Finally, here is a link that helps me to understand better this tool.
Also, in my GitHub account I have a repository with a demo app, so you can give it a look.
So, that all for today I hope that you found this post helpful and if so share it with your colleagues.