SSH2+Maven整合开发(一)
用ssh2进行开发非常关键的步骤就是搜集jar包,各种依赖和冲突会极大地干扰开发过程,随着maven的逐渐流行,maven似乎已成为管理的标配技术,为此本人花了一点时间做了个例子,并将整个过程记录下来,方便后期的开发。
一、环境
Eclipse:eclipse-jee-kepler-SR2-win32
Spring tools:springsource-tool-suite-3.5.1.RELEASE-e4.3.2-updatesite
Hibernate tools:hibernatetools-Update-4.1.2.Final
maven3:3.2.1
tomcat:apache-tomcat-7.0.54
1.1 tomcat配置
打开tomcat-users.xml,增加如下配置: <role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user password="111111" roles="manager-gui,manager-script" username="admin"/>
1.2 Maven配置
将maven解压到D盘某个目录(随便那个目录都行,只要做系统不丢失就行),找到conf目录下的setting.xml文件,打开后,找到如下被注释掉的代码段
<localRepository>/path/to/local/repo</localRepository>
将其值改为一个确定的目录,如D:\maven-dependcies,该目录用于保存maven下载的jar或者插件。
然后继续找到如下被注释掉的代码段:
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
将其复制,然后在后面粘贴,需要说明一下id对应后面将要讲解的pom.xml中的server内容,username和password对应1.1 中提到的username和password(后面章节会讲到)。
上面步骤完成后,可在windows环境变量中增加maven的配置,网上很多方法,在此不再赘述。
1.3 Eclipse配置
eclipse kepler版本默认包括maven插件,对于spring tools和hibernate tools两个工具只需要在eclipse中选择“install new software”,并选择压缩文件安装即可,无需解压。
二、ssh2框架开发
点击Eclipse菜单栏File->New->Ohter->Maven得到如下图所示对话框:
选中Maven Project并点击Next,到下一个对话框继续点击Next得到如下对话框
如图示操作,选择maven-archetype-webapp,点击Next
按图示填写好groupId, artfactId,version等信息,点击Finish。
由此我们成功创建了一个简单的Maven项目,项目结构如图所示:
打开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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lytech.app</groupId>
<artifactId>security</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>security Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- ==================================== -->
<!-- ===== Spring,Hibernate,JDBC 版本 === -->
<!-- ==================================== -->
<struts2.version>2.3.16.3</struts2.version>
<spring.version>4.0.6.RELEASE</spring.version>
<hibernate.version>4.3.6.Final</hibernate.version>
<spring-security.version>3.2.5.RELEASE</spring-security.version>
<aspectj.version>1.6.9</aspectj.version>
<!-- ==================================== -->
<!-- ========== 其他依赖包的 版本 ====== -->
<!-- ==================================== -->
<activiti.version>5.16.1</activiti.version>
<mysql.version>5.1.32</mysql.version>
<junit.version>4.11</junit.version>
<druid.version>1.0.7</druid.version>
<log4j.version>1.2.17</log4j.version>
<slf4j.version>1.7.7</slf4j.version>
<fastjson.version>1.1.41</fastjson.version>
<ant.version>1.9.4</ant.version>
<druid.version>1.0.7</druid.version>
<!-- ==================================== -->
<!-- ========== 发布名称 ====== -->
<!-- ==================================== -->
<finalName>security</finalName>
</properties>
<dependencies>
<!--************jsp****************** -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<!--************Struts2****************** -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
<exclusions><!-- 与hibernate-core中的依赖重复 -->
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--************Spring****************** -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!--************aspect****************** -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!--************Spring security****************** -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version>
</dependency>
<!--************Hibernate****************** -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--************log4j****************** -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--************mysql jdbc****************** -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--************ant压缩****************** -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>${ant.version}</version>
</dependency>
<!--************alibaba json****************** -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!--************alibaba druid****************** -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--************slf4j****************** -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!--************commons lib****************** -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<!--************junit****************** -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!--************Activiti****************** -->
<!-- ************************************* -->
<!-- commons-logging和jcl-over-slf4j会发生冲突,commons-logging由spring-core关联 -->
<!-- ************************************* -->
<!-- <dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency> -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/${finalName}</path>
<username>admin</username>
<password>111111</password>
</configuration>
</plugin>
</plugins>
</build>
</project>
在依赖中有个问题一直没有解决,如果将activiti中的注释打开,则eclipse中的hibernate配置会发生slf4j冲突的错误,这个问题一直没有解决掉,希望高手能不吝指正。
下面讲讲build部分配置。我是用的tomcat7,和以前的版本对于热部署还是有区别的,在执行deploy命令时一直出现错误,看了http://stackoverflow.com/questions/5410479/tomcat-maven-plugin-403-error后才得以解决,看来国外高手还是厉害,至少对知识的共享精神比国人无私的多。<server>节点的内容在第一部分讲解过了,只要对应setting.xml,中的<id>即可,<path>就是部署到tomcat的war文件名,username和password参考前面部分。
选择pom.xml,右键run as》maven build...,分别建立如下几个命令:
clean package
tomcat7:deploy
tomcat7:redeploy
记住tomcat7:redeploy一定要保证工程在运行状态。配置方式如下:
在new中选择建立一个新的server
选择tomcat7,将刚才建立的工程关联到新服务器下,然后双击tomat
弹出配置窗口如下:
Server Locations中选择Use Tomcat installation,deploy path随便设置一个路径名,工程就会上传到该目录。server配置完毕后,先运行它,然后选择刚才配置的maven命令,首先执行clean package,将会在target中生成目标,同时将jar包上传到lib中,如果执行tomcat7:deploy,则将在tomcat的webapps中存放一个${finalName}为文件名的war包。