Maven
What is Maven?
It is a
Build tool – building a code in a development environment
Project management tool – it helps to generate reports, helps
in the dependency management, etc.
Maven as a Build tool.
Why we are using Maven?
To reduce the common problems and activities which are
needed, when we are developing applications.
1. Multiple jars – Program may contain one/many
frameworks and frameworks are need to include it all the required “jar”. “jar”
are need to available in compile time, need to bundle them in the distribution.
(We can miss something/ we don’t know what is jar?)
2. Dependencies and versions – a jar can depend on
another jar, so we have need to make sure that all my dependencies are closed
and make sure that I have supplied all the dependencies. Dependencies could
differ bases on the versions.
3. Project structure – Proper structure for the
application. (E.g. Directories, libraries , etc.)
4. Building, publishing and deploying
Repositories – helps Maven to overcome the above
mention common problem and activities.
Make a Simple Maven
Application.
Steps:
1. Make a directory [mkdir myapp]
2. Move into the directory [cd myapp/]
3. To download Maven Repositories from online [mvn
archetype:generate]
Note – Make sure that you are connected to internet
//this command will download
all the Maven plugging.
Choose a number ? Go with the default number what is
it showing. (press enter)
What is archetype? It is a model how we want our project.
(predefined)
Choose a version number? 6 & enter
Define value for property ‘groupId’? org.apache.maven
//similar to the Package
Define value for property ‘artifactId’? MavenSampleApp
//similar to class name – name for the complete application
Define value for property ‘version’: 1.0.SNAPSHOT? Use
as it as default NOW
Define value for property ‘package’: org.apache.maven:? Use
the default selected GroupId
Ask conformation for the inputs what we did now. Press ‘y’
and enter.
FINISHED
It will create a directory named ‘MavenSampleApp’ as we
created. And inside to that directory there is a ‘src’
directory and a ‘pom.xml’ file.
Example: Information about the application - ‘pom.xml’
<project
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven</groupId> //entries
we have entered//
<artifactId>MavenSampleApp</artifactId> //entries we have entered//
<version>1.0-SNAPSHOT</version> //entries
we have entered//
<packaging>jar</packaging> //entries we have entered// application
we are writing will be build and package as a jar.
<name>MavenSampleApp</name> //name of the application. Differ from
artifactId. (Application
call by this name but saving this compiled application in the repository
artifactId will be use) //
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies> //this the place where Maven going to
play
<dependency>
<groupId>junit</groupId> //this project is depend on the junit
<artifactId>junit</artifactId>
<version>3.8.1</version> //junit version
<scope>test</scope>
</dependency>
</dependencies>
</project>
Compile the Maven
Application
Default: There will be a Java class with Main Method having
the out put of “Hello World” (selected archetype – create this automatically)
Steps
1. Go to the directory which has ‘pom.xml’
2. [mvn compile]
//downloading all the
relative dependencies mentioned in the ‘pom.xml’ + compile the Java class in
the application directory. (all the classes in the
‘src’ folder compiled in a single command)
3. [mvn package]
//it will package into a jar
file + some downloads of plugging
After this done you can see a build jar inside the ‘target’
directory which is inside our applicatio directory.
Example: [INFO] Building jar:
/home/benjamineubuntu/myapp/MavenSampleApp/target/MavenSampleApp-1.0-SNAPSHOT.jar
4. Execution [java -cp target/MavenSampleApp-1.0-SNAPSHOT.jar
org.apache.maven.APP]
Note : Make sure you are inside the directory where the
‘pom.xml’ file is saved.
//java -cp
target/MavenSampleApp-1.0-SNAPSHOT.jar [artifactId][.][Java class wants to run
(without ‘.java’)]
5. Display output as ‘Hello World!’
What maven has done up to
that?
©IT Today
Comments
Post a Comment