Getting Started With Maven

I'm trying to better understand using Maven 2.0.x in IDEA. I know Java well, but get to use IDEA less than Eclipse, and am totally new to Maven 2.0.x.

Here are my questions:

1) How should a project using JSF/Facelets, Hibernate, Spring, and EJB3 with Maven2 be structured? Single module with single POM that produces an EAR? Or, multiple modules with multiple POMs perhaps having a common parent POM for the project?

2) I recall reading that we can let POM generate the module file. Should I let the POM drive building the module files or manually sync the modules files with POM dependency changes?

3) Does it matter if I use the Maven build or test goals, or the IDEA Build | Make Project? That is, will I end up with the same result - an EAR, WAR, or JAR file?

4) Is there any other advice or good online IDEA + Maven resource you can refer me to?

(Trying to understand how to use Maven with IDEA by referring to IDEA's Online Help is like trying to assemble a novel using only a dictionary. The pieces may all be there, but the total picture truly eludes me!)


Comment actions Permalink

Hi Jack,

Generally, I would recommend splitting your large project into multiple POMs. You do that for the same reasons you want to achieve clean modularity in any project in general. If you have designed your architecture thoughtfully, you probably have your domain model, middle-tier services, presentation tier, etc. implemented independently. You want to be able to build and package any of these components separately, and then simply use them as dependencies in your application(s). For example, you may have a web application (Web module) that depends on your generic Domain component and some of the services. You may have another application with a different UI implementation that could use (re-use) that domain and one or more services. So, for a typical architecture, I would suggest the following approach:

1. Based on the business use cases, carefully design the Domain model. Note that your domain classes should have absolutely no dependencies on any service-specific or app-specific classes. (The only dependencies Domain might still have are some common classes and utilities, e.g. apache commons, log4j, jUnit, your own generic stuff, etc.) Create a separate POM for your domain model. This way, you enable yourself to build and distribute the domain with different services and applications, if necessary. Your domain classes may now be included into any IntelliJ project as a module.

2. Design the services that are not application specific (if you have such services) that use the domain objects. Create a POM for each service. The dependencies for each service may include your Domain. The classes and resources for each service may now be used as a separate IntelliJ module in a project.

3. You may create a top-level POM for all your independent services, if there is a case when you want to include them all in one project and build/package them all together.

4. Place all app-specific classes (domain, services, dao implementations, presentation-specific, etc.) under the Java source tree within the root for this application. Create an application-specific POM (e.g. web app - based on Maven's weapp archetype) that will use your generic services as its dependencies. Just include the Maven definitions for all your other previously defined Maven modules into your application's POM, just like you include the dependencies on any 3rd party libraries. If, say, your the services depend on the Domain module, the Domain POM will be used automatically to build the Domain module when the top-level build is inviked, or to include that module in the project when the POM is used by IntelliJ to construct an IDEA project. No need to include the Domain dependency explicitly into the top-level app POM then. For this particular application, that top-level POM is what you need to build the app and configure your IDEA project with all its modules. Each dependency POM will correspond to a separate module in your multi-module application project.

[Read the Maven tutorial on how to write and organize POMs. Once you have your POM files ready, I recommend to run the "mvn install" command from the command line to make sure that everything works fine and your project builds. Once you have that tuned, you are ready to use those poms for your development/Idea setup. Run the "mvn install" command for each new POM separately to test and debug your POM. Then you can run it on any top-level app-specific POM.]

5. To create a project in IDEA, I would recommend to use the option to create a project from an external model (Maven) that ALWAYS loads your configuration from the POMs on Idea's start-up. This ensures that your project configuration is completely IDE-agnostic and can be reused by the team members who use Eclipse, for example. Also, you can (should) store the POMs in a VCS, and automatically get any configuration updates in your IDE by checking out the latest POMs. When you ask Idea to use Maven for your new project, it will search for all POM files under the specified Root. You will have a chance to select/deselect the POMs that you want included in your project. After you make your selection and proceed, IntelliJ will create +temporary +project files on the fly based exactly on the configuration provided in the POM files. Note that if you have more than one POM, a multi-module project will be created where each module corresponds to a single POM configuration. All dependencies will be automatically resolved according to the dependencies between your POMs, and the necessary libraries from the Maven repository will be added to the module's dependencies. If you update a POM file during your IDEA session, you will have to synch the POMs with the tmp IDEA configuration by clicking the synch button in the top-bar menu of the Maven pane on the right side of your screen.

The result of your build depends on the purpose of each POM/sub-project (how your POM's packaging property is set). Your domain and services should normally be all packaged in +individual +jars. Your complete web app may be packaged as a WAR file that contains individual jars created by the lower-level POMs. Or, it may be an EAR file, whatever you need. The bottom line: use multiple POMs, one for each independent component of your architecture. (Not sure why you are using both Spring AND EJBs, in most cases Spring will do the job. Perhaps you have your reasons...)

NOTE: If you don't need the sources for a particular module included in your project and just use the module's JAR as one of the project's dependencies, do not select that module's POM when creating your IntelliJ project. Since your top-level POM (directly or indirectly) depends on that library, the library will be pulled into your project (as a JAR from the Maven repository) - assuming that you have already built that module previously.

I must note that I have found a few kinks in the way Idea synchronizes POMs with its settings. For example, sometimes I have to manually synch Maven POMs with Idea to have some modules "noticed" at runtime. Not sure why. Also, (this is not Maven related, but rather project/Spring/Webflow support) Idea recognizes my Spring context files fine, but fails to recognize the web-flow configuration files. So, I have to manually edit the web module's Spring facet's file set to add the web flow files - each time I restart Idea - for the web flow beans to be recognized as part of the context. I don't have to do it to build and run the app because those things will get resolved at the time of the build, but it is annoying to have the red stuff in my config files during development, so I always make sure that my Spring file sets are updated. I hope Jetbrains fixes this soon.

Sorry if the answer is somewhat chaotic, it's hard to answer your question in just a few sentences. You will need to carefully read the Maven tutorial to get a really good idea on how beneficial it might be to your project. I would certainly recommend using Maven. it is a great tool that helps to simplify and standardize project configuration and builds. After you have your POMs set up, it will be just a matter of minutes for any developer with IDEA to set up the project, no matter how complex it is. (It may take longer for Eclipse users, not sure... ;) ) Just make sure your POMs don't get out of hand. It helps to keep them clean and well organized. Don't unnecessarily repeat dependencies in nested POMs, etc.

The main benefits of using Maven:

- Simpler and shorter than using pure Ant for your builds.
- Same configuration files used to configure the project and to build the project in all environments (you can easily configure Maven to enable/disable certain things depending on the build scope/environment), to run tests, create reports, etc.
- Standardized IDE-independent project setup,
- etc.

And finally: Use Maven goals inside IntelliJ (inside the Maven pane on the right side of the editor) to build your project (with or without tests, I recommend always having tests enabled.) This will ensure that your IntelliJ builds will be always identical to your production builds. All ear/war and class files will be generated in the ]]>/target directory. Then, you can easily configure IntelliJ to deploy them in your app server the same way you would do it w/o Maven.


Message was edited by:

Message was edited by:



Comment actions Permalink

The current maven archetype plugin has the goal of archetype:generate

This presents a list of project configuration choices, with a list of the technology used in each

Maven then creates the directory skeleton and the pom file. I have been opening the pom with Idea, instead of using the maven idea plugin.


Please sign in to leave a comment.