简体   繁体   中英

Maven java project in eclipse doesn't see classes in same package

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 ?

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...

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM