Eclipse Maven Workspace Resolution not seeing Generated Classes - java

I have two maven projects in eclipse, a jar and a war. The war has a dependency on the jar, which is resolved through workspace resolution.
The problem is that the jar has generated classes, which are added to the jar through build-helper-maven-plugin. But these classes aren't being properly resolved in the war project. For example: It auto-completes the class but keeps saying it can't be found. More importantly, when running glassfish through eclipse, I get a class not found for these classes.
If I disable workspace resolution everything works fine, but I hope to use workspace resolution. Any ideas?
EDIT:
Folder structure. The maven workspace resolved persistence project in the lower image is in the Maven Dependencies folder, essentially your seeing the top and bottom of the folder.

IDK if I am correct, but you are talking about Eclipse problems - it does not "see" generated classes right?
To fix it, you have to add generated sources directory to the eclipse's build path and it should fix your problem.
Right click on project that has generated classes->buildPath->conf
buildpath
In source tab - click add folder
Select the directory where build helper generates java files.
Generated classes will apear as additional source folder in Eclipse's project hierarchy and voila, Eclipse can autocomplete and resolve generated classes now on the same conditionstha any it would on any other class written by you in the same project by hand.

Related

Why sometimes eclipse doesn't need .project and .classpath file during importing into workspace/How does eclipse recognize projects as projects?

I just checked out some projects from svn to my pc, and I tried to import the projects into my workspace.
But I failed to import some of them, and eclipse didn't recognize those projects(didn't show up in the import window).
And then I turn to google and found a solution -"add .projects and .classpath file to project folder" works.
However, what make me really confusing is that I foundthe projects I successfully imported into workspace without manually adding the files for them also don't contain .projects and .classpath. Eclipse just create the files for them automatically after import, so it seems that the fail of importing projects(or recognizing projects during import) is not just simply because of the lack of .projects and .classpath.
Does anyone know what cause the difference?
How does eclipse exactly recognize a project as importable project?
*eclipse version info here
Eclipse or any other IDE doesn't required .classpath or .project files. These files will be auto created during the project import. IDE can capable to pull the artifacts from maven central repo automatically by using build descriptor file such as pom.xml or build.graddle.
The files .classpath and .project will store the paths from local machine, if we carry them to another system, the artificats and other dependencies may not be present in the same directory, so it will start complaining build errors. Thats why while committing to GIT always there is .gitignore, so that local configuration will not carry to others while contributing.

"Could not find or load main class" after removing Maven nature and files (pom.xml, target/) from an Eclipse Project

I accidentally converted my project to Maven by going to Configure > Convert to Maven Project. Now I want to undo this. I read that I need to right click Maven > Disable Maven Nature and that worked fine. However I want to totally remove Maven, so I deleted the pom.xml and the target folder. When I try to run my code now, I get the error:
Error: Could not find or load main class
So what am I missing? How do I revert from a Maven project to a non-Maven project?
When you convert a Java project to a Maven project in Eclipse, the Maven Integration for Eclipse (m2eclipse) configures the Java incremental compiler to put the compiled class files in the same location as Maven would put them, i.e. target/classes.
So when you remove the Maven nature and delete the target folder, you now also have deleted the compiled class files and your project can no longer run. AFAIK, the incremental compiler doesn't detect when you remove its output files, so you need to trigger a rebuild by cleaning the project (Project > Clean...)
This will fix the problem that you can not launch your project, but may re-create a target folder. If you also want this to be "fixed", you can switch back to some other folder name for the binaries, e.g. bin, in the project's Java Build Path configuration on the Source tab.
Is it basically a Maven project, i.e., do you have and maintain it through a pom.xml? Then my suggestion is to delete the project in Eclipse but keep the files on the disk (i.e., it removes it from the workspace). Then, run a simple mvn eclipse:clean eclipse:eclipse which creates a simple Java project without the Maven nature based on the POM (so the libraries are linked and the source/output directories are set up correctly - this may solve your ClassNotFoundError).
If it's a simple Java project, I would advise deleting it from the workspace, removing the .classpath and .project files and importing it again with the Create a Java project with existing sources wizard.
Either way, make a backup of your project before you start doing anything :-)

Eclipse plugin cannot find class from class folder

I'm developing an Eclipse plugin, and want to use some pre-compiled classes which I've put to lib/ folder in my project (so the directory structure looks like {proj_root}/lib/com/example/Required.class). Then I've added lib/ folder as class folder to project's build path. At build time all is working fine, I can import and use that classes in my code. And now the problem is, when I'm running my plugin in separate Eclipse instance I get ClassNotFoundException by my plugin refering to class from precompiled classes folder.I tried specifying my class folder in "Order and export" tab in Build path settings. Also I tried setting checkbox in plugin's build.properties GUI editor in front of lib/ folder to be included in binary build. In that case if I export my plugin to JAR I can see lib/ folder inside it, with my classfiles, but at runtime my plugin still cannot find any of classes from lib/.Can you please give me an advice on how I should tell Eclipse where to find that classes at runtime?
PS.:
I did research on stackoverflow on this problem, but all suggestions are the same: just add class folder in project's Build path.
At this point in plugin's build.properties Text editor, eclipse shows warning The class folder 'lib/' is not associated to any output library entry. and suggests to make this modification of code:
output.. = bin/,lib/. I have tried that solution but it is seems to change nothing with the problem.
It sounds very much like you have added the lib folder to the "Java Build Path" properties page of the plug-in project.
But... for plug-in projects, this does not work as these are OSGi bundles - you must include the lib folder in the PDE Editor on the "Runtime" page:

Eclipse - Create Dist directory

Recently I was forced into using Eclipse because of TFS plugins:
I have a few projects that I'm converting to Eclipse projects from Netbeans. Some of these projects reference each other. After starting to convert these projects I quickly found that Eclipse doesn't want to jar projects post-build. So I used an build.xml and created a new 'builder' for each project (whose bright idea was it to not allow me to reuse builders across multiple projects?). After I got all that working I was sitting back thinking about how I would go about building for deployments, and it occured to me that eclipse is not including any of the referenced assemblies in the build output directory. This sucks, because manually creating lib folders and copying over all of the jar files which are required will be error prone, and time consuming. So heres the question. Is there any reasonable way to set up a builder, or property on an eclipse project such that when I build it, it will create a 'dist' directory, containing both the jar'ed project classes, and a lib folder with all of the referenced jars attached to the project?
Is there any reasonable way to set up a builder, or property on an eclipse project such that when I build it, it will create a 'dist' directory, containing both the jar'ed project classes, and a lib folder with all of the referenced jars attached to the project?
Yes, right-click on the Project and select Export. Type "jar" into the search box and select Runnable JAR file. In the export dialog, select the "Copy required libraries..." option. There should also be an option there to save this export as an Ant script.
I dont know how to resolve a list of dependencies using ANT
Next, you should consider using Ivy. This will add dependency management to your build script. There's probably some learning curve here, but these tutorials should help.

Change output folder for Android R.java in Eclipse when using Maven

I have an android project setup using the maven-android plugin. This plugin adheres to maven output folders for the location of the R.java file (i.e. target/generated-sources/r).
However, when using this project in Eclipse, I can't configure aapt to rely on that folder.
Even if I manually go into the project properties and set up the folders manually, the ADT plugin goes in and resets just some of the settings (i.e. adds the /gen folder back as a source folder, and restores the output folder to /bin/classes instead of /target/classes), while leaving the maven /target/generated-sources/r folder and breaking the build of the project.
I would like to use the default Maven folder structure with the Eclipse ADT. Any ideas on how I can do this?
Thanks
Responding to the comments:
Yes, am using the m2e-android plugin.
Because ADT is still generating R.java in /gen, Eclipse is failing to compile if there still exists an R.java under target/generated-sources/r due to duplicated class definitions.
Unfortunately, the Android Connector for M2E does not support moving generated folders like the ADT gen folder to other directories. This is a restriction of the ADT, although there is a ticket to explore a way around this restriction:
https://github.com/rgladwell/m2e-android/issues/68
If you'd like to stay updated, please comment on this ticket.
This seems to be working for me:
Let the ADT keep gen as a source folder, but change the output folder for that source folder to be /target/classes.
I can build in both eclipse and using maven from the command line, and I don't get duplicate R class problems in either case. Essentially, Eclipse generates the R.java file in /gen, while maven generates it in /target/generated-sources/r, but both builds put the R.class file under /target/classes.
Note: at one point I had added /gen as an additional source directory in my pom.xml using build-helper-maven-plugin, and I had to remove that.

Categories

Resources