Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Tuesday, 17 May 2016

What are the element defined in pom.xml ?


pom.xml elements

  • project : Root element
    • modelVersion : Set to 4.0.0
    • groupId : ID for project group
    • artifactId : Project / Artifact Id
    • version : Project version under given group
    • packaging : Packaging type - jar / war / ...
    • name : Name of maven project
    • url : Project URL
    • dependencies : Dependencies of the project
      • dependency : A dependency under dependencies element
        • groupId, artifactId, version
        • scope : Scope of Maven project :
                       compiled / provided / runtime / system / test


Example
<project ...>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.shaan.project</groupId>
  <artifactId>myapp</artifactId>
  <version>1.0</version>

  <packaging>jar</packaging>
  <name>My sample App</name>
  <url>http://maven.apache.org</url>

  <dependencies>
     <dependency>
          <groupId>junit</groupId>  
          <artifactId>junit</artifactId>  
          <version>4.2.2</version>  
          <scope>test</scope>
     </dependency>
     ...
  </dependencies>  
  ...
</project>

Monday, 16 May 2016

What is pom.xml and how it is different from project.xml ?


pom.xml

  • POM = Project Object Model
  • contains project info and configuration - dependencies, source dir, test dir, build dir
  • Maven reads pom.xml and execute the target


pom.xml vs. project.xml

  • Before Maven 2, project.xml is used
  • pom.xml is used in Maven 2 and later versions


What is the fully qualified artifact name of Maven project ?


<groupId>:<artifactId>:<version>

How to check installed Maven version ?


Check maven version
mvn -version

How to install Maven ?


1. Download Maven
Download the zip from http://maven.apache.org/download.cgi and extract in your local system

2. Set MAVEN_HOME
In env vars, add MAVEN_HOME = <home-dir-of-maven>

3. Add Maven to Path variable
In path variable, add / append path of Maven
%MAVEN_HOME%/bin

4. Verify maven installation
mvn -version

What is the Build life cycle of Maven project ?


1. validate : Validate if the project is correct
2. compile : Compile the source code
3. test : Test the compiled source code using test framework
4. package : Make packaged distribution of the compiled code 
5. verify : Run checks on integration test results
7. install : Install package into local repo
8. deploy : Copy the package to remote repo

Local repo vs. Central repo vs. Remote repo


Local repo

  • created by maven in local system when you run any maven command.


Central repo

  • created on internet by maven community (Not a full set of libraries)


Remote repo

  • Also on internet by different vendors and used inside pom.xml 



What are the functions of a Build tool ?



  • Generate documentation from source code
  • Compile source code
  • Create jar/war/tzg package of compiled code, properties files and dependent JARs
  • Sync the package to the repository

Which problems can be solved by Maven ?



  • Managing dependent libraries
    • In case of using frameworks, there must be a lot of JARs to include in project.
  • Enforcing correct project structure
    • according to frameworks used (JEE, Struts, Spring etc.)
  • Project Build and Deployment


Sunday, 15 May 2016

What are different repository types in Maven ?


Maven repo
  • A directory of JAR file with pom.xml file
  • used to search the dependencies
    • Order of searching dependencies : Local -> Central -> Remote

1. Local
  • in local system (Default : %USER_HOME%/.m2 directory)
  • To change location : MAVEN_HOME/conf/settings.xml
  • Directory automatically created when you run any maven command

Change location of local repo in settings.xml
<settings>
  <localRepository>/path</localRepository> 
</settings>


2. Central

3. Remote
  • Available on internet
  • Some libraries which are not available on Central repo , they can be found in Remote repo
  • Libraries (to use from Remote repo) are specified in pom.xml

Using libraries from Remote repo
<project>
  ....
  <dependencies>
     <dependency>
         <groupId>org.apache.wicket</groupId>
         <artifactId>wicket</artifactId>
         <version>1.4.21</version>
     </dependency>
  </dependencies>
</project>