Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do I need to configure in an Eclipse project to fix incorrect “The import XXX cannot be resolved”

I am following this Contract first using CXF tutorial.

All was going well, including generating sources from the WSDL, using build-helper-maven-plugin but when time came to "implement the service", i.e. move the generated RegistryResourceImpl.java to the normal source folder src/main/java, Eclipse started complaining about some symbols (like the interface RegistryResource) not being recognized.

Realizing that the normal src/main/java folder is assigned a different package name, I simply added in the RegistryResourceImpl.java the import for the package where RegistryResource.java belongs:

import com.yourcompany.sample.ns.RegistryResource;

But Eclipse in its current project configuration appears to be blind to com.yourcompany.sample.ns, not knowing where to find it.

I tried finding an option in the Build Path to point to the generated sources but none of what I tried helped.

I have a feeling I am missing something very trivial but what is it?

Also, shouldn't m2e (Maven plugin for Eclipse) already handle that kind of moving implementation files automatically?

Update (after trying @Patrick's suggested solution):

  1. target/generated/src/main/java doesn't exist, but checking the "Create new folder" box, I was able to create it.
  2. However, upon pom.xml > Run As > Maven clean, it gets deleted (so needs to be re-created after every clean???)
  3. If not re-creating after clean, doing pom.xml > Run As > Maven install, creates the generates source under a totally different tree: generated-sources/cxf/com/yourcompany/sample/ns. Why???
  4. Yet, the RegistryResourceImpl.java module is still automatically created under target/generated sources. At this point, I no longer see complaints about "unresolved package/imports/symbols" (weird).
  5. But... as soon as it is being moved to non-target folder/package, i.e. com.yourcompany.sample.ns under src/main/java, "import com.yourcompany.sample.ns.RegistryType;" cannot be resolved again!

On the other hand, if I try to add as a "source folder" the actual generated path generated-sources/cxf/com/yourcompany/sample/ns (instead of the irrelevant/non-existent target/generated/src/main/java, the generated tree under target is moved (deleted) to under /src/main/java. I don't understand this behavior of Eclipse.

Lastly, even when I do the Maven > Update Project suggested here, thereby getting rid the "Maven 2 project error icon", the RegistryResourceImpl.java module still shows unresolved symbols, although the entire project build perfectly fine!

What could explain this?

Is it possible that the build-helper-maven-plugin is incomplete or even broken?

Is there a way to make CXF + build-helper-maven-plugin work harmoniously with Eclipse so that I don't see those disturbing red underlines of "unresolved symbols" even when the project builds successfully?

like image 478
Eternal Learner Avatar asked Aug 02 '13 22:08

Eternal Learner


2 Answers

You need to add a source folder in Eclipse for your generated sources. The tutorial mentions this in its comment "Note: M2Eclipse doesnt seem to pick up on this source folder so you will have to add it manually in eclipse." If you followed the directions at the tutorial, the location you need to add should be (relative to your project directory)

target/generated/src/main/java

To add the folder, open your project properties and select "Java Build Path" from the left hand menu. From the "Source" tab, press the "Add Folder..." button, and select "target/generated/src/main/java" as the source folder.

Please not that Maven will remove the generated folders when running a mvn clean install. You may want to configure Eclipse to refresh resources when you run this command, in order to avoid inconsistencies. The Eclipse run configuration includes a Refresh tab that allows you to enable "refresh resources upon completion."

Another option you might consider is skipping the use of Maven for your wsimport needs. I would call wsimport directly, put the generated artifacts under your src/main/java folder, and be done with it. Unless you really need to re-generate the artifacts with each build, I think the Maven plugin is more trouble than its worth.

like image 139
Patrick Avatar answered Sep 23 '22 00:09

Patrick


If not re-creating after clean, doing pom.xml > Run As > Maven install, creates the generates source under a totally different tree: generated-sources/cxf/com/yourcompany/sample/ns. Why???

You need to configure the cxf-codegen-plugin to output the generated code to the desired folder.

...
<configuration>
    <sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot>
    ...  
</configuration>  

Then either install a connector for cxf-codegen-plugin (that is, if one is available) or use maven build-helper-maven-plugin with its connector. Make sure that sources point to the same place as sourceRoot.

<configuration>
    <sources>
        <source>${basedir}/target/generated/src/main/java</source>
    </sources>
</configuration>

ps: To install / discover connectors go to Preferences -> Maven -> Discover

like image 32
Anthony Accioly Avatar answered Sep 20 '22 00:09

Anthony Accioly