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
- Generate the Maven webapp project skeleton
- Change the directory structure
- Add dependencies
- 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:
.
├── 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:
.
├── pom.xml
└── src
└── main
├── java
├── resources
└── webapp
├── index.jsp
└── WEB-INF
└── web.xmlAccording 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:
- All subdirectories containing Java source code under
srcandweiboare moved to thesrc/main/javadirectory. - Configuration files
config.propertiesandlog4j.propertiesare moved to thesrc/main/resourcesdirectory. - All files under the
WebRootdirectory are moved as-is tosrc/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.
.
├── 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
└── javaAdd dependencies
The pom.xml file:
<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
<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>mvn jetty:run