Eclipse is by far and away the most popular IDE among Java developers.
Table of Contents
Download and Install
- Read Development Environment for additional configuration information.
- Download Eclipse 3.2 or Eclipse 3.3Rc2 and use Callisto (Software Update) to get plugins for developing web and database-driven applications.
- Install Eclipse into $TOOLS_HOME/eclipse-3.2.
- When starting Eclipse, set your workspace directory to
C:\Sourceon Windows and
- In your project's directory, run mvn eclipse:eclipse. This will generate project files using the Maven Eclipse Plugin. If you're using a modular archetype, you may need to run mvn install eclipse:eclipse.
- Launch Eclipse and go to File > Import > Existing Projects into Workspace (under the General category). Select the root directory of your project, followed by the modules to import. Click Finish to complete the process.
Set the classpath variable
M2_REPO. Eclipse needs to know the path to the local maven repository. You should be able to do this by executing the command: mvn -Declipse.workspace=C:\Source eclipse:add-maven-repo.
If that doesn't work, can also define a new classpath variable inside Eclipse. From the menu bar, select Window > Preferences. Select the Java > Build Path > Classpath Variables page and set M2_REPO to equal
After configuring Eclipse, you should be able to compile your project and run tests from within your IDE. For tests that rely on pre-existing data, you may have to periodically run mvn dbunit:operation to re-populate your database. You shouldn't need to worry about deploying from Eclipse because you can use the Jetty Plugin (mvn jetty:run-war) or Cargo (mvn cargo:start -Dcargo.wait=true). If you'd prefer to use the Eclipse Web Tools Project to auto-deploy your project, see the Integration with WTP section below.
For more information on using Eclipse with Maven, see Maven's Eclipse Mini-Guide.
If you'd like to run Maven from Eclipse, see the Maven 2.x Eclipse Plugin.
Integration with Eclipse Web Tools Project (WTP)
Below are some quick instructions contributed by Shash Chaterjee. For more information see this thread.
- Use the AppFuse archetypes to create the project. I used the Struts-2 Modular version. If you're not using a modular archetype, you can skip to step #6.
- Under "myproject", create a new dir called pom. Copy myproject/pom.xml to myproject/pom/pom.xml.
- Modify myproject/pom.xml to change the artifactId to "myproject-pom".
- Modify core/pom.xml and web/pom.xml to change the parent pom artifactId to
"myproject-pom" and delete the relativePath element
- Modify myproject/pom.xml to add the "pom" module, in addition to core and web in the modules section
- Modify myproject/pom/pom.xml to remove the "core" and "web" modules
- Run mvn install eclipse:eclipse
<wb-resource deploy-path="/" source-path="src/main/webapp"/>
<wb-resource deploy-path="/" source-path="target/myproject-webapp-1.0-SNAPSHOT"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/resources"/>
<classpathentry kind="src" path="src/main/resources" excluding="ApplicationResources_zh*.properties ... **/*.java"/>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v5.5"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
- Preferences -> Java -> Installed JREs: Make sure default JRE is actually the JDK and not the JRE.
- Preferences -> Server -> Installed Runtimes: Pick an Apache -> Tomcat 5.5 server, and point it to your local installation.
- Import -> Existing Projects into Workspace: Point to your AppFuse project dir and import myproject-core and myproject-web.
- Open J2EE perspective, then Run->Run On Server (when prompted, connect it to the Tomcat server you configured previously)
- You may want to copy the launch configuration and then add all the properties defined in myproject/pom.xml.
One of the nice things about AppFuse 2.0 is you shouldn't have to use Maven for the most part. It has much better IDE support now. You should be able to run most of your tests from within Eclipse. If you can run your tests, you should be able to debug them as well. If you write your tests so they don't depend on data already being in the database, you should be able to run your tests all day long. If you depend on pre-existing data, you may have to run mvn dbunit:operation (same as ant db-load in AppFuse 1.x) every so often, or use DbUnit's Java API to pre-populate your database.
To debug your application while running it in Jetty , see Debugging with the Maven Jetty Plugin in Eclipse.
Tips and Tricks
- Ctrl+Shift+R will allow you to find any file within your project. It's a handy way to open files quickly without having to navigate through your source tree.
- Ctrl+Shift+T does the same, but restricted to Java source files (Java view only).
- Ctrl+Shift+E opens a dialog with the files you most recently open listed.
In order to clean up the project view in Eclipse, you can hide the files you don't need. First of all, make sure you're in the Java Perspective (Window → Open Perspective). Then click the little (down) arrow in the top right corner of the Package Explorer pane. Select Filters and check Libraries from external and Libraries from project. Click OK to continue.
Another useful Eclipse trick is to use abbreviated package names. It's a nice feature on projects where you're inflicted with super.long.package.name.syndrome. Go to Window -> Preferences -> Java -> Appearance. Check the "Compress all package names" checkbox and type "1." (no quotes) in the text field.