How to create a new Dynamic Web Project in Eclipse from a Subversion Checkout

Hi, here are the steps to checkout a project from Subversion and get it running in Eclipse using the built-in Tomcat.  Thanks to Chris Brooks for encouraging/inspiring me to figure out a better way to set up my development environment after watching me use clunkier habits.  ;)   Apparently, this style of configuration is called "Hot Code Replacement".  Three's a cool name for ya.

1.  In Eclipse, choose New -> Dynamic Web Project (might have to dig for this type under "Other -> Web -> Dynamic Web Project")

2. For Project Name, type the project (ex, "myproject") , or, if you want the code deployed into the root of your Tomcat, type "ROOT". 

3. For "Source folders on build path", I deleted the "src" that's there by default because we will be getting our own "src" from subversion.

4.  For "Context root", I left that as-is (ex. your project name or "ROOT").  For "Content directory", change this from "WebContent" to "web" so as to match the actual folder name in SVN.  (This folder will automatically get overwritten when we do the checkout.)

5. Now right-click on your project folder in the Project Explorer and choose Team -> Share Project, then pick SVN, then pick the repo's URL, then choose "Use specified folder name" and surf to the "trunk".  *** Eclipse might add an extra folder at the end of the svn URL, so you might need to delete that.  You can comment "Initial import".  Next you might get a warning saying the specified folder already exists in the repo.  Say YES it's ok to check out the folder and connect it to the existing location.

6.  After everything is checked out, right click on the new "src" file that has just been created and choose Build Path -> "Use as source folder".  Also do this with the "lib" folder.

7. Right click on the project folder and choose Properties.  Next go to Java Build Path, then the Libraries tab.  Click "Add JARs" and individually add each Jar file that was in your lib folder. Click OK.

8.  Right click on the project folder and choose Properties and go into Deployment Assembly.  Hopefully it will have automatically mapped "/web" to "/" for you, but if not you can add that manually.  (i.e. Source should be "/web" and Deploy Path should be "/")... do this by going Add-> Folder and navigate to "web".  This will create a new entry in the little table, then you can double click the cell under the "Deploy Path" column to change it to WEB-INF/classes.  Click Apply and OK.

9. Similarly, map "/src" to "WEB-INF/classes".  For me, I had to change "/lib" to "WEB-INF/lib" (it was set to WEB-INF/classes for me).  I also had to individually map collections of jars in subfolders in the lib, such as "/lib/jerseyclient" to "WEB-INF/lib", and map "lib/log4j" to "WEB-INF/lib", and "lib/mysql" to "WEB-INF/lib".

9.b) We also have to figure out Deployment Assembly stuff for  .properties files, context.xml, web.xml etc..  Normally, I keep all my configuration files in a "conf" folder, and I have one sub folder for "dev" and another sub folder for "prod".  However, Eclipse doesn't let you map individual files in the Deployment Assembly.  This sucks and it's ugly, but we have no choice here but to COPY the properties files in "Dev" into the appropriate folders, i.e. copy context.xml int META-INF, web.xml into WEB-INF, and most other things into WEB-INF/classes.   A logical way to think about this is that the "conf" folder with "dev" and "prod" is good for producing war files with ANT.  But if you want hot code deployment, you have to develop with the configuration files hardwired into the directory hierarchy.

10.  Next go under the Servers tab.  If you don't have it, it's under Window ->
Show View ->  Servers. Now if you don't have any servers set up yet, you'll see a link to Define a new server from the new server wizard.  Click that and choose a Tomcat server, and Browse to whereever you have Tomcat installed.  (If you don't have it installed, all you have to do is download and unzip anywhere.)  Right click on the Tomcat server and choose "Add and Remove".

11.  Next, you can move any of your Dynamic Web Projects from the "Available" column to the "Configured" column.  Move your project over to the right and click Finish.

12.  Now under Servers you should be able to right-click Tomcat and choose Start.  It should show you a familliar Tomcat console as it starts.  Next click the little globe icon in the tool bar, "Open Web Browser" and type in http://localhost:8080

13.  At step 12 you might actually see your web app.  Woo hoo!  Or, you might get a blank white screen.  Awwwe.  I feel your pain and you have my empathy.  Here are some things you can try if it's blank.

14.  Check the console (it's a tab in Eclipse).  If this tab isn't visible for you, call it up with Window --> Show View --> Console.  One time, I noticed I was getting a "Class Not Found" error for one of my own classes!  This was because the complied Java classes weren't being copied to metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps (in the Eclipse workspace).  So, I manually created a mapping in the Deployment Assembly from the "build" folder to WEB-INF/classes.  I don't think that should be necessary, but, what can I do, it wasn't working, and this fixed it. :\

15.  Right click on the Tomcat under the Servers tab and click Publish.  This should copy your files into the Eclipse local tomcat.  If you are curious, this is located in your Eclipse workspace folder, under metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps.  (Sometimes that tmp0 might be a tmp1 so it's best just to use your Windows Explorer or Mac Finder to search for the folder called "wtpwebapps")  Sometimes you have to right click the Tomcat under the Servers tab and click "Restart in Debug".  Sometimes you have to right click the Tomcat and click "Clean" or "Stop" or quit Eclipse altogether and go back in.  Another thing I did was double-click on the Tomcat Server then at the very bottom click the "Modules" tab (at the bottom), then, you'll see a table where one of the columns is Auto Reload (for your project).   Edit this row, then in the popup window un-check "Auto reloading enabled" then Save.  Finally, really double check to make sure the lib folder is getting published to WEB-INF/lib and not WEB-INF/classes!!

16. b.  Another problem I had was errors popping up in the Problems window, such as "JspWriter cannot be resolved to a type", even though the jar was RIGHT THERE.  I solved this one by turning OFF the Jsp validator then turning it back on.

17.  Finally you should be able to see your system in the Eclipse browser window.  Try changing one of the JSP files and saving it, then push refresh in the Eclipse browser window.  Instant feedback!!!

The following resources helped me to create these directions.  Huge thanks to those developers!!

No comments:

Post a Comment

Translate