Maven kickstart with a practical example

Maven

WHAT :  A software project management tool that can automate the build and many other build related processes.

WHY: Store all your project dependencies, document your process of  “building” onto an XML file. Think about a more visual ant. btw, Maven can generate ant scripts, which you can use for building.\

KICKER: It can download jar files from repositories, depending on what you may need. Imagine this capability when taking over an existing project.

MORE: Maven has a lot of plugins, which we can leverage just by setting up the project as a Maven project. Lets try to do a small project that will really show you the real advantage of Maven

 

Project: Generate a Java object /Bean from a json file. Use Maven.

This will generally be used when you trying to write a wrapper for all sorts of REST API and all, for example; Basecamp

Step 1: Lets look at Basecamp’s API

Step 2: We want to generate the object for the API endpoint Project

Step 3: Here is  a sample JSON


Step 4: Lets generate an XML from the JSON using this online converter 

Step 5: Now we have an XML, lets also get the XSD ( definition) using this link, we can use our XML file generated in step 4.

Step 6: Install Maven in your computer.

Step 7: Set your classpath to point to the directory where you unzipped it {install_directory}/maven/bin should the value of your classpath.

Step 8: See if the classpath setting worked by typing mvn on the command prompt/shell etc

Step 9: Lets create a skeleton project using maven. execute this command

mvn archetype:generate -DgroupId=com.mycompany.app -Dar
tifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMo
de=false

Step 9:  Check out the folder, you should have a directory called my-app in there. Look for a file called pom.xml. This is what stores all the dependency. This is also the place where you should tell maven to download different plugins.

Step 10: We will be using the JAXB plugin, this can take an XSD file and generate a corresponding Java Object.

Step 11: Open up pom.xml, and add the dependency as explained on this document

Step 12: Once everything is ready to go, open up the my-app folder, to the location described below, and create a file called “Project.xsd”

Step 13: Go to command prompt/shell , browse to my-app, and run mvn compile. This in essence, will do everything for you, it is configured to look under the folder for xsd files, download the necessary jar files for JAXB, and run the code generation. Once everything is done, take a look at the my-app folder for generated sources

Step 14: As Maven is a complete software generation project management system, there are very many “pretty-up” features. Try out the commands mvn site, mvn clean, mvn install, mvn package  etc. They are all very helpful