Monday, October 14, 2013

Understanding Maven Concepts


  • Maven builds are controlled by Project Object Model (POM) files.  POM files, unlike Ant scripts, describe the project. A POM file doesn’t have code or task steps, it just describes the structure of the project .
  • Maven manages dependencies between projects.
  • If your project needs a jar file like Log4J, then add dependency to your POM file and Maven will automatically fetch that jar file and make it available while compilation .
  • When your own code is compiled and stored by Maven, it also has to have a version number. Version numbers with ‘SNAPSHOT’ in them are special. They’re “work-in-progress” code. Maven breaks components into ‘releases’ and ‘snapshots’. It won’t allow you to store components that have snapshot version numbers as releases.


If Project Structure  is  having "Standard Directory Layout" i.e.

/src
  /main
    /java - All your Java source files, e.g., /com/mycompany/Main.java
    /resources - Resource files that need to end up in the project JAR
    /webapp - Web files if you’re building a WAR
  /test
    /java - All your test Java source files
    /resources - Resource files needed for testing
/target - Everything generated by the build, include the JAR/WAR file

Then your POM file would look like this:


groupId will identify your project uniquely across all projects, so we need to enforce a naming schema. It has to follow the package name rules, what means that has to be at least as a domain name you control, and you can create as many subgroups as you want

artifactId is the name of the jar without version ,The name of the artifact the project creates .

version if you distribute it then you can choose any typical version with number ,  The version number of the artifact created

  • So, in our above example, the final JAR file for our project would be ‘my-project.jar’ but it would be registered with Maven as com.mycompany.my-project.
  • Version numbers with ‘SNAPSHOT’ in them are special. They’re “work-in-progress” code  .
  • Maven breaks components into ‘releases’ and ‘snapshots’


Some common goals:
  1. clean remove any old build files
  1. compile compile the code
  1. test compile the code and run the unit tests
  2. package compile and test the code and create any build products
  3. site generate site documentation


  • Maven components are stored in repositories(like nexus ). You can define as many repositories as you like, but you can’t install your own JAR files
  • Each developer also has their own “local” repository. The local repository is just a set of directories on each developer’s machine (located under ‘.m2’ in the user’s home directory). When Maven downloads components from repositories they are stored locally. Developers can also install components directly into the local repository
  • When you start adding dependencies to your POM files, Maven will attempt to locate them in one of it’s repositories and then download them to the local repository.
  • This POM file will cause Maven to download the Log4J 1.2.13 component and make it available on the classpath while compiling the code. If you were creating a WAR file, EAR file, etc. it would also copy the jar file into the ‘lib’ directory of the final package.
  • optionally, give them a scope. The scope tells Maven whether the dependency is only necessary when testing the code or only necessary to compile but not for packaging.

  • When you’re working in a team, you need to be able to share your custom components between team members.
(By Sona Type)