This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Ant, Maven and Other Build Tools and the fly likes Learning Maven Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "Learning Maven " Watch "Learning Maven " New topic
Author

Learning Maven

Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 266
Hi All,

I am completely new to Maven. I am pretty excited to learn Maven. I have installed Eclipse Java EE IDE for Web Developers. Version: Juno Service Release 2.

I also installed Maven Integration for Eclipse WTP in Eclipse.

I downloaded the latest version of Maven (3.0.5) from: http://maven.apache.org/download.cgi

Now I created a Maven Project : File -> New -> Other -> Maven -> Maven Project -> Next -> Which group ID and artifact ID do I choose ?

How do I make a war file?

Suppose I want to try out a simple web project and would like to make a war file for it and deploy in JBoss server.

How to proceed. Feeling very confused.

Not getting help from anywhere

Please do Help and Guide me to learn Maven !!!

Sid.
Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Hi,

Now I created a Maven Project : File -> New -> Other -> Maven -> Maven Project -> Next -> Which group ID and artifact ID do I choose ?


Eclipse -> File -> New -> Other -> Maven -> Maven Project -> Next ->
Then you could select and archetype, set filter to 'web' and select 'maven-archetype-webapp', click next.
Now set group id "my-webapp", artifact id "my-webapp-test" and package "my.webapp.test".
You are done, browse your maven webapp project.

Click your project / run / run as / click maven build / click on new launch configuration (icon with a sheet +) / select your maven project as base directory and set goal as package / run
Now you have your project packaged as a war in the target directory.


Best regards
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 266
Carles Gasques wrote:Hi,

Now I created a Maven Project : File -> New -> Other -> Maven -> Maven Project -> Next -> Which group ID and artifact ID do I choose ?


Eclipse -> File -> New -> Other -> Maven -> Maven Project -> Next ->
Then you could select and archetype, set filter to 'web' and select 'maven-archetype-webapp', click next.
Now set group id "my-webapp", artifact id "my-webapp-test" and package "my.webapp.test".
You are done, browse your maven webapp project.

Click your project / run / run as / click maven build / click on new launch configuration (icon with a sheet +) / select your maven project as base directory and set goal as package / run
Now you have your project packaged as a war in the target directory.


Best regards


Thanks Charles,

I rt. clicked on my project. selected Run As -> Maven build...

I am not getting any new lunch configuration.... Also how to select my maven project as base directory ..... set goal as package / run. ???

Also I need to add dependency and plugins as well..... For example dependency for servlets-apis jar files, jstls, etc..

Please consider me as a layman in maven and explain in detail.

Thanks
Sid.

Carles Gasques
Ranch Hand

Joined: Apr 19, 2013
Posts: 199
    
    1
Sorry,

Click your project / run / run as /run configurations / click maven build / click on new launch configuration (icon with a sheet +) / select your maven project as base directory and set goal as "package" / click run button
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5835
    
    7

The other possibility is to ignore the various archetypes and instead go with a simple Maven project (the top checkbox) on the first page New Maven Project dialog, then on the next page choose "war" in the packaging dropdown. That will give you a basic WAR project.

What you use for GroupId and ProjectId is up to you. You really need to examine how others have set these up. The groupId usually matches the basic package name for the classes in the project. For example, you can find groupIds that start with "org.apache", "org.jboss", etc.

For the projectId, use the name that you want to give to your WAR file. That name will also be the name used for the project directory in the Eclipse workspace.

It might help if you went through a simple Maven tutorial for building a web application. You can find on in chapter 4 of Better Builds with Maven (http://www.maestrodev.com/support/)


JBoss In Action
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 266
Peter Johnson wrote:The other possibility is to ignore the various archetypes and instead go with a simple Maven project (the top checkbox) on the first page New Maven Project dialog, then on the next page choose "war" in the packaging dropdown. That will give you a basic WAR project.

What you use for GroupId and ProjectId is up to you. You really need to examine how others have set these up. The groupId usually matches the basic package name for the classes in the project. For example, you can find groupIds that start with "org.apache", "org.jboss", etc.

For the projectId, use the name that you want to give to your WAR file. That name will also be the name used for the project directory in the Eclipse workspace.

It might help if you went through a simple Maven tutorial for building a web application. You can find on in chapter 4 of Better Builds with Maven (http://www.maestrodev.com/support/)


Hi,

I managed to make a war file successfully through Maven.

What I am doing is for a new maven project:

File -> New -> Other -> Maven-> Maven Project -> (Next)

Chose Filter as : webapp

It shows as:

Group Id: org.apache.maven.archetypes
Artifact Id: maven-archetype-webapp
Version: RELEASE.

Clicked on Next

Entered the Artifact Id and GroupId and clicked on Finish.


I am basically trying to learn Servlet Concepts by running the practical examples. I am using JBoss 7.1 as my Application server.

Following is my pom.xml



My Queries:

1) If I am creating servlets then I would need to compile Java source files to build .class files. How do we do this in maven ? Would .class files automatically become a part of the war file? What I need to modify in my pom.xml file ?

2) I want the generated war file to deploy automatically in the Jboss folder. Currently I have to copy the war file from its generated location to the JBoss folder (Jboss->standalone-> deployments)

For generating a war file I write click on pom.xml -> Maven build ... -> Goals - package -> Click on Apply and Click on Run

Please guide and help me ....

Thanks and Regards,
Sid.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5835
    
    7

1) Yes. Place your servlet and related Java sources in src/main/java. Maven will automatically include the compiled class files in WEB-INF/classes in the WAR file. You do not need to do anything special in the POM to get this behavior.

2) There is a Maven plugin for JBoss AS 7 that will do this for you. See http://docs.jboss.org/jbossas/7/plugins/maven/7.4.Final/
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 266
Peter Johnson wrote:1) Yes. Place your servlet and related Java sources in src/main/java. Maven will automatically include the compiled class files in WEB-INF/classes in the WAR file. You do not need to do anything special in the POM to get this behavior.

2) There is a Maven plugin for JBoss AS 7 that will do this for you. See http://docs.jboss.org/jbossas/7/plugins/maven/7.4.Final/


Hi Peter,

1) I am having source folder as : src/main/resources and not src/main/java

Are we trying to say that the java source files would compile only when they are kept in the folder src/main/java and not any other folder such as src/main/resources?

In the dialog box for selecting an archetype I am giving the filter as : webapp The GroupId, ArtifactId and Version which I choose is as follows:

Group Id: org.apache.maven.archetypes
ArtifactId: maven-archetype-webapp
Version: RELEASE

Am I missing something OR doing something wrong ?

Kindly advise

Thanks and Regards,
Siddharth
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5835
    
    7

That is correct. Java source files need to be in src/main/java. Other files that you expect to be in the classpath, such as properties files or the log4j.xml file, should be in src/main/resources. That the difference between a source file (which is processed by the Maven Compiler Plugin, and resource files, which are processed by the Maven Resources Plugin.

What books have you read on Maven? Have you looked at:
Better Builds with Maven (http://www.maestrodev.com/support/) - a little dated but still contains good tutorials to get you started (has a comlete tutorial on building web apps)
Maven, the Complete Reference (http://www.sonatype.com/resources/books) - I reference this one so much I have it pinned to my Nitro PDF jump list
Both are free PDF downloads and recommended reading.
Ed Burns
author
Ranch Hand

Joined: Sep 11, 2006
Posts: 82
    
    5
Because mastery of Maven is essential to effectively using Hudson, we cover Maven extensively in our book, "Hudson Continuous Integration in Practice". Here is a little excerpt from Chapter 5: Hudson and Automated Testing.


TIP
Maven is an extreme example of the principle of “inversion of control.” The key to success with Maven is to work with it, not against it. This is best achieved by avoiding trying to change how Maven does things. Rather, it is best to find out the way Maven intends to do the sort of thing you want and then provide input to that process.

All of the action in Maven happens on top of the backbone of the build lifecycle. All of the work done by Maven during a build is done by plugins executing at specific phases during the build lifecycle. Each specific thing that a plugin does is called a “goal.” Plugins typically declare in which lifecycle phases their goals are designed to execute. There are actually three lifecycles that are used in Maven, called clean, default, and site. Every invocation of mvn will use at least one of these three lifecycles. When analyzing its command-line arguments, Maven determines which of the three lifecycles to run and the end phase for each one, based on the order in which the arguments are specified. The clean and site lifecycles deal with running the clean and site goals. Clean resets the project to its nonbuilt state, usually just deleting the target directory. Site generates reports, such as code coverage and unit test results. The default lifecycle is the most interesting and is where most of the work is done by Maven and its many plugins. Hudson jobs that invoke Maven will usually have cause to use all of these different lifecycles in one way or another.
Each of the three lifecycles consists of several different phases, each with a name and purpose and executed in sequence by Maven. Maven always executes at least one of the three lifecycles, and when it does, it always executes them from the start phase until the user-specified end phase. You can think of the execution of the lifecycle phases as a journey on a non-express train from the starting station through multiple stations. When traveling on a such a train from the starting station, you can get off at any station, but no stations can be skipped.


Ed
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30584
    
154

Ed Burns wrote:Because mastery of Maven is essential to effectively using Hudson, we cover Maven extensively in our book, "Hudson Continuous Integration in Practice". Here is a little excerpt from Chapter 5: Hudson and Automated Testing.

Only if you are using Maven . If you are using Hudson to run Ant scripts, mastery of Ant is essential.

And yes, I know this is a nit, but had to bring it up anyway. The Maven coverage in the book is great.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5835
    
    7

We have Maven and Jenkins well integrated at work. In fact, every project is a Maven project, thus every Jenkins job runs Maven - even the numerous jobs and projects that have nothing to do with Java. And every artifact we build goes into Nexus, even the non-Java artifacts.
 
GeeCON Prague 2014
 
subject: Learning Maven