3

I've just started using maven for trying to build a multi module project.

Everything would be fine other than 2 small problems.

Using the eclipse wizard to create a new maven project of 'simple J2EE' archetype fails due to a bug in eclipse whereby when looking for archetypes it ignores the proxy setting (https://bugs.eclipse.org/bugs/show_bug.cgi?id=348893)

solution is to create my project on the command line and then copy the resulting file structure into a 'simple maven' project inside my workspace - the import project fails also!

My real issue however is the following

When I click on the project folder and 'create a new java class' it puts the class in the wrong place, according to where it should be in the maven architecture (am I selecting the wrong option?). So I move it.

so now I create a second class, that has the first class as a member. Both classes are obviously inside the same package namespace. But I can't see any of the methods of the member class from my second.

I'm guessing I'm doing something wrong in my POM or in my structure.

But I don't know what and don't know what info you may need to help me resolve it.

For now I have reverted to creating my sub module as a separate package in eclipse, using ANT and the various eclipse tools to point to the required libraries etc. then when I've done testing I convert the project into a 'maven' project. My parent POM and child POM all validate OK from within eclipse after I have done this. But again if I decide to extend the functionality and add in a new class to my module the class file ends up in the wrong place and I can't see my other classes from it (or this new class from them).

Please help. I like the idea of Maven and using it to control my dependencies, by the way things are going I'm going to head over to ANT, and maybe learn IVY for dependency control (although I'm a bit muffed to have gone to all the struggle of getting maven to function only to throw it out!)

Should I stick to ANT / IVY and forget maven, but keep up the maven directory structure for the future?

Edit.

So I may have missed something... I just tried again creating a simple maven project in eclipse, this time I didn't select an archetype. I has put my new 'module' underneath the parent folder. This isn't what I have been doing, I've created a numer of external modules at the same level as my parent. I notice from the effective POM in the parent (with my test module) it has a section and and other tags. Will I solve my problem by adding this into my individual module POM's ?

DaveM
  • 734
  • 4
  • 11
  • 35

3 Answers3

19

This is the result of Eclipse/Maven weirdness and seems to occur randomly. To solve:

Right click on the project and select Maven > Update Project...

Chris Passante
  • 191
  • 1
  • 2
  • Chris, thanks for the info. Truth is I lost so much hair playing with maven I've returned to a simpler ANT setup (although the hair loss may be related to other 'living' things ie small children :). I'll try again one day, when I decide to conver my current selection of 'library' projects into a single parent one. For now though I don't have time to mess around fighting the thing every time I open my workspace. – DaveM Jan 17 '13 at 10:16
  • Thank you for this. It fixed my problem immediately. – Graham Jul 09 '13 at 16:16
  • Worked perfectly. Software is weird. – TheGuyWithTheFace Jun 09 '15 at 20:27
  • Helped, me to. The same thing suddenly happened in a Maven project of mine after I fiddled with the .pom a bit. It also started to throw ClassNotFoundException out of the blue. If you found yourself in a similar situation, try this out first before chewing through classpaths and whatnot. – Sargon1 Jul 16 '17 at 20:35
  • also you can Run as - > Maven clean / install –  Dec 20 '17 at 11:28
4

Look at your .classpath. Verify that the source folders and targets are where eclipse expects them.

An example:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java"/>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>
ptyx
  • 4,074
  • 1
  • 19
  • 21
  • Should this info be in my POM (Parent or sub module?), or should it be a setting in Eclipse? If I can put it in my Parent POM, is there a way to set it so as it is the same for all projects, without the need to 'copy and paste it' for each one? Unfortunately the documentation on Maven often seems a bit lacking, in favour of the 'convention over configuration' approach, that said, should I even need to state a classpath in my maven projects, it should just be there, no? – DaveM May 21 '12 at 10:13
  • There are two approaches with eclipse: just rely on the pom, and generate .classpath and .project out of it. Or source control the 3 files. I like the second approach better as I'm tired of running into issues where some of the info required in the eclipse files aren't available in the pom. With that in mind, the code I put above should be the content of your project/.classpath file. – ptyx May 21 '12 at 17:17
  • OK i've found the .classpath file (only visible from the 'navigator' view). But should I edit this directly or should I set it from the project properties dialogue? Also you suggest to source control the 3 files (I guess you mean the parent POM, the specifc project POM and the project .classpath file?). The next question is why does this not work automagically (like almost everything else in eclipse) or why is a maven project, created from the command line, not containing the required lines? Or am I missing a maven step? – DaveM May 22 '12 at 07:46
  • I am suggesting to source control the 3 files. I have no idea how you arrived where you are, there are dozens of different way to create eclipse/maven projects and only a few of them work well for both. – ptyx May 22 '12 at 18:00
  • So I've edited the files (directly in one case and using the properties dialog in eclipse) and things seem to be working for both methods. so cool. Answer accepted – DaveM May 23 '12 at 13:33
0

Maybe you have some like this configuration in your .project file:

<buildSpec>
        <buildCommand>
            <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
            <triggers>full,incremental,</triggers>
            <arguments>
                <dictionary>
                    <key>LaunchConfigHandle</key>
                    <value>&lt;project&gt;/.externalToolBuilders/org.eclipse.wst.common.project.facet.core.builder.launch</value>
                </dictionary>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
            <triggers>full,incremental,</triggers>
            <arguments>
                <dictionary>
                    <key>LaunchConfigHandle</key>
                    <value>&lt;project&gt;/.externalToolBuilders/org.eclipse.jdt.core.javabuilder.launch</value>
                </dictionary>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
            <triggers>full,incremental,</triggers>
            <arguments>
                <dictionary>
                    <key>LaunchConfigHandle</key>
                    <value>&lt;project&gt;/.externalToolBuilders/org.springframework.ide.eclipse.core.springbuilder.launch</value>
                </dictionary>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
            <triggers>full,incremental,</triggers>
            <arguments>
                <dictionary>
                    <key>LaunchConfigHandle</key>
                    <value>&lt;project&gt;/.externalToolBuilders/org.eclipse.wst.validation.validationbuilder.launch</value>
                </dictionary>
            </arguments>
        </buildCommand>

Change it for this config (or other of one of your projects):

<buildCommand>
        <name>org.eclipse.wst.common.project.facet.core.builder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.springframework.ide.eclipse.core.springbuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
fedorqui
  • 275,237
  • 103
  • 548
  • 598