Maven, a Yiddish word meaning accumulator of knowledge, is an Apache project that standard the way we build projects. It mainly works with Java, but can also be used with other languages. Maven faces traditional compilation tools, it is not the opponent of the Ant compiler that we all have blindly used, it extends Ant, so think about it as an improved Ant. This article aims to show you what, why and how.
Maven works on the top of a XML file named POM (Project Object Model) that describes its dependencies on external modules and components, the build order, directories and required plug-ins. It dynamically downloads the required external dependecies at the moment they are specified from a central repository though you can declare different sources. Follow me in a practical example to see how it works!
Maven is usually integrated with development tools like Eclipse IDE, anyway it is a better practice if we properly install Apache Maven in our command prompt as it was made for.
STEP 1. To guarantee a successful instalation we must be sure we have java jdk installed in our command prompt: so go and type:
If you had no problem there, just take note of your java version and go right into step 2. If you find a problem here, Windows users can find help in this video and Linux/Unix users here.
Windows Users: download the program from it’s official source here. Unzip, move unziped folder to Program Files and open bin/mvn.bat If you have problems, it may be that you have java JRE and not JDK, so try to reinstall it properly with step 1.
Linux and Unix: run the command
sudo apt-get install maven2
Now that everybody have installed maven, we can successuly type the command:
Using the command line, go into the folder you will to be your workspace. Noobs in Command Line: watch this video. Right in your workspace, let’s initializate a maven project:
mvn archetype:create -DgroupId=com.dps.maven2example -DartifactId=maven2example_logic
java -cp target/maven2example_logic-1.0-SNAPSHOT.jar com.dps.maven2example.App
If success, you will get a “Hello World!” message in return. If you are inside a private network, you may fail from the first command. Solution for that is to configurate your proxy at the maven configuration: please look for your maven installation folder and open the file conf/settings.xml Right there is an space for proxy configuration, uncomment it and fill the form.
ADDING NEW DEPPENDENCIES
Open your pom.xml file This is my view:
I strongly recommend you to use a good text editor to visualize your files, if you fell curious, I’m using Atom text editor. Atom is free and multiplatform (since it’s kind of new, specially Windows and Linux users will have to scratch Google to find a suitable installer).
See we have an dependency into our file. You may aldo have noticed during the exacution of your commands, some files were downloaded: this is what I refered to when I told you maven dinamically downloads the required external dependecies at the moment they are specified. Let’s add another dependency, a spring library, and see what happens.
STEP 1. Go to the main repository at http://mvnrepository.com/ (save this link) and search “spring”
From the list of results, select the one that has the spring library. There, you will find the:
STEP 2. Okay, let’s add it to our POM, run “mvn package” and see how the whole thing gets downloaded. Now we can use the librarie’s methods.
MOVING TO ECLIPSE
Finally, we all know it is easier to have a IDE that reads our project’s dependencies and outocomplete our code in real time. In my case, I like to use Eclipse IDE. Let me show you a the car
- You can download the Spring Tool Suit (which is a preconfigured Eclipse for maven, spring, etc ) that we will be using in the spring that follow from here.
- If you already have Eclipse, and you don’t know if you’ll not follow the spring series, you can look for the maven plugin in Help > Eclipse Market Place …
- You can download the Eclipse IDE for Java EE Developers, which has integrated maven plugin but not spring.
Once you are running the Eclipse IDE of your choise (or other IDE, I don’t know), you just go to File > Import… > Maven > Existing Maven Projects > Browse and choose the root directory of your project (in this case maven2example_logic). Your project will charge then and everything down “src” folder will show up as java libraries.
Note you have a division named “Maven Dependencies” with all your libraries charged inside, and not also now you are able to use such importet content.
maven in eclipse
We are done for today. We had a very practical tour in maven’s world and now we are ready for the spring series: spring is an open source framework for scalable web applications in Java, keep following ProgSchedule because they are comming soon!
Maven’s Apache site – What is Maven?
IBM – 5 cosas que usted no sabía acerca de… Apache Maven
Wikipedia – Apache Maven