how to use maven
DESCRIPTION
Maven how to use as command line install get workand Setting up a simple Apache Maven 2 projectand how you can install maven-plughin in Eclipse and how you can use maven wthitin Eclipse by exampleCreating your first Maven ProjectTRANSCRIPT
HOW TO USE MAVEN2
Maven Tutorial by example
By Martin Nad [email protected]
OVERVIEW
This documentation is about how to use maven by practical example and show how to install Maven in different environment as windows or Linux-like system even how to install Maven on eclipse and NetBean
TABLE OF CONTEXT:
FORWORD
GET AND INSTALL MAVE
MAKE YOUR APPLICATION-ARCHETYPE
MAKE PACKAGE
OTHER KIND OF ARCHETYPE
USING MAVEN whitin Eclipse
GET PLUGING FOR MAVEN WITH ECLIPS
USING MAVEN WITH ELCLIPSE
MAKE PACKAGE WITH ECLIPSE
DEFINE THE VERSION OF JDK IN MAVEN
SOME TIPS
DEFINE THE VERSION OF JDK IN MAVEN
HOW TO EXCLUDE A PACKAGE FROM ANOTHER PACKAGE
DIFFERNCES BETWEEN TEST AND –PTEST
SOME PRACTICAL CONFIGURATION WITH ECLIPS
MY OTHER DOCUMENTATION
How to Use Maven2
FORWORD
Maven is frame work to manage a project.
There are some similarly between Ant and Maven, but the first thing you should remember is that Maven is not Ant.
It is very easier to work with Maven then Ant and it is very simpler too.
You can compare Ant with “make” function in c and c++ programming languages.
Ant gives you possibility to make your application.
Maven even manages your application and it will make your application too.
FIRST STEP:GET AND INSTALL MAVE
You can download Maven from:
http://maven.apache.org/download.html and just unzip it and put every where you want and just remember that put /maven/bin/ in your path; and don’t forget to put your JAVA_HOME on your system-environment.
SECOND STEP: MAKE YOUR APPLICATION-ARCHETYPE
Now you can make a new maven project/archetype by this command:
$ mvn archetype:create
-DgroupId=mygroupid
-DartifactId=myapp
-DarchetypeArtifactId=maven-archetype-quickstart
Here myApp is the name of your applicationmygroup is the groupid of your application.
Maven-archetype-quickstart is the type of archetype that you want to use
If you run it for first time, you should get lot of information about downloading different packages by this commandAnd you will see that maven works and you will definitely get a message by maven that Archetype created in dir: ./myappAnd .....
BUILD SUCCESSFUL
Maven tell you even which directory you have used and which directory it put your application package.When you run that command-line, you will get more information about createArchetype.Just change the directory to my-app
THIRD STEP: MAKE PACKAGE
Cd my-app and just write this commandMvn package
Exactly as before, Maven wants to download those packages that it need to compile and it will make a package of your empty application.You will see those lines at the almost end of the output of the mvn-commandBuilding jar: myapp/target/myapp-1.0-SNAPSHOT.jarBUILD SUCCESSFUL
And it means that maven could successfully build your package and put it on the specific directory. You should to know it would run the test programs if you have some in your test-directory.
And so easily you have created your first application with maven and you could successfully make a package
There is another useful command in maven:Clean, to clean your application, what I mean, it will delete all classes ,files and directory and build everything from beginning.Usually I use this command for every time I want to make a new package
Mvn clean package
Maven will first clean the application and then makes a new package for you.You can run your application as follow:
Java -cp target/myapp-1.0-SNAPSHOT.jar mygroup.App
and if you don't get any error and if it doesn't write either anything, it sounds that you could run your application and everything was successful. It doesn't return any value because you didn't edit or change the App.java
App.java is the default Java file when you use maven-create-archetype
OTHER KIND OF ARCHETYPE
Maven has several archetypes that you can just use them very easily and it will make all class hierarchy for you.Here you find some of them:
Maven-archetype-quickstartMaven-archetype-siteMaven-archetype-site-simpleMaven-archetype-webapp
If you want to get a list of archetypes and choose from the list you can just write this command:
mvn archetype:generate
you will get a list of the archetypes and very simply choose a certain archetype and just give a archetypeid and groupid and you make a new project-structure.
Unfortunately some of them are not complete as webapp, you need add some folder for it.
For instance I can give here some example for building different archetypes:
You can read my documentation about how you can create your custom archetype in maven, and reuse it.
But now here I will show how you can create an application with command-line from an existing archetype for example webapp
for java module, ejb module and enterprise application module you can use this archetype and this command:
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=myProjectArtifactId -DartifactId=myJavaModule1 -DpackageName=my.package -Dversion=1.0
and for for application module:
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=myMultiProjectGroupId.myProjectArtifactId -DartifactId=myWebApplicationModule1 -DpackageName=my.package -Dversion=1.0
Here you are:
mvn archetype:create -DgroupId=my company -DartifactId=myWebapp -DarchetypeArtifactId=maven-archetype-webapp By this command you can use the maven-archtype-webapp and make your application.Please make some sample of archetype and look at the class hierarchies.You can change the value of the –DgroupId= to something else and you can change the name of your application by give another name to the –DartifactiId=
USING MAVEN WHITIN ECLIPSE
GET PLUGING FOR MAVEN WITH ECLIPSE
In the Eclipse user: “Eclipse -> Help -> Software Updates -> Find and Install”
then “Search for new features to install”,
then click on “New Remote Site”,
and enter the Maven Projects name (e.g. Maven2Plugin) and its home URL there “http://m2eclipse.codehaus.com“.When you are finished successfully you should work with maven inside Eclipse.Sometimes Eclipse doesn’t work very well with Maven, you should remember, in this case that you get some problem with
packaging or using maven commands in the Eclipse, clean the projects in Eclipse and Eclipse shouldn’t build automatically the projects.I use just maven to my project and I don’t use the build automatically option at all.
USING MAVEN IN ECLIPSE
UNFORTUNATELY, Eclipse hasn't support to have an hierarchy structure of project. If you need or want to make some hierarchy project structure, you should use manually Maven command and build your project-structure and then import into the Eclipse. Later I will give an example to how make an hierarchy project structure by using Maven.
Remember that this option”build automatically” shouldn’t be active.
I got lot of problem with this kind of issue.
If something doesn’t work, or was strange when you want to compile, or make package, just try to clean the project from here.
MAKE PACKAGE WITH ECLIPSE
DEFINE THE VERSION OF JDK IN MAVEN<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build>
HOW TO EXCLUDE A PACKAGE FROM ANOTHER PACKAGE <exclusions> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> </exclusions>
WHAT IS DIFFERENCES BETWEEN "MVN TEST CLEAN PACKAGE" AND "MVN -PTEST CLEAN PACKAGE"
as you can see the differences in syntax is just "-P" when you use "-p" maven would make a package as you defined in you your profile "test" in the pom.xmlyou can have several profile, as I use my profiles are, ”local", "product", "staging", "test".advantages to use profile in your pom.xml is you can define your environments in your profile, if you use for example database, you can define different database connection, username, password for each profile. As:
but if you just use "mvn test clean package" it will just run the test-classes and make a new package.If you want just test your junit or other kind of test-classes it is good idea just run this command.
Be continuing….
For donation use this link
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5814019
or
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5813954
MY OTHER PAPERS
Properties in Java and Spring by Martin Nad
Spring and Cxf by Example
Jboss and Perm Gen Error
Using Ftp Service With Spring
JunIt
How to use Maven
ReFactoring
Maven and Custom Archetype
How to Write Effective Code
Using Generic in Java Why and where