Project setup: Having one web project depend on another - eclipse

I would like to have two "Dynamic Web Projects" in Eclipse. One basically "extends" the other so I would like to define the second as having access to the code and the JARs of the first.
Is this possible in Eclipse? If so, how?
In the "Java Build path" of the second project, in the "Projects" tab I have included the first project. In the "Java Build path" of the first project, in the "Order and Export" I have marked that its JARs etc should be exported. Compilation works fine.
When I deploy the second project, it cannot find any of the JARs or the code of the first project. If I go to "Deployment Assembly" of the second project and add the first project, it inserts "WEB-INF/lib/first.war" into the deployment assembly; this isn't very helpful, a WAR doesn't belong there. I see no other way to add compiled code and JARs from the first project into the deployment of the second.
What do I do?

I think what you want to do is to open the plugin.xml of the project you want to add the dependency to. On the dependencies tab you can add the project you it needs to depends on. I think this will keep you going.
Regads

Related

Adding project dependency to web project in Eclipse

I have a Dynamic Web Project that gets deployed as a WAR that I have created in Eclipse. The project depends on 2 other small projects. I have them marked for export, and have them on the build path, but Eclipse warns me that it will not be exported, and sure enough, I get a ClassNotFoundException when I try to compile.
If need be, I can pull all the java files into a .zip file, but I don't know how to add a .zip file as a dependency. I'm sure I'm making this harder than it needs to be.
Open the web project's Properties dialog (context menu, or Project menu from the menu bar) and go to the Deployment Assembly page. This is where you tell it to also make them jars when deploying.

Maven in eclipse not showing all folders

I am new to Maven and have been trying to get a project working with Eclipse, hibernate,Maven and mysql. I am stuck at the very first step. I have everything configured properly i think and if i create a new Maven project in eclipse it does not show me any folder under src/main or src/test. although if i go back to that folder in the workspace it has a src/main/java
FUrthermore the src/main/resources folder is not created at the time of project creation?
Any clue what the problem maybe or how i can fix it?
Thank you!
For your first problem, when you create a new Maven project the folders that get created depend on the archetype you choose. Assuming you chose quickstart, then it does create (assuming you chose com.example as your package in the wizard and example-project as your artifactId):
And it configures the project so that /src/main/java is in the Build Path. That said, if you are viewing your project in the Package Explorer view, then the packages are shown outside of the folder structure. So, you would see the the com.example.example_project package containing App.java in the build folder /src/main/java and you would see the com.example.example_project package containing AppTest.java in the build folder /src/test/java. These would show up above the libraries which is above non-build folders which is where you see the src folder.
To answer your second question, no, /src/main/resources is not generated assuming you chose the quickstart archetype (this is governed by the quickstart archetype and does the same thing whether generated in eclipse or on the command line).
And third, to fix this (I assume you mean add the resources folder), find the src/java folder (below the libraries), right click and choose New->Folder. Name it resources. Then right click your project, choose Maven->Update Project. This will cause maven eclipse to reconfigure the project according to the Maven configuration which will result in the /src/main/resources being added to the build path. As such, it will get moved above the libraries next to /src/main/java and /src/test/java.

The "Order and Export" tab in "Java Build Path"

What's the use of "Order and Export" tab in "Java Build Path" dialog box?
By the name 'order', I can guess that JRE System library is searched before Plug-in Dependencies, but why do we have 'Export' in Build Path ? And what's it for the button left side of the elements?
ADDED
It's the example that I borrowed from Eclipse distilled.
The orderes.webapp depends on orders, but it's likely to use the classes defined in ubl and catalog.
In such a case, orders can export catalog and ubl so that the packages that depends on it can use catalog and ubl.
And something more about "Order and Export".
The check of the left side button modifies the .classpath file to add exported=true such as <classpathentry exported="true" kind="lib" path="lib/log4j-1.2.7.jar"/>.
The src is always checked, and you can't uncheck it.
References
Interpreting eclipse .classpath file. What does 'kind="con"' and 'exported="true"' mean?
Configuring an Eclipse Java Project
Java Build Path
This tab seems to pull double duty (see bottom of this help page for small amount of detail).
On one hand, it functions as the order of resolution for resources used in the building of the project in question (the "Order" part). On the other, it also serves to define what parts of this build path are contributed to dependent projects' (those listing this project in the "projects" tab for their view of this preference panel) build paths (the "Export" part).
This latter function is why the checkboxes exist on the left hand side, as you ask (note that the '-' signs cannot be unchecked, as source files defined in this project cannot be deselected, as stated in help file). A dependent project that expects some resource from the contributing project will not compile/run if the contributing project doesn't export that resource.
EDIT: see this SO post for corroboration.
a very good example from Coderanch by Ilja Preuss
Say you have junit.jar in the build path of project A. Project B
depends on project A.
Now you write a junit test in project B. If project A exports
junit.jar, project B can use it at compile time - no more action
necessary. If A doesn't export it, B doesn't know about it - you will
have to explicitely put it into its build path, too.
Order and Export tab as your rightly guessed shows the order of libraries which will be included in your project build path.
Using the buttons up and down you can adjust your libraries order according to your project needs.
To answer your specific question, if this project were to be added as a dependency for another Java project, the order would be used to resolve classes and packages in this project when running the other project. Those classes and packages are said to be exported to/for the other running project's class path.
There is one case where-in I found this useful. I had to use a sun.* class of an IBM jar. This class was also present in local jre, but my requirement was to use IBM's class. But by default,the compiler would refer the local jdk class only.
In the eclipse order and export tab, I moved the external jar ahead of the JRE lib. Now, the compiler would refer to the class from IBM jar instead of local JRE.

How to get peer project source in eclipse and maven

creating a parent project with only a pom.xml, and lots of sub projects such as:
my-web
my-core
my-backoffice
etc. is easy, and the sonotype eclipse plugin does most of the work.
However, getting one project to know about the source in the other project seems to be hard. E.g. when you are debugging the my-web project, and step into my-core, eclipse doesnt know where to get the source.
Looking in the Java Build Path in eclipse, the maven plugin has added my-core as a folder under "Web App Libraries". I.e. its not using the my-core-0.0.1.SNAPSHOT.jar or similar, its using the raw java files. Great!
But how to tell maven to tell eclipse to look for the source in the same place?
Im not really sure where to start. Im guessing its possible to get maven to put the source in a special jar using the maven-soure-plugin, but this will usually be out of sync with the actual java files which the web project seems to be using directly.
A quick and dirty solution is to manually Edit the Java Build Path for each project, and add my-core and other dependant projects in the "Projects" tab. Is this best practice? Any other suggestions?
A quick and dirty solution is to
manually Edit the Java Build Path for
each project, and add my-core and
other dependant projects in the
"Projects" tab.
If this doesn't happen automatically you have a configuration problem.
My Guess would be that you have a version mismatch between the pom dependencies and the actual project versions. Or your projects have an unusual name template (m2eclipse resolves projects by their artifactId AFAIK)
Either way, what always helps is in your Debug configuration (Run > Debug Configurations ...) select the Source tab and just Add... the selected projects.

Eclipse classpath and NoClassDefFoundError

I'm going to try to give as much detail as possible here, pardon me if some is irrelevant.
I have two projects in eclipse. Project 1: com.myworkplace.parent, with code in the package of the same name. Project 2: com.myworkplace.child, with code in package of the same name (I moved my code to that package, from the default package, if that makes a difference). Both are located in my workspace folder and structured the way you'd expect them to be, as far as I can tell.
I've added child to the build path of parent in eclipse using Java Build Path -> Projects -> Add. Parent's .classpath file contains the entry:
<classpathentry combineaccessrules="false" kind="src" path="/com.myworkplace.child"/>
I add a reference to a com.myworkplace.child.Child class in parent, import it, compile it with no errors, run and get:
java.lang.NoClassDefFoundError: com/myworkplace/child/Child
What gives?
Edit: The parent application is an RCP app, maybe OSGI (I really don't know much about RCP and related stuff.) Some googling leads me to believe it might have something to do with this.
Since your application is an RCP app there is a difference between the eclipse classpath that is used while you are editing code and the OSGi classpath that is used at runtime. It sounds like you need to add the com.myworkplace.child plugin to the list of dependencies of the com.myworkplace.parent plugin.
Open either plugin.xml or META-INF/manifest.mf in the parent project. Eclipse should open the PDE plugin editor (a form based editor for the underlying config files). Click the Dependencies tab at the bottom and add your child plugin to the "Required Plug-ins" list on the left hand section of the form. Alternately you could add the com.myworkplace.child package to the "Imported Package" list on the right. The difference between the two is beyond the scope of this answer, but you can read about that in the eclipse docs now that you (hopefully) are moving in the right direction.
We found that even doing all the proper things in the eclipse rcp project, we were still getting such errors. The solution was to delete the application's workspace directory (by default it would store workspace settings under .eclipse, but we've tweaked our code to use another dir that is not shared with other eclipse stuff).
When we removed this dir after a rebuild, our app worked. Apparently it was storing the classpath somehow along with other settings, but we didn't investigate the exact details.
(NOTE: This applies only if you've called the IWorkbenchConfigurer.setSaveAndRestore method passing a boolean value of 'true' - which means that your application will save and restore various rcp settings after a shutdown of the application and subsequent relaunch.)
If your code is running in an application server, you need to configure the Classpath correctly for that application server.
To do that, find your Servers tab, double-click it, click Open Launch Configuration in the new tab that appears in the main editing area, click the Classpath tab and ensure the Classpath there is correct.