首页 > 代码库 > 使用 Eclipse 的 Maven 2 插件开发一个 JEE 项目
使用 Eclipse 的 Maven 2 插件开发一个 JEE 项目
1.新建 Maven 项目
Eclipse 的 Package Explorer 视图下右击 -> New -> Maven -> Maven Project -> Next -> Select project name and location 对话框直接点 Next -> Select an Archetype 对话框,Catalog 选择 "All Catalogs",Filter 选择 Group Id 为 "org.apache.maven.archetypes"、Artifact Id 为 "maven-archetype-quickstart"、Version 为 "1.1" 的那个,点 Next -> Enter an artifact id 对话框,Group Id 输入项目组 "mia",Artifact Id 输入 "noti-service"(最好是你的项目名),Version 输入版本号 "1.0.0",Package 输入主干包名 "com.defonds.noti",点击 Finish,新的 Maven 项目 noti-service 生成。如下图所示:
2.编辑 pom.xml
根据项目的需要,引入所依赖的包。作者的 pom.xml 示例如下:
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>mia</groupId>
- <artifactId>noti-service</artifactId>
- <version>1.0.0</version>
- <packaging>jar</packaging>
- <name>noti-service</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <spring.version>3.1.2.RELEASE</spring.version>
- <httpClient.version>4.1.3</httpClient.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <!-- uniform spring version and using new spring MVC annotation begin-->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- uniform spring version and using new spring MVC annotation end-->
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.2</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-core-asl</artifactId>
- <version>1.9.2</version>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.16</version>
- </dependency>
- <dependency>
- <groupId>org.apache.ibatis</groupId>
- <artifactId>ibatis-sqlmap</artifactId>
- <version>2.3.4.726</version>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.4</version>
- </dependency>
- <dependency>
- <groupId>commons-dbcp</groupId>
- <artifactId>commons-dbcp</artifactId>
- <version>1.2.2</version>
- </dependency>
- <dependency>
- <groupId>commons-exec</groupId>
- <artifactId>commons-exec</artifactId>
- <version>1.0</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.19</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.6.1</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.6.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- <version>${httpClient.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpcore</artifactId>
- <version>${httpClient.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpmime</artifactId>
- <version>${httpClient.version}</version>
- </dependency>
- <dependency>
- <groupId>commons-codec</groupId>
- <artifactId>commons-codec</artifactId>
- <version>1.6</version>
- </dependency>
- <!-- api invoker -->
- <dependency>
- <groupId>commons-httpclient</groupId>
- <artifactId>commons-httpclient</artifactId>
- <version>3.1</version>
- </dependency>
- <dependency>
- <groupId>dom4j</groupId>
- <artifactId>dom4j</artifactId>
- <version>1.6.1</version>
- </dependency>
- <dependency>
- <groupId>json</groupId>
- <artifactId>json</artifactId>
- <version>2.3</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- <encoding>UTF-8</encoding>
- <failOnError>false</failOnError>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-jar-plugin</artifactId>
- <configuration>
- <archive>
- <addMavenDescriptor>false</addMavenDescriptor>
- </archive>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-source-plugin</artifactId>
- <executions>
- <execution>
- <id>attach-sources</id>
- <phase>verify</phase>
- <goals>
- <goal>jar</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.2-beta-1</version>
- <configuration>
- <descriptors>
- <descriptor>assembly.xml</descriptor>
- </descriptors>
- <appendAssemblyId>false</appendAssemblyId>
- </configuration>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
- </project>
3.新建 web.xml
src - main 下新建目录 webapp,webapp 下新建目录 WEB-INF,WEB-INF 下新建 web.xml,如下图所示:
根据项目的实际需要编辑 web.xml,作者的示例如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>noti-service</display-name>
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>/WEB-INF/log4j.properties</param-value>
- </context-param>
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>noti-service.root</param-value>
- </context-param>
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/applicationContext*.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <filter>
- <filter-name>SetCharacterEncoding</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>utf-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>SetCharacterEncoding</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <!-- Spring MVC see /WEB-INF/spring-servlet.xml -->
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!-- session timeout, unit: second -->
- <session-config>
- <session-timeout>120</session-timeout>
- </session-config>
- </web-app>
4.新建资源目录
src - main 和 src - test 下分别新建 Source Folder 名为 resources:
5.修改 .classpath
切换至 Eclipse 的 Navigator 视图,编辑 noti-service 项目下的 .classpath 文件内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <classpath>
- <classpathentry kind="src" path="src/main/java"/>
- <classpathentry kind="src" path="src/main/resources"/>
- <classpathentry kind="src" path="src/test/java"/>
- <classpathentry kind="src" path="src/test/resources"/>
- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
- <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
- <classpathentry kind="var" path="TOMCAT_HOME/lib/servlet-api.jar"/>
- <classpathentry kind="var" path="TOMCAT_HOME/lib/jasper.jar"/>
- <classpathentry kind="var" path="TOMCAT_HOME/lib/jsp-api.jar"/>
- <classpathentry kind="var" path="TOMCAT_HOME/lib/el-api.jar"/>
- <classpathentry kind="var" path="TOMCAT_HOME/lib/annotations-api.jar"/>
- <classpathentry kind="output" path="src/main/webapp/WEB-INF/classes"/>
- </classpath>
编辑好保存后,Package Explorer 视图下的 noti-service 如下:
6.部署到 tomcat 下
首先要安装 tomcat 插件,可以参考《集成 Tomcat 插件到 Eclipse 的过程》。
安装好 tomcat 插件后,Package Explorer 视图下右击项目名,点击 Properties 打开 Properties 对话框,Tomcat -> 勾选 Is a Tomcat Project,输入 Context name 内容为 "/noti",输入 Subdirectory to set as web application root(optional) 内容为 "/src/main/webapp",(可以参考下图)点击 OK。
这时项目根目录下会有 .tomcatplugin 生成,而 %tomcat%/conf/Catalina/localhost 目录下会有 noti.xml 生成。
7.修改 .tomcatplugin
切换至 Eclipse 的 Navigator 视图,编辑 noti-service 项目下的 .tomcatplugin 文件内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <tomcatProjectProperties>
- <rootDir>/src/main/webapp</rootDir>
- <exportSource>false</exportSource>
- <reloadable>false</reloadable>
- <redirectLogger>true</redirectLogger>
- <updateXml>true</updateXml>
- <warLocation></warLocation>
- <extraInfo></extraInfo>
- <webPath>/noti</webPath>
- <webClassPathEntries>
- <webClassPathEntry>/noti-service/src/main/webapp/WEB-INF/classes</webClassPathEntry>
- <webClassPathEntry>org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER</webClassPathEntry>
- </webClassPathEntries>
- </tomcatProjectProperties>
8.断点跟踪
这时,你就可以单击工具栏里的小猫按钮,对自己的 JEE 项目进行断点跟踪调试了。项目最终截图如下:
使用 Eclipse 的 Maven 2 插件开发一个 JEE 项目