Maven Confusion

Hello,

If I create a maven project in IDEA 12.0.4, and select "org.apache.maven.archetypes:maven-archetype-webapp", the generated project is confusing.

Specifically, in the generated folder structure, I cannot create a package in which to put my Java classes, regardless of how many "folders" I create beneath "src".  Right-clicking the orange folders in the project doesn't give me a create package option. All I can do is create another folder...

I think the folders need to be blue before you can create a package (get the package option in the right-click menu, for example).

I've studied the 'Stamdard Directory Maven Layout", but it's not revealing in helping me figure out why the project in Intelij is being created like it is.

I've tried various archteypes, but being able to create a package is alluding me.

This is really confusing. Maybe I'll start to like Maven once I understand it, but the 5 minute demo hasn't convinced me and I already spent a week trying to figure out a JAR conflict issue.

Look forward to any comments as to how you can create pacakges and use Maven effectively.

I'm also assuming I can add the Tomcat task and it will just "work".

Thanks in advance for any replies.

-m

4 comments

The image below shows the directory structure to use if you want a single module webapp project. (The other option is to put the Java classes into a second module that the webapp then has a dependency on.) After you add the src/main/java and resources directories, you will need to re-import the maven project (think of it as synchronizing the idea project and the maven project) so that IDEA 'sees' them as a source directories (and turns them blue). Do that in the the maven tool window by clicking the reimport button reimport.png


screenshot.png

0

Hi Mark,

Thanks for your reply.

What archetype did you use? I was thinking I could create a mvn project and have the folder setup as expected (java folder "blue" already, etc.).

In my case, I was creating a brand new Maven project using the apache webapp archetype so there's nothing to "re-import", unless I'm misunderstanding what you mean.

Also, I don't know how to get the blue src folder like you got.

Still missing something pretty basic I guess.

Would appreciate a follow up reply.

Thanks,

- m

0

We have internal archetypes we use. Using the webapp (org.apache.maven.archetypes:maven-archetype-webapp) archetype is fine. It simply will not create the directories

  • src/main/java
  • src/main/resources
  • src/test/java
  • src/test/resources


You will need to manually create them afterward and then reimport the project. Understanding how IDEA works with maven and some of the related terminology may help.

When working in IDEA, there needs to be an IDEA project configured that sets the classpath, SDK, source directories (i.e. blue), test directories (green) dependencies, etc. etc. One way to do that is to go in and manually configure all that. Another is to use maven. When you work with a maven project within IDEA, IDEA is not "directly" using the maven project. It looks at the maven POM files, analyzes them, and then creates an IDEA project that matches the maven project and its configuration (setting up the source directories, dependencies, webapp facets, etc.). This process, in the IDEA terminology, is called "importing the maven project."  If you make any changes either to the POM (or make structural changes to the project) you need to "reimport" the project so that IDEA can discover any new settings and add them (such as new dependencies) and remove any old settings (such as removed dependencies). You can think of reimporting as re-synchronizing the IDEA project configuration to the maven POM(s). You can set up IDEA to automatically reimport the maven project whenever the POM changes. (File > Settings > [Project Settings] > Maven > Importing > "Import Maven projects automatically") That will handle most changes to the maven POM and works in most cases. (Some people prefer to manually reimport the project whenever there is a change. IDEA shows a pop-up when ever it detects a reimport is necessary).

In a standard Java maven project, when IDEA imports the maven project, it sets src/main/java & src/main/resources as source directories and src/test/java & src/test/resources as test source directories. What is happening in the case of the webapp is that those directories are not initially present (by default). So when IDEA does its import, it does not set those directories up as sources and test sources. After you manually add them, IDEA will not see them as sources until it is reconfigured. IDEA needs to be told to reconfigure itself. By running a reimport (as described in my original post), IDEA will go through the maven POM(s) again and configure the IDEA project. When doing the reimport, it will now "see" the above directories are present and configure them as source and test source directories. Even if you have "automatically reimport" option set, you will need to manually run the re-import since adding the new directories is not a change that will fire off the automatic reimport.

0

Terrific reply. :)

Thank you very much! It now makes sense (and everything you suggested works fine).

I truly appreciate your reply, Mark.

Thanks again!!!

- m

0

Please sign in to leave a comment.