Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Maven in my Java Project and Why?

Tags:

I am trying to figure out the use of Maven and I got many articles describing its features and uses. But I am just not able to understand the actual use of Maven from productivity standpoint.

From what I am used to in our school projects was just create a new Java project in Eclipse, write your Code, create a .war (if web-based) and paste the code to the webapps folder of Tomcat and start the server!

So,

  1. Where does Maven come into picture? I have used Ant and I understand Ants benefit of a standardized build process. But why do we need an advanced Ant in form of Maven?

  2. In any case, I need to use it, so where do I get started - basic flow, some good tutorials?

Thanks

like image 385
zengr Avatar asked Jun 18 '10 18:06

zengr


People also ask

Why maven is used in Java project?

Maven is chiefly used for Java-based projects, helping to download dependencies, which refers to the libraries or JAR files. The tool helps get the right JAR files for each project as there may be different versions of separate packages.

What are the advantages of using maven in your project?

Advantages of using Maven: One can easily build their project to jar,war etc. as per their requirements using Maven. Maven makes easy to start project in different environments and one doesn't needs to handle the dependencies injection, builds, processing, etc. Adding a new dependency is very easy.


2 Answers

Maven is used to manage the build, testing, and deployment processes. It can separate the unit tests and integration tests so you only run them when necessary and cut down on build time.

It is also a dependency manager, which means when you realize the server piece of your project needs apache commons-logging 1.0.4 but the client conflicts with anything past 0.7.9, you can just add a couple lines to the respective pom.xml files, and Maven handles all of that (downloading, installing, and keeping track of the different versions of those dependencies).

I was not a believer before my current task, but after 2 years using it for large enterprise applications, I definitely respect what Maven brings to the table. There are a lot of online resources but if you are going to be the lead on this and really feel uncomfortable, I recommend getting a book -- the O'Reilly one is helpful.


Forgot to mention that there is an Eclipse plugin which makes it almost painless to use with Eclipse: m2Eclipse.


Second update for example pom.xml segment to answer OP question:

Your pom.xml will contain XML code such as:

<dependencies>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.0.4</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

These are downloaded from the central Maven repository (google "maven nexus") or you can configure your own additional repositories (like for your own projects, or if you are not Internet-connected).

like image 169
Andy Avatar answered Sep 23 '22 22:09

Andy


I had exactly the same perception as you and for years I avoided Maven.

The thing is, it allows you to easily get the required jars your application may need( called dependencies - jars and other things - ) . So the next time somebody else run your project he will get the jars automatically.

I know that's a bit hard to grasp, until you work with an existing projects using it.

For instance I downloaded an open source project recently, which depended on 10 or 12 different on different jar versions. After downloading the source code and executing Maven, all those jars ( and a lot more others ) were downloaded for me.

The problem with Maven ( as a friend of mine told me ) is that to perform a "Hello world" program, it first downloads the world to greet him. :P

like image 20
OscarRyz Avatar answered Sep 20 '22 22:09

OscarRyz