eclipse下构建maven的web项目(三)

一、Maven web项目下的完善pom.xml和引入相应类包

1. 打开pom.xml 加入属性<properties>标签,根据项目需要加入相应属性,如:

<properties>
<spring.version>3.1.0.RELEASE</spring.version>
<org.slf4j.version>1.6.4</org.slf4j.version>
<org.strtus.version>2.1.8.1</org.strtus.version>
<java.home>C:\Program Files (x86)\Java\jdk1.6.0_37</java.home>
</properties>

2. 在pom.xml底部加入<profiles>标签补充JAVA API 和 相应属性标签

<profiles>
<profile>
<id>development</id>
<activation>
<property>
<name>java.vendor</name>
<value>Sun Microsystems Inc.</value>
</property>
</activation>

<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/lib/tools.jar</systemPath>
</dependency>
</dependencies>

<properties>

<!-- database start -->
<aiyou-mongodb-host>127.0.0.1</aiyou-mongodb-host>
<aiyou-mongodb-port>27017</aiyou-mongodb-port>
<aiyou-mongodb-username>username</aiyou-mongodb-username>
<aiyou-mongodb-password>password</aiyou-mongodb-password>
<aiyou-mongodb-databaseName>aiyou</aiyou-mongodb-databaseName>
<!-- database end -->

<!-- log start -->
<aiyou.log.level>INFO</aiyou.log.level>
<aiyou.log.path>d:/logs/你的项目名称</aiyou.log.path>
<aiyou.log.ConversionPattern>[%t] - %d - [%5p] - %l - %m%n</aiyou.log.ConversionPattern>
</properties>
</profile>
</profiles>

3. 补充<build>标签 构建属性(<build>标签放在<profiles>标签上面)

 <!-- 构建属性 -->
 <build>
   <finalName>aiyou</finalName> <!-- 项目名称 -->
   <resources> <!-- 代码源 -->
  <resource>
   <directory>src/main/resources</directory>
   <filtering>true</filtering>
  </resource>
 </resources>
 <testResources>
  <testResource>
   <directory>src/test/resources</directory>
   <filtering>true</filtering>
  </testResource>
 </testResources>


 <plugins><!-- 插件 -->
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-war-plugin</artifactId>
   <version>2.1-beta-1</version>
   <configuration>
    <archiveClasses>true</archiveClasses>
   </configuration>
  </plugin>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>2.1</version>
   <configuration>
    <source>1.6</source>
    <target>1.6</target>
    <encoding>UTF-8</encoding>
   </configuration>
  </plugin>
 </plugins>
 </build>

4.  指定引入库,将项目所要引入的类包指定引入库地址(<repositories>放在<build>标签上面)

<!-- 引入的库 -->
 <repositories>
  <repository>
   <id>com.springsource.repository.bundles.release</id>
   <name>EBR Spring Release Repository</name>
   <url>http://repository.springsource.com/maven/bundles/release</url>
  </repository>
  <repository>
   <id>com.springsource.repository.bundles.external</id>
   <name>EBR External Release Repository</name>
   <url>http://repository.springsource.com/maven/bundles/external</url>
  </repository>
  <repository>
   <id>com.springsource.repository.maven.release</id>
   <url>http://maven.springframework.org/release/</url>
   <snapshots>
    <enabled>false</enabled>
   </snapshots>
  </repository>
  <repository>
   <id>spring-milestone</id>
   <name>Spring Maven MILESTONE Repository</name>
   <url>http://maven.springframework.org/milestone</url>
  </repository>
  <repository>
   <id>maven.central</id>
   <url>http://repo1.maven.org/maven2</url>
  </repository>
 </repositories>

5. 引入相应的依赖,本例为搭建struts2+spring3+mongoDB,

所以除引入基本的类包如Junit、servlet-api、jsp-api、log4j相应包以外,还需要依赖struts2、srping3以及相互关联的包。详情:

<artifactId>junit</artifactId>

<artifactId>servlet-api</artifactId>

<artifactId>jsp-api</artifactId>

<!--slf4j 日志接口依赖包 -->

<artifactId>slf4j-api</artifactId>

<artifactId>slf4j-log4j12</artifactId>

<artifactId>jul-to-slf4j</artifactId>

<!-- struts2依赖包 -->

<artifactId>struts2-core</artifactId>

<artifactId>struts2-tiles-plugin</artifactId>

<artifactId>struts2-spring-plugin</artifactId>

<!-- spring依赖包 -->

<artifactId>spring-core</artifactId>

<artifactId>spring-beans</artifactId>

<artifactId>spring-web</artifactId>

 <artifactId>spring-context</artifactId>

<artifactId>spring-aop</artifactId>

<artifactId>spring-oxm</artifactId>

<artifactId>spring-tx</artifactId>

<artifactId>spring-data-mongodb</artifactId>

具体依赖包的作用和意思网上查下即可,这里就不多讲了。

6. web容器里引入相应类目 Tomcat

右键点击Properties点击Tomcat选择General 勾上 Is a Tomcat Project

选择Devloader Classpath 勾上Activate Devloader ,然后全部勾选,除了Java安装的JDK下的lib下的包,如:C:\Program Files (x86)\Java\jdk1.6.0_37\lib\下的,

还有去掉用户本地仓库下的jsp-api-2.0.jar 和servlet-api-2.5.jar选项,因为tomcat本身就有该两个类包。

最后去掉最后一个选项,名为:org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER

点击Apply按钮后点击OK,这样Maven下搭建web项目框架初步完成。

具体的struts2+spring3+mongoDB搭建代码,以后慢慢补充。

相关推荐