首页 > 代码库 > 使用 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 示例如下:

 

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>mia</groupId>  
  5.   <artifactId>noti-service</artifactId>  
  6.   <version>1.0.0</version>  
  7.   <packaging>jar</packaging>  
  8.   <name>noti-service</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   
  11.   <properties>  
  12.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  13.     <spring.version>3.1.2.RELEASE</spring.version>  
  14.     <httpClient.version>4.1.3</httpClient.version>  
  15.   </properties>  
  16.   
  17.   <dependencies>  
  18.       
  19.     <dependency>  
  20.       <groupId>junit</groupId>  
  21.       <artifactId>junit</artifactId>  
  22.       <version>3.8.1</version>  
  23.       <scope>test</scope>  
  24.     </dependency>  
  25.       
  26.     <!-- uniform spring version and using new spring MVC annotation begin-->  
  27.           
  28.     <dependency>  
  29.         <groupId>org.springframework</groupId>  
  30.         <artifactId>spring-core</artifactId>  
  31.         <version>${spring.version}</version>  
  32.     </dependency>  
  33.     <dependency>  
  34.         <groupId>org.springframework</groupId>  
  35.         <artifactId>spring-web</artifactId>  
  36.         <version>${spring.version}</version>  
  37.     </dependency>  
  38.   
  39.     <dependency>  
  40.         <groupId>org.springframework</groupId>  
  41.         <artifactId>spring-orm</artifactId>  
  42.         <version>${spring.version}</version>  
  43.     </dependency>  
  44.       
  45.     <dependency>  
  46.         <groupId>org.springframework</groupId>  
  47.         <artifactId>spring-context</artifactId>  
  48.         <version>${spring.version}</version>  
  49.     </dependency>  
  50.   
  51.     <dependency>  
  52.         <groupId>org.springframework</groupId>  
  53.         <artifactId>spring-webmvc</artifactId>  
  54.         <version>${spring.version}</version>  
  55.     </dependency>  
  56.   
  57.     <dependency>  
  58.         <groupId>org.springframework</groupId>  
  59.         <artifactId>spring-tx</artifactId>  
  60.         <version>${spring.version}</version>  
  61.     </dependency>  
  62.     <!-- uniform spring version and using new spring MVC annotation end-->  
  63.       
  64.     <dependency>  
  65.         <groupId>org.codehaus.jackson</groupId>  
  66.         <artifactId>jackson-mapper-asl</artifactId>  
  67.         <version>1.9.2</version>  
  68.     </dependency>  
  69.       
  70.     <dependency>  
  71.         <groupId>org.codehaus.jackson</groupId>  
  72.         <artifactId>jackson-core-asl</artifactId>  
  73.         <version>1.9.2</version>  
  74.     </dependency>  
  75.       
  76.     <dependency>  
  77.         <groupId>log4j</groupId>  
  78.         <artifactId>log4j</artifactId>  
  79.         <version>1.2.16</version>  
  80.     </dependency>  
  81.       
  82.       
  83.     <dependency>  
  84.         <groupId>org.apache.ibatis</groupId>  
  85.         <artifactId>ibatis-sqlmap</artifactId>  
  86.         <version>2.3.4.726</version>  
  87.     </dependency>  
  88.       
  89.     <dependency>  
  90.         <groupId>commons-lang</groupId>  
  91.         <artifactId>commons-lang</artifactId>  
  92.         <version>2.4</version>  
  93.     </dependency>  
  94.       
  95.     <dependency>  
  96.         <groupId>commons-dbcp</groupId>  
  97.         <artifactId>commons-dbcp</artifactId>  
  98.         <version>1.2.2</version>  
  99.     </dependency>  
  100.       
  101.     <dependency>  
  102.              <groupId>commons-exec</groupId>  
  103.             <artifactId>commons-exec</artifactId>  
  104.             <version>1.0</version>  
  105.     </dependency>  
  106.       
  107.     <dependency>  
  108.         <groupId>mysql</groupId>  
  109.         <artifactId>mysql-connector-java</artifactId>  
  110.         <version>5.1.19</version>  
  111.     </dependency>  
  112.       
  113.     <dependency>  
  114.         <groupId>org.slf4j</groupId>  
  115.         <artifactId>slf4j-api</artifactId>  
  116.         <version>1.6.1</version>  
  117.     </dependency>  
  118.   
  119.     <dependency>  
  120.         <groupId>org.slf4j</groupId>  
  121.         <artifactId>slf4j-log4j12</artifactId>  
  122.         <version>1.6.1</version>  
  123.     </dependency>  
  124.       
  125.         <dependency>  
  126.             <groupId>org.apache.httpcomponents</groupId>  
  127.             <artifactId>httpclient</artifactId>  
  128.             <version>${httpClient.version}</version>  
  129.         </dependency>  
  130.           
  131.         <dependency>  
  132.             <groupId>org.apache.httpcomponents</groupId>  
  133.             <artifactId>httpcore</artifactId>  
  134.             <version>${httpClient.version}</version>  
  135.         </dependency>  
  136.           
  137.         <dependency>  
  138.             <groupId>org.apache.httpcomponents</groupId>  
  139.             <artifactId>httpmime</artifactId>  
  140.             <version>${httpClient.version}</version>  
  141.         </dependency>  
  142.       
  143.         <dependency>  
  144.             <groupId>commons-codec</groupId>  
  145.             <artifactId>commons-codec</artifactId>  
  146.             <version>1.6</version>  
  147.         </dependency>  
  148.           
  149.         <!-- api invoker -->  
  150.         <dependency>  
  151.             <groupId>commons-httpclient</groupId>  
  152.             <artifactId>commons-httpclient</artifactId>  
  153.             <version>3.1</version>  
  154.         </dependency>  
  155.           
  156.         <dependency>  
  157.             <groupId>dom4j</groupId>  
  158.             <artifactId>dom4j</artifactId>  
  159.             <version>1.6.1</version>  
  160.         </dependency>  
  161.           
  162.         <dependency>  
  163.             <groupId>json</groupId>  
  164.             <artifactId>json</artifactId>  
  165.             <version>2.3</version>  
  166.         </dependency>  
  167.           
  168.   </dependencies>  
  169.     
  170.   <build>  
  171.     <plugins>  
  172.         <plugin>  
  173.             <artifactId>maven-compiler-plugin</artifactId>  
  174.             <configuration>  
  175.                 <source>1.6</source>  
  176.                 <target>1.6</target>  
  177.                 <encoding>UTF-8</encoding>  
  178.                 <failOnError>false</failOnError>  
  179.             </configuration>  
  180.         </plugin>  
  181.   
  182.         <plugin>  
  183.             <artifactId>maven-jar-plugin</artifactId>  
  184.             <configuration>  
  185.                 <archive>  
  186.                     <addMavenDescriptor>false</addMavenDescriptor>  
  187.                 </archive>  
  188.             </configuration>  
  189.         </plugin>  
  190.   
  191.         <plugin>  
  192.             <groupId>org.apache.maven.plugins</groupId>  
  193.             <artifactId>maven-source-plugin</artifactId>  
  194.             <executions>  
  195.                 <execution>  
  196.                     <id>attach-sources</id>  
  197.                     <phase>verify</phase>  
  198.                     <goals>  
  199.                         <goal>jar</goal>  
  200.                     </goals>  
  201.                 </execution>  
  202.             </executions>  
  203.         </plugin>  
  204.   
  205.         <plugin>  
  206.             <artifactId>maven-assembly-plugin</artifactId>  
  207.             <version>2.2-beta-1</version>  
  208.             <configuration>  
  209.                 <descriptors>  
  210.                     <descriptor>assembly.xml</descriptor>  
  211.                 </descriptors>  
  212.                 <appendAssemblyId>false</appendAssemblyId>  
  213.             </configuration>  
  214.   
  215.             <executions>  
  216.                 <execution>  
  217.                     <id>make-assembly</id>  
  218.                     <phase>package</phase>  
  219.                     <goals>  
  220.                         <goal>single</goal>  
  221.                     </goals>  
  222.                 </execution>  
  223.             </executions>  
  224.         </plugin>  
  225.     </plugins>  
  226.   </build>  
  227. </project>  

 

 

        3.新建 web.xml

        src - main 下新建目录 webapp,webapp 下新建目录 WEB-INF,WEB-INF 下新建 web.xml,如下图所示:

技术分享

        根据项目的实际需要编辑 web.xml,作者的示例如下:

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app id="WebApp_ID" version="2.4"  
  3.     xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  5.     <display-name>noti-service</display-name>  
  6.   
  7.     <context-param>  
  8.         <param-name>log4jConfigLocation</param-name>  
  9.         <param-value>/WEB-INF/log4j.properties</param-value>  
  10.     </context-param>  
  11.   
  12.     <context-param>  
  13.         <param-name>webAppRootKey</param-name>  
  14.         <param-value>noti-service.root</param-value>  
  15.     </context-param>  
  16.   
  17.     <context-param>  
  18.         <param-name>log4jRefreshInterval</param-name>  
  19.         <param-value>60000</param-value>  
  20.     </context-param>  
  21.   
  22.     <context-param>  
  23.         <param-name>contextConfigLocation</param-name>  
  24.         <param-value>/WEB-INF/applicationContext*.xml</param-value>  
  25.     </context-param>  
  26.       
  27.     <listener>  
  28.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  29.     </listener>  
  30.       
  31.     <listener>  
  32.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  33.     </listener>  
  34.   
  35.     <filter>  
  36.         <filter-name>SetCharacterEncoding</filter-name>  
  37.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  38.         <init-param>  
  39.             <param-name>encoding</param-name>  
  40.             <param-value>utf-8</param-value>  
  41.         </init-param>  
  42.     </filter>  
  43.     <filter-mapping>  
  44.         <filter-name>SetCharacterEncoding</filter-name>  
  45.         <url-pattern>/*</url-pattern>  
  46.     </filter-mapping>  
  47.   
  48.       
  49.       
  50.     <!-- Spring MVC see /WEB-INF/spring-servlet.xml -->  
  51.     <servlet>  
  52.         <servlet-name>spring</servlet-name>  
  53.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  54.         <load-on-startup>1</load-on-startup>  
  55.     </servlet>  
  56.   
  57.     <servlet-mapping>  
  58.         <servlet-name>spring</servlet-name>  
  59.         <url-pattern>/</url-pattern>  
  60.     </servlet-mapping>  
  61.   
  62.     <!-- session timeout, unit: second -->  
  63.     <session-config>  
  64.         <session-timeout>120</session-timeout>  
  65.     </session-config>  
  66.   
  67. </web-app>  

 

 

        4.新建资源目录

        src - main 和 src - test 下分别新建 Source Folder 名为 resources:

技术分享

        5.修改 .classpath

        切换至 Eclipse 的 Navigator 视图,编辑 noti-service 项目下的 .classpath 文件内容如下:

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <classpath>  
  3.     <classpathentry kind="src" path="src/main/java"/>  
  4.     <classpathentry kind="src" path="src/main/resources"/>  
  5.     <classpathentry kind="src" path="src/test/java"/>  
  6.     <classpathentry kind="src" path="src/test/resources"/>  
  7.     <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>  
  8.     <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>  
  9.     <classpathentry kind="var" path="TOMCAT_HOME/lib/servlet-api.jar"/>  
  10.     <classpathentry kind="var" path="TOMCAT_HOME/lib/jasper.jar"/>  
  11.     <classpathentry kind="var" path="TOMCAT_HOME/lib/jsp-api.jar"/>  
  12.     <classpathentry kind="var" path="TOMCAT_HOME/lib/el-api.jar"/>  
  13.     <classpathentry kind="var" path="TOMCAT_HOME/lib/annotations-api.jar"/>  
  14.     <classpathentry kind="output" path="src/main/webapp/WEB-INF/classes"/>  
  15. </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 文件内容如下:

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <tomcatProjectProperties>  
  3.     <rootDir>/src/main/webapp</rootDir>  
  4.     <exportSource>false</exportSource>  
  5.     <reloadable>false</reloadable>  
  6.     <redirectLogger>true</redirectLogger>  
  7.     <updateXml>true</updateXml>  
  8.     <warLocation></warLocation>  
  9.     <extraInfo></extraInfo>  
  10.     <webPath>/noti</webPath>  
  11.     <webClassPathEntries>  
  12.         <webClassPathEntry>/noti-service/src/main/webapp/WEB-INF/classes</webClassPathEntry>  
  13.         <webClassPathEntry>org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER</webClassPathEntry>  
  14.     </webClassPathEntries>  
  15. </tomcatProjectProperties>  



 

        8.断点跟踪

        这时,你就可以单击工具栏里的小猫技术分享按钮,对自己的 JEE 项目进行断点跟踪调试了。项目最终截图如下:

技术分享

使用 Eclipse 的 Maven 2 插件开发一个 JEE 项目