Migrating a Java Web Project from MyEclipse to Maven

Nov 6, 2016 · 702 words

A year ago, I collaborated with checky to write a Java Web project called IFTTT-Web. At the time, this project was a programming assignment for a course. To complete it as quickly as possible, we chose to develop it in MyEclipse. After the course ended, the code sat idle. One day, I wanted to reconfigure the project in Eclipse (since MyEclipse is paid software and I no longer have it installed), but I failed. Java Web projects are relatively complex; besides Java code, they include JSP pages, JavaScript scripts, CSS files, and various other files required for a webapp. Trying to get an existing project running again in Eclipse is indeed quite difficult.

At that time, I tried a more “primitive” approach: manually compiling the Java source files via the command line and copying the compiled bytecode and webapp files into the Tomcat directory. This method worked, of course, but it required too many manual operations for deployment and was clearly not a long-term solution. It wasn’t until recently that I realized the importance of Maven in the Java world and started using it in my projects. This time, I attempted to rebuild that year-old project using Maven and finally succeeded. Here, I am recording the entire process.

Main Steps

  1. Generate the Maven webapp project skeleton
  2. Change the directory structure
  3. Add dependencies
  4. Use Jetty

Generate the Maven webapp project skeleton

For clarity and intuition, I will first generate the Maven webapp directory skeleton and then move the existing files into their corresponding directories. Run mvn archetype:generate, making sure to select org.apache.maven.archetypes:maven-archetype-webapp for the artifactId.

Change the directory structure

First, let’s look at the directory structure before the update:

text
.
├── src
│   ├── log4j.properties
│   ├── database
│   ├── model
│   │   ├── data
│   │   └── task
│   ├── servlet
│   └── task
│       ├── action
│       ├── mail
│       ├── run
│       ├── trigger
│       └── weibo
│── weibo
│   ├── config.properties
│   ├── log4j.properties
│   └── weibo4j
│       └── ...
├── lib
│   └── ...
└── WebRoot
    ├── assets
    ├── component
    ├── css
    ├── dashboard.jsp
    ├── favicon.ico
    ├── font
    ├── index.jsp
    ├── js
    ├── login.jsp
    ├── META-INF
    │   └── MANIFEST.MF
    ├── register.jsp
    └── WEB-INF
        └── web.xml

This is a typical directory structure for a Java Web project generated by MyEclipse, where the WebRoot directory contains various files required by the webapp. Note that both src and weibo are source folders.

The Maven project skeleton generated in the previous step looks like this:

text
.
├── pom.xml
└── src
    └── main
        ├── java
        ├── resources
        └── webapp
            ├── index.jsp
            └── WEB-INF
                └── web.xml

According to Maven’s conventions for webapp projects, src/main/java holds the Java source code, src/main/resources holds resource files, and src/main/webapp holds files like JSP, JavaScript, and CSS, with web.xml located in the src/main/webapp/WEB-INF directory.

Thus, the original files are moved as follows:

  1. All subdirectories containing Java source code under src and weibo are moved to the src/main/java directory.
  2. Configuration files config.properties and log4j.properties are moved to the src/main/resources directory.
  3. All files under the WebRoot directory are moved as-is to src/main/webapp.

It feels quite simple to move them :) The original lib directory is no longer needed, as we will soon use Maven dependencies to handle this task. You can view the directory structure after moving the files here.

text
.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   ├── database
    │   │   ├── model
    │   │   │   ├── data
    │   │   │   └── task
    │   │   ├── servlet
    │   │   ├── task
    │   │   │   ├── action
    │   │   │   ├── mail
    │   │   │   ├── run
    │   │   │   ├── trigger
    │   │   │   └── weibo
    │   │   └── weibo4j
    │   ├── resources
    │   │   ├── config.properties
    │   │   ├── log4j.properties
    │   │   └── schema.sql
    │   └── webapp
    │       ├── assets
    │       ├── component
    │       ├── css
    │       ├── dashboard.jsp
    │       ├── favicon.ico
    │       ├── font
    │       ├── index.jsp
    │       ├── js
    │       ├── login.jsp
    │       ├── META-INF
    │       │   └── MANIFEST.MF
    │       ├── register.jsp
    │       └── WEB-INF
    │           └── web.xml
    └── test
        └── java

Add dependencies

The pom.xml file:

xml
<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.11</version>
		<scope>compile</scope>
	</dependency>
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
	</dependency>
	<dependency>
		<groupId>javax.mail</groupId>
		<artifactId>mail</artifactId>
		<version>1.4</version>
	</dependency>
	<dependency>
		<groupId>commons-httpclient</groupId>
		<artifactId>commons-httpclient</artifactId>
		<version>3.1</version>
</dependency>

Use Jetty

xml
<plugin>
	<groupId>org.eclipse.jetty</groupId>
	<artifactId>jetty-maven-plugin</artifactId>
	<version>9.2.11.v20150529</version>
	<configuration>
		<scanIntervalSeconds>10</scanIntervalSeconds>
		<webApp>
			<contextPath>/</contextPath>
		</webApp>
	</configuration>
</plugin>
bash
mvn jetty:run