What do I need to configure in an Eclipse project to fix incorrect “The import XXX cannot be resolved”
cxf, eclipse, java, m2e
Solution
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`
Problem
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): - `target/generated/src/main/java` doesn't exist, but checking the "Create new folder" box, I was able to create it. - However, upon pom.xml > Run As > `Maven clean`, it gets deleted (so needs to be re-created after every clean???) - 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??? - 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). - 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?