Pages

Wednesday, November 6, 2013

Maven 101: Day 3 :Compile, Test, Package and Install

 
Goto Prev: Understanding POM


Maven loves its directory structure..:)


Maven is all about standardization, it has a default directory structure and the dudes and dudettes at Maven do insist that we stick with the default/recommended directory structure as much as possible.
Now they have provided a way for us to override it using the project descriptor see Apache Maven Tutorial : Directory structure explanation. I am not even considering going down that route. I am happy with the default and recommended structure.

I plan to go back to inspecting the directory structure after each step ie, compile, test and package.

 

Compile

Finally time to run mvn compile.

Here is how the project looks before I hit mvn compile.



So the structure is 

/namaste/src
/namaste/src/main
/namaste/src/main/java
/namaste/src/test
/namaster/src/test/java

namaste: The project folder
src: This folder contains all the material required for building the project
main: This folder contains the material for the main build artifact
test: This folder contains the material for unit tests
java: You will find this folder under both main and test. Under the java folder we see the package structure. This is the folder where you will place your java files.

/main/java/com/gsm/app contains the class App.java.

This is how App. java looks.



/test/java/com/gsm/app contains the test class AppTest.java. This class implements the JUnit test for App.java.
Here is the screen shot for AppTest.java

Next step mvn compile. 

Now that I have a fully compiled maven project let me inspect the directory structure.



So compiling the project has added the folder target. This folder is a peer to src and created under namaste.

So the location of the folders created after compile is:

/namaste/target
/namaste/target/classes

Under the classes folder we see the project structure and the java class, App.class.

target: This directory holds all the output of the build

Test

Next step, mvn test. This runs the maven test and two new directories get created.

The test classes get compiled and placed under /namaste/target/test-classes.
A test report is created and placed under /namaste/target/surefire-reports.

Package

Next I ran mvn package.
This resulted in the creation of the jar and the maven-archiver folder.
Both these artifacts were created under the /namaste/target folder
Here is a screen shot of the target folder after I ran mvn package.


Under the maven-archiver folder, I spotted a pom.properties file, I am yet to figure out what do with it. Maybe someday I will be mavenlightened enough to know the answer to that, for now moving on.

Install

Ran mvn install and that copied the files to ./m2/repository. It created the following folder structure under ./m2/repository.
/com/gsm/app/namaste/1.0-SNAPSHOT
The 1.0-SNAPSHOT folder contained the following files.


Question: What is a Maven Repository?
 Detailed answer to this question can be found at :Intro to Maven Repos.
However, here is the summary:
There are two types of repositories remote and local. Remote: These are hosted repositories that contain artifacts that need to be shared among various developers. Maven's central repositories are remote repositories that contains the common maven artifacts. Local: which is a copy of your installation, usually lives on your local box and contains artifacts that you downloaded from the remote repositories and the artifacts created by your build effort.

Most corporates have an internal hosted remote repository, this repository has artifacts downloaded from the Maven Central repositories and   artifacts released by the the internal team.
A repo manager is usually used to host such a repository ex. Sonatype Nexus


 Phew !!!! So ready to call it a day.

No comments:

Post a Comment