MainMenu

Home Java Overview Maven Tutorials

Saturday 11 March 2017

Maven Tutorials For Beginners

For Video Tutorial : Move on Youtube Channel


Note : Select the playlist as per your need & move with number sequence




For Video :-

Part:-1


Part:-2

What is a Build Tool?

If you want to run your java code independently, then it is good to integrate your project with build tool.
This build tool will setup every thing which is requied to run your java code independently.
It makes programmer's life easy while handling the huge project by providing a common platform.
It generates source code, compiling code, packaging code to a jar etc.

What is Maven ?

Maven is a build tool and it performs very simillar to Ant which is a different build tool.
Basically Maven is a Software Project management tool which provides new concept of project object model (POM).
It allows the user to automate the process of creation of the initial folder structure, performing and compilation and testing and the packaging and deployment of the final project and makes it one step process to do a build. IT Makes build consistent with another project.
Maven is also used to manage the dependencies.

What problems does maven solves ?
For project development/Testing we need a lot of JAR files like Selenium, TestNG, Spring etc..
first we need to download them and then import these JAR files in build path in project.
but with Maven we just need to add the dependencies...

Maven will go out and download the JAR files for those projects for you.
and maven will make those jar files available during compile/run.

Maven Repository
This is the place where Maven stores all the projects jars files or libraries or dependencies. By default the folder name is `.m2` and by default the location in window 7 is ‘Libraries\Documents\.m2‘.

Maven Central Repository
Mavel central repository is the default location ‘http://mvnrepository.com/‘ for Maven to download all the project dependency libraries.


Maven Local Repository : Local machine repository with in folder .m2

maven Central Repository : https://repo1.maven.org/maven2

maven remote repository : https://mvnrepository.com



Features of Maven :-
1.You can view the output of Maven commands inside the Eclipse, using its own console.
2.It does the dependency management for Eclipse build path based on Maven's pom.xml.
3.It automatic downloads the required dependencies and sources from the remote Maven repositories.
4. It resolves Maven dependencies from the Eclipse workspace without installing to local Maven repository (requires dependency project be in same workspace).


Maven is a Software Management and comprehension tool and it is based on the concept
of Project Object Model(POM) which can manage project build, reporting and documentation
from a central peice of information.
All build syystems are essentially the same :
> Compile Source code
> Copy Resource
> Compile and Run Tests
> Package Project
> Deploy Project
> Cleanup
Developers of maven wanted :
> A standard way to build the projects
> A clear definition of what the project consisted of
> An easy way to publish project information and a way to share JARs across several projects.
> The result is a tool that can now be used for building and managing any java-based project.
> Intended to make the day-to-day work of java developers easier and generally help with the comprehension of any java-based project.


What is POM ?


"As a fundamental unit of work in maven, POM is an XML file that contains information about project and configuration details used by Maven to build the project".
POM Stands for Project Object Model :-
> Describe a project
> Name and version, Artifact Type, Source Code Locations, Dependencies.
> Plugins
> Profile(Alternate build configuration)
> Uses XML by default
> Not the way Ant uses XML


POM File Structure :

pom.xml :
Project meta data : Project name, version etc. Output file type : JAR, WAR...
Meta data Example :
<groupId> </groupId>
<artifactId><</artifactId>
<version>&/ltversion>
<packaging></packaging>
<name></name>
dependencies : List of projects we depend on Spring, Hibernate etc.
<dependencies></dependency></dependency>
</dependencies>
plug ins : Additional custom tasks to run : generate JUnit test reports etc.

Maven's Objectives


> Making the build process easy
> Providing a uniform build system
> Providing quality project information
> Providing guuidelines for best practices development
> Allowing transparent migration to new features.

Maven Build life cycle :
a). validate
b). compile
c). test
d). package
e). verify
f). Install


Example :
mvn compile : make available target folder
mvn test : surefire reports
mvn verify : verify jar file
mvn intall : put jar file under local repository

Maven Basic Commonds
maven clean : cleans the maven project by deleting the target directory

Example :
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ NewFramework ---
[INFO] Deleting D:\Software\Selenium\Validate_Samples\Eclipse_Workspace\NewFramework\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.783 s
[INFO] Finished at: 2024-01-21T19:04:57+05:30
[INFO] ------------------------------------------------------------------------





maven install : builds the maven project and install the project files(jar, war, pom.xml)

Example :
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ NewFramework ---
[INFO] Building jar: D:\Software\Selenium\Validate_Samples\Eclipse_Workspace\NewFramework\target\NewFramework-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ NewFramework ---
[INFO] Installing D:\Software\Selenium\Validate_Samples\Eclipse_Workspace\NewFramework\target\NewFramework-0.0.1-SNAPSHOT.jar to
C:\Users\cchauhan\.m2\repository\NewFramework\NewFramework\0.0.1-SNAPSHOT\NewFramework-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\Software\Selenium\Validate_Samples\Eclipse_Workspace\NewFramework\pom.xml to C:\Users\cchauhan\.m2\repository\NewFramework\NewFramework\0.0.1-SNAPSHOT\NewFramework-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.621 s
[INFO] Finished at: 2024-01-21T19:06:07+05:30
[INFO] ------------------------------------------------------------------------





maven test : run the test cases of projects

Example :
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestSuite
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
www.way2testing.com

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.74 s - in TestSuite
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.766 s
[INFO] Finished at: 2024-01-21T19:08:09+05:30
[INFO] ------------------------------------------------------------------------







Install Maven in summarize form


> Install the JDK in your windows machine
> Add jdk path as "JAVA_HOME" variable in environment variable > User variable
> Add jdk bin path in path under environmental variable > system variable
> Set the Maven_Home, user variable & system variable under the Environment variable.
> verify that maven intalled(cmd >> mvn -version)

Method 1 :- Steps to install Maven in Eclipse

Step : 0. Open eclipse click on help then select the eclipse market place and in search box enter the Maven and hit enter


Alternatively you can also install the maven from Install new Software window.

Step : 1. Click on the help from the top menu in Eclipse and select Install New Software


Step : 2. Click on the Add button on the newly opened window.
Step : 3. In the Name box type your favorite name, i will type Maven and in location enter this link ‘http://download.eclipse.org/technology/m2e/releases/‘ and click on OK button


Step : 4.A check-box will appear in the pop window, Check the check-box and click on Next & Next button.
Step : 5. Accept the ‘Terms and Conditions‘ and move forward by clicking on Finish button.





Method 2 :- Step : 1. Download and Install java in your machine

Click Here for complete tutorial of Java Download & install

Step : 2.Setup java Environment Variable
Go to My Computer and select Properties
Now click on Change setting



In pop up window , click on Advanced tab and then click on Environment Variable


Step : 3. Click on New button under ‘System Variables..‘


Step : 4.Write ‘JAVA_HOME‘ in the Variable name box and enter ‘C:\Program Files\Java\jdk1.8.0_20‘ JDK path in the Variable value box and click OK. copy the path upto JDK, bin folder & edit the path variable & add it in the end.


Step : 5.Download Maven Click here to Download maven

Step : 6.Extract this zip file & copy he location in my case it is 'C:/apache-maven-3.2.3′
Step : 7.Set up the Maven Environment Variable the same way we set up the Java Environment Variable above.
Write ‘MAVEN_HOME‘ in the Variable name box then enter ‘C:\apache-maven-3.2.3‘ Maven path in the Variable value box and click OK.


Step : 8.Write ‘PATH‘ in the Variable name box then enter ‘C:\apache-maven-3.2.3\bin’ Maven path in the Variable value box and click OK.
Step : 9. Verify that Maven is installed properly or not
Open the commond prompt & type mvn -version as below :

If you see the result as above, then congratulation you have installed Maven in your machine
When you will create maven project, there will be two folders :

src/main/java
All the source code that you put, will display under the main/java package.

src/test/java
The code that you want to test, will display under test/java package.

Also :-
src/main/java : Your Java source code
src/main/resource : Properties/config files used by your app
src/main/webapp : JSP files and web config files other web assets(images, css , js etc)
src/test : Unit testing code and properties
target : Destination directory for compiled code. Automatically created by Maven.

what are the plugins in Maven?



Maven is based on plugin execution framework, you can also call plugin as feature of Maven where plugin use to do every task.

Plugins in Maven generally use to do :-

Create Jar/War file
Compile code files
Perform Unit testing on Test code (code which is present in src/test)
Create project documentation
Create Project reports.

Get Dependency

Selenium Dependency : Click here for selenium dependecies
Select the version that you want for your project.

Apache POI Dependency : Apache POI Dependency
Select the version that you want for your project.

TestNG Dependency : TestNG Dependency

PhantomJs dependency : Click here for PhantomJS Dependency
Select the version that you want for your project.

Get Maven Plugins



Maven Plugins :
Click here for Complier Plugin
Click here for Surefire plugin
Click here for TestNG plugin

Build Failure Error :

If you are getting some error, like JDK not found, src not available etc. then just copy the below code & paste it just above to </project> and update your Maven project

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<suiteXmlFile>src/main/resources/testng.xml</suiteXmlFile> </properties>
<dependencies>
<dependency> <groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.6.0</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>



If you are facing below issue
then here is the solution :-

Unable to locate the Javac Compiler in:
C:\Program Files (x86)\Java\jre6\..\lib\tools.jar
Please ensure you are using JDK 1.4 or above and
not a JRE (the com.sun.tools.javac.Main class is required).



Open the eclipse --> Navigate to Window --> Preferences --> As shown in below image



Now in the popup window go to Java --> Installed JREs as shown in below image



Now select the JRE & click on Edit button as shown in below image



Now click on Directory button in JRE installer window as shown in below image



Now go to your directory "C" or where you have installed your JDK file, select it as shown below



After that click on OK, Apply, OK ans so on.....
Now refresh & update your project
Congratulation Your issue has been resolved. :)

Create Maven Project in eclipse



First i will create a Maven project in my eclipse as below :


In maven project, inside the main/test folder i will write my code , which will be tested.


Now open the POM.xml file and add the dependency & plugin as you want in your project & save it.


Now run your project as Maven test


Tags :

Maven Tutorials for Beginner

How to setup maven with eclipse

How to Install Maven in eclipse

Latest Maven Tutorial 2017

Maven Tutorial 2017

How to setup path variable for Maven

How to setup Environment variable for Maven

No comments:

Post a Comment