Tutorial: Using Maven to build your J2EE Projects

Discussions

News: Tutorial: Using Maven to build your J2EE Projects

  1. Tutorial: Using Maven to build your J2EE Projects (35 messages)

    Maven is a high-level, project management, build and deployment tool from the Apache project that adds a layer of abstraction above Ant. Using extensive code samples, this article shows you how to use Maven to set up a project template, compile source files, create a JAR and publish the artifact into a repository. It covers inheritance in the Maven POM (Project Object Model), shows you how to set up a sample J2EE project, create WARs, EJB-JARs, dependency JARS, build the EAR file and how to use the Maven Reactor.

    Read Maven Magic

    Threaded Messages (35)

  2. My God! This is more documentation on Maven than I've ever seen, does this mean it's getting close to being finished at long last? Great tool, highly recommended!

    -John-
    C24.biz
  3. My God! This is more documentation on Maven than I've ever seen, does this mean it's getting close to being finished at long last? Great tool, highly recommended!

    -John-
    C24.biz
  4. "Maven on the other hand mandates certain directories and file names, but it provides plugins to make life easier."

    One question I have is does a jar/war/ear have to be in a certain directory structure, or can you tell Maven where each src might be? Looking at it, it looks like it's smart enough so you can tell it where the source is in each project.xml.

    Steve
  5. For this you'll have to define a number of undocumented properties (read Jelly code for default Maven plugins) or you can write your custom plugin (that's what I did).
  6. Which properties are considered undocumented?

    http://maven.apache.org/reference/project-descriptor.html
    http://maven.apache.org/reference/plugins/war/properties.html
    http://maven.apache.org/reference/plugins/jar/properties.html

    These seem to cover it...
  7. For this you'll have to define a number of undocumented properties (read Jelly code for default Maven plugins) or you can write your custom plugin (that's what I did).
  8. Basically each deliverable has its own project ... and you have the multi-project structure that Srikanth outlined (and so in each project.xml you specify where the source is for that part).

    Good article and useful in getting people started on Maven with a real example that they can associate with. Srikanth didn't touch on the website generation capabilities of Maven and in particular the "multiproject" plugin (which uses the Reactor). The "multiproject" plugin is a first stab at linking together multiple (dependent) projects (and since Maven guides people to have separate projects for each deliverable, is necessary).
  9. We are making intensive use of Maven since a year.
    We manage to continuously integrate the code from 110 devers each two hours.
    It has many plugins, some OS, some not (like clover for test coverage).
    But it would need a GUI to administer all the scripts and our experience shows you must dedicate one full time equivalent people per 50 devers...

    I would suggest, IDE vendors take that in consideration and propose some kind of propriatary build engines or at least GUI for Maven.

    I think AntHill is a $product that intends to do the same; see also cruise control (Os).

    Laurent.
  10. Main Entry: ma·ven
    Variant(s): or ma·vin /'mA-v&n/
    Function: noun
    Etymology: Yiddish meyvn, from Late Hebrew mEbhIn
    Date: circa 1952
    : one who is experienced or knowledgeable : EXPERT; also : FREAK 4
  11. Many thanks

    Recently , i am using maven to build, integrate, manage our project , it's pretty good!

    more resources

    www.onjava.com/pub/a/onjava/2003/10/22/maven.html
    www.javausergroup.at/events/maven.pdf
    http://www-106.ibm.com/developerworks/java/library/j-maven/

    maven chinese site
    www.huangdong.com

    founder_chen
    www.huihoo.org
    Open Source Middleware Community
  12. Maven seems good but....[ Go to top ]

    I've been interested by the article because everybody around me tell me that maven is a great tool that simplifies the build process. I wanted to have more documentation before spending time on evaluating this tool.

    This article help me understanting the concepts but I still have some doubts.

    Explain me how MAVEN would resolve this practical build process:

    I have a project which deals with client and server sources. The server sources are used to produce a war file. The client sources are used to produce a jar client file. Some of the sources (4 classes )are shared by both server and client.

    When I want to test my project I must use my WAR file with an ejb JAR to deploy a testing EAR. Then my client test code can perform some call on my server to validate the war.

    To understand the script in detail you can have a look at the actual ant build file at :
    http://cvs.sourceforge.net/viewcvs.py/*checkout*/bsframework/fwk2/remoting/build.xml?content-type=text%2Fplain&rev=1.5

    How does MAVEN handles this kind of problem with its limitation of producing only one artifact per project?

    Gaetan Zoritchak,
    Bright Side Factory,
    http://www.bs-factory.com
  13. ... but try this[ Go to top ]

    OK, to give you an *idea* of how you could handle your system, try a project structure like this

    project.xml - TopLevel project
    common/project.xml - Shared source project (creates JAR)
    client/project.xml - Creates client (dependent on "common", creates JAR)
    server/project.xml - Creates server (dependent on "common", creates WAR)

    The testing parts of each project can be configured to bundle in things that are needed - specified in a separate part of the project.xml. Are you testing with JUnit, or with some other tool ? Its likely that in the Maven plugin for your testing tool (JUnit, Clover, etc) that you can create the EAR no problem.

    The bottom line is that Maven targets you to create a single deliverable per project, so you use multiple projects with interdependencies. You CAN create multiple deliverables in a project by use of the maven.xml file, particularly for one-off type situations.


    I'd have no doubt that your situation can be handled easily enough, using the above as a template. If in doubt try the maven users mailing list and you'll get an answer from someone who has done it.

    HTH
  14. ... but try this[ Go to top ]

    Thanks for your answer.

    I find this solution quite "heavy". In this example, the commons classes represents 7 classes, the server adds 4 classes, the client 3 classes. I don't find that using 3 projects to handle few classes helps a lot!!!

    Gaetan Zoritchak,
  15. Re: ... but try this[ Go to top ]

    Thanks for your answer.

    >
    > I find this solution quite "heavy". In this example, the commons classes represents 7 classes, the server adds 4 classes, the client 3 classes. I don't find that using 3 projects to handle few classes helps a lot!!!
    >
    > Gaetan Zoritchak,

    The example project was meant to illustrate the interdependency among projects.
    Hence the three projects were created.

    It does in no way mean that number of projects is proportional to the number of classes.

    I would put it like this: The number of projects is dependent only on the granularity of dependency that you want.
    Niothing prevents you from having a common project with perhaps 1000 classes in it.

    Hope that helps,
    Srikanth
  16. Error building the sample code[ Go to top ]

    Perhaps I am missing something from the example but I am getting the following error when I execute maven foobar:build-all

    Attempting to download xdoclet-web-module-1.2b4.jar.
    WARNING: Failed to download xdoclet-web-module-1.2b4.jar.
    Attempting to download jakarta-struts-1.0.2.jar.
    WARNING: Failed to download jakarta-struts-1.0.2.jar.
    Attempting to download ldapjdk-1.0.jar.
    WARNING: Failed to download ldapjdk-1.0.jar.


    Any insights?
  17. Error building the sample code[ Go to top ]

    The author used ver 1.2b4 of xdoclet, yet that is not currently on IBiblio (Mavens download site). As a result it can't find the JAR's to put in the Maven repository on your machine. You can alleviate this by going to the xdoclet site (xdoclet.sf.net) and download it manually (they have a Maven info page over there).

    The other jars you can do something similar with ... grab them manually and put them in your Maven repository.

    If in doubt, go to the Maven users mailing list
  18. The author used ver 1.2b4 of xdoclet, yet that is not currently on IBiblio (Mavens download site). As a result it can't find the JAR's to put in the Maven repository on your machine. You can alleviate this by going to the xdoclet site (xdoclet.sf.net) and download it manually (they have a Maven info page over there).

    >
    > The other jars you can do something similar with ... grab them manually and put them in your Maven repository.
    >

    You are right Andy. I should have perhaps mentioned this in the tutorial.

    The ldapjdk.jar is not necessary. When I created the example by cutting and pasting from another project of mine, it must have been included by oversight. Sorry ;-(

    It should be removed from the project.xml's dependency sections.


    Hope that helps,
    Srikanth
  19. RE: Error building the sample code[ Go to top ]

    Thanks for the insights and perhaps you can provide further insights by providing the information.

    1) Looking at the Maven plug-in link for the the XDoclet, it seems to show 1.2b2

    2) Looking at the available downloads from SourceSourge, only 1.2b3 is available

    Question. Where can I download 1.2b4? and/or what step did I miss?

    I don't mean to be a pain but I am interested in getting xdoclet and maven to work but was having a hard time so I was excited to see a tutorial about it.

    I tried modifying the version number to 1.2b2 (per XDoclet website) and I get the following error

    BUILD FAILED
    File...... file:/C:/Documents and Settings/Administrator/.maven/plugins/maven-xdoclet-plugin-1.2b2/
    Element... deploymentdescriptor
    Line...... 2467
    Column.... 39
    java.lang.NullPointerException
  20. RE: Error building the sample code[ Go to top ]

    1.2b3 contains 1.2b4 jars (for some reason, better ask the XDoclet people :-). Get that and add to your repository
  21. RE: Error building the sample code[ Go to top ]

    Andy/Srikanth,

    Thanks for the insights and perhaps you can provide further insights.

    1) Looking at the Maven plug-in link for the the XDoclet, it seems to show 1.2b2
    2) Looking at the available downloads from SourceSourge, only 1.2b3 is available

    ie neither shows seems to show 1.2b4 so I guess my real question was where can I download 1.2b4? and/or what step did I miss?

    I don't mean to be a pain but I have worked with maven and xdoclet but had trouble integrating the two so I was excited to see a tutorial about it.

    I tried modifying the version number to 1.2b2 (per XDoclet website) and I get the following error

    BUILD FAILED
    File...... file:/C:/Documents and Settings/Administrator/.maven/plugins/maven-xdoclet-plugin-1.2b2/
    Element... deploymentdescriptor
    Line...... 2467
    Column.... 39
    java.lang.NullPointerException
  22. My Fixes When Building[ Go to top ]

    ear\project.xml
    Remove lpdapjdk references (or change to something that exists on server e.g. ldapsdk V4.1)
    Change:
    <ear.appxml.ear.context-root>foobar-web</ear.appxml.ear.context-root>
    To:
    <ear.appxml.war.context-root>foobar-web</ear.appxml.war.context-root>

    Foobar-Web/project.xml
    Again, remove lpdapjdk references (or change to something that exists on server e.g. ldapsdk V4.1)
    'Jakarta-struts' seems to just be struts now...
    Changed:
          <groupId>jakarta-struts</groupId>
          <artifactId>jakarta-struts</artifactId>
          <version>1.0.2</version>
    To:
          <groupId>struts</groupId>
          <artifactId>struts</artifactId>
          <version>1.1</version>

    Override in properties file, not in plugin.
    Foobar-Web/project.properties
    maven.xdoclet.webdoclet.deploymentdescriptor.0=false
    maven.xdoclet.webdoclet.jsptaglib.0.destDir=${maven.build.dir}/${pom.artifactId}/WEB-INF/tld
    maven.xdoclet.webdoclet.jsptaglib.0.filename=taglib.tld

    Download an install maven-xdoclet-plugin-1.2b4 manually (mine would only download maven-xdoclet-plugin-1.2 automatically and it didn't work).
    Edit maven-xdoclet-plugin-1.2b4/plugin.jelly
    Comment out:
            <path id="webdoclet.java.compile.src.set" location="${maven.xdoclet.webdoclet.destDir}"/>
            <maven:addPath id="maven.compile.src.set" refid="webdoclet.java.compile.src.set"/>
    This was causing an attempted java compilation at an invalid source path. May be better way to solve - will email the xdoclet folks.
  23. My Fixes When Building[ Go to top ]

    ...{sniparoo}...
    Override in properties file, not in plugin.
    Foobar-Web/project.properties
    maven.xdoclet.webdoclet.deploymentdescriptor.0=false
    maven.xdoclet.webdoclet.jsptaglib.0.destDir=${maven.build.dir}/${pom.artifactId}/WEB-INF/tld
    maven.xdoclet.webdoclet.jsptaglib.0.filename=taglib.tld

    Download an install maven-xdoclet-plugin-1.2b4 manually (mine would only download maven-xdoclet-plugin-1.2 automatically and it didn't work).
    Edit maven-xdoclet-plugin-1.2b4/plugin.jelly
    Comment out:
            <path id="webdoclet.java.compile.src.set" location="${maven.xdoclet.webdoclet.destDir}"/>
            <maven:addPath id="maven.compile.src.set" refid="webdoclet.java.compile.src.set"/>
    This was causing an attempted java compilation at an invalid source path. May be better way to solve - will email the xdoclet folks.
    Yup..Yup. I was getting the same error. Actually, with Maven 1.0-RC3, xdoclet 1.2b4, I simply omitted the <code>maven.xdoclet.webdoclet.jsptaglib.0.destDir</code> setting and it worked. It DID create a separate directory in the <code>maven.build.dir</code> (aka "/target") called "xdoclet/webdoclet/WEB-INF/tlds/" and put the TLD there. However it makes it in the WAR, so no worries.
  24. Proxy-Settings[ Go to top ]

    When I tried to execute the first example I got a proxy error. Mavens FAQ gave the answer:

    set maven.proxy.host and maven.proxy.port (e.g. in project.properties in the project directory)
  25. Problem running the maven java:compile[ Go to top ]

    I am a first timer for MAVEN. I followed the steps provided by srikant in the article. I created a directory as follows:

    C:/maventest/TestProject
  26. I am sorry. I pressed reply before I can complete the message. please bear with me.

    I am a first timer of MAVEN. I am learning how to user maven for project and deployment management so that I can effectively deploy my projects.

    I followed the steps provided by srikant in his article. I created a new directory as follows:

    C:\MavenTest\TestProject.then these are the steps I followed:

    1. I placed project.xml in the directory mentioned above. the xml file is as follows:

    <?xml version="1.0"?>
    <project>
    <pomVersion>3</pomVersion>
    <groupId>TestProject</groupId>
    <id>sample-project</id>
    <currentVersion>1.0</currentVersion>
    <name>TestProject</name>
    <!-- Project Management section goes here -->

    <organization>
    <name>MyOrganization</name>
    <url/>
    <logo/>
    </organization>
    <inceptionYear/>
    <package>test.*</package>
    <logo/>
    <description>This is just a test description</description>
    <shortDescription>this is short description</shortDescription>
    <url/>
    <issueTrackingUrl/>
    <siteAddress/>
    <siteDirectory>C:\MavenTest\TestProject</siteDirectory>
    <distributionDirectory>C:\MavenTest\TestProject</distributionDirectory>
    <repository>
    <connection></connection>
    <url></url>
    </repository>
    <mailingLists>
    <mailingList>
    <name>Dev List</name>
    <subscribe/>
    <unsubscribe/>
    </mailingList>
    </mailingLists>
    <developers>
    <developer>
    <name>sreram kumar</name>
    <id>sreram</id>
    <email>myname.mydomain.com</email>
    </developer>
    </developers>

    <!-- Project Dependency section goes here -->
    <dependencies/>
    <!-- Project Build section goes here -->

    <build>
    <nagEmailAddress>myname at mydomain dot com</nagEmailAddress>
    <sourceDirectory>C:/MavenTest/TestProject</sourceDirectory>
    <!--<unitTestSourceDirectory>${basedir}/test/java</unitTestSourceDirectory>
    <unitTest>
    <includes>
    <include>**/*Test.java</include>
    </includes>
    </unitTest>-->
    <resources/>
    <!--<resource>
    <directory>${basedir}/src/conf</directory>
    <includes>
    <include>*.properties</include>
    </includes>
    </resource>
    </resources>-->
    </build>


    <!-- Project Reports section goes here -->
    <reports>
    <report>maven-changes-plugin</report>
    <report>maven-jdepend-plugin</report>
    <report>maven-checkstyle-plugin</report>
    <report>maven-pmd-plugin</report>
    <report>maven-junit-report-plugin</report>
    <report>maven-clover-plugin</report>
    <report>maven-changelog-plugin</report>
    <report>maven-file-activity-plugin</report>
    <report>maven-developer-activity-plugin</report>
    <report>maven-file-activity-plugin</report>
    <report>maven-license-plugin</report>
    <report>maven-linkcheck-plugin</report>
    <report>maven-jxr-plugin</report>
    </reports>
    </project>

    2. I created a folder test.
    3. I created a java file with package test.
    4. I copied the java source file to test directory within the directory mentioned above.
    5. I entered the directory cd C:\MavenTest\TestProject
    6. I entered the following command
       C:\MavenTest\TestProject>maven java:compile

    I got the following error in the dos prompt. could srikant or somebody proficient in using MAVEN help me out.

     __ __
    | \/ |__ _Apache__ ___
    | |\/| / _` \ V / -_) ' \ ~ intelligent projects ~
    |_| |_\__,_|\_/\___|_||_| v. 1.0-rc1-SNAPSHOT

    Attempting to download commons-jelly-tags-antlr-20030211.143720.jar.
    Error retrieving artifact from [http://www.ibiblio.org/maven/commons-jelly/jars/commons-jelly-tags-antlr-20030211.143720.jar]: java.net.ConnectExcepti
    on: Connection refused: connect
    WARNING: Failed to download commons-jelly-tags-antlr-20030211.143720.jar.
    Attempting to download commons-lang-1.0.1.jar.
    Error retrieving artifact from [http://www.ibiblio.org/maven/commons-lang/jars/commons-lang-1.0.1.jar]: java.net.ConnectException: Connection refused:
     connect
    WARNING: Failed to download commons-lang-1.0.1.jar.
    Attempting to download antlr-2.7.2.jar.
    Error retrieving artifact from [http://www.ibiblio.org/maven/antlr/jars/antlr-2.7.2.jar]: java.net.ConnectException: Connection refused: connect
    WARNING: Failed to download antlr-2.7.2.jar.
    The build cannot continue because of the following unsatisfied dependencies:

    commons-jelly-tags-antlr-20030211.143720.jar (no download url specified)
    commons-lang-1.0.1.jar (no download url specified)
    antlr-2.7.2.jar (no download url specified)

    Total time: 7 seconds
    Finished at: Fri Dec 05 10:20:47 GMT+05:30 2003
  27. The problem that people seem to face is that Maven attempts to automatically connect to iBiblio to download certain artifacts whereas ideally I would like it to use a central server that is behind the coroporate firewall. How do we make Maven do that?
  28. thats easy too[ Go to top ]

    Set the property

    maven.repo.remote

    to point to an alternative repository.

    You can also set maven.repo.local to point to a local machine

    Please ask your questions on the maven users mailing list and you will get a more detailed reply
  29. thats easy too - but how?[ Go to top ]

    Set the property

    >
    > maven.repo.remote
    >
    > to point to an alternative repository.
    >
    > You can also set maven.repo.local to point to a local machine
    >
    > Please ask your questions on the maven users mailing list and you will get a more detailed reply

    I tried setting the maven.repo.remote to point to my local ip address and same for maven.repo.local i.e.

    maven.repo.local=<MyIPAddress>
    maven.repo.remote=<MyIPAddress>

    still it seems to connect to the http://www.ibiblio.org/maven/commons-jelly/jars. could you please tell me how to change this. probably give me some steps to change them.
  30. using a proxy[ Go to top ]

    If you are behind a proxy you can add the following properties to your build.properties:

    maven.proxy.host = PROXY-HOST
    maven.proxy.port = PROXY-PORT
    maven.proxy.username = PROXY-USER
    maven.proxy.password = PROXY-PASSWORD

    That's it.

    (When you have a pretty complete local repository, you can setup a local repository pretty easily with a simple website (like ibibilio.org/maven) and use a property:
    maven.repo.remote = http://local-server/maven

    Rgrds,
    Klaas
  31. hi[ Go to top ]

    hi,

    i think u have to check the network connections in the build.xml file of each to get it downloaded from the internet....
  32. JEE maven skeleton project[ Go to top ]

    bit late, but see my JEE maven reference project deployable on glassfish http://www.wowww.nl/wordpress/2009/11/04/jee-5-getting-started-tutorial/
  33. i got the same problem too and none of the solutions suggested in this page helped. Here is what I did.
    Added a file build.properties at the same level as project.xml.
    with the following contents
    maven.repo.remote.enabled=true
    maven.mode.online=true
    maven.repo.remote=file:///D:/Dhanya/test/repository
    I copied all the jars required to repository each in its own folder.
    ie.
    test\repository\ant\jars
    test\repository\antlr\jars
    test\repository\commons-jelly\jars
    test\repository\commons-lang\jars
    test\repository\commons-net\jars
    The compilation worked
  34. I'm new to maven and try to learn from the sameple. Yet when building sample proeject, it states error as follow. What may cause such problem? I've marked ldapjdk and put j2ee-1.3.1.jar and jakarta-struts-1.0.2.jar in appropreate folder in ~/.maven/ dir.
    I appreciate any suggestions, sincerely.
    ========== ERROR ==========BEG
    BUILD FAILED
    File...... file:/C:/unzipped/Foobar-Travels/maven.xml
    Element... maven:reactor
    Line...... 12
    Column.... 49
    Unable to obtain goal [foobar-dist] -- file:/C:/unzipped/Foobar-Travels/Foobar-W
    eb/maven.xml:9:45: <attainGoal> No goal [xdoclet:webdoclet]
    Total time: 6 seconds
    Finished at: Fri May 28 16:07:17 GMT+08:00 2004
    ========== ERROR ==========END
  35. Error No goal [xdoclet:XXXXX][ Go to top ]

    Hello, first of all, I want to apologize for my english.

    Well, like others new to maven, I've got the following problem executing maven: "<attainGoal> No goal [xdoclet:webdoclet]" or "<attainGoal> No goal [xdoclet:ejbdoclet]"

    When you install maven, you install a lot of plugins and dependencies but you don't install the plugin that supports xdoclet goal.

    There are 2 principals sites where you can find a plugin http://maven.apache.org/reference/plugins/index.html and http://www.ibiblio.org/maven

    After you have found the plugin, you have to install it. How? it's very easy, in this case, we need maven-xdoclet-plugin (artifactId) which belongs to xdoclet group (groupid)

    maven -DartifactId=maven-xdoclet-plugin -DgroupId=xdoclet -Dversion=1.2 plugin:download
  36. Practice Area Lead - Continuous Build Integration Tools

    We are currently recruiting for a Practice Area Lead who has experience in open-source Continuous Build Integration Tools, such as Maven, Hudson, Nexus, etc. One of the upcoming projects this resource will support, is a Maven and Nexus implementation/conversion we are performing for a client in NJ. This project could be done on a consulting basis,  or as a FTE opportunity to join our practice. Please let me know if you are interested in a career opportunity with CMI (or converting to Maven/Hudson in a consulting capacity at our client). There is a lot more information available at our website http://www.cmi.com.

    Please contact:
    Oliver Eaton
    Recruiting Manager CMI
    732-450-1100 ext. 114
    oliver dot eaton at cmi dot com