在maven仓库确定坐标的三个条件
classpath*:xxx.xml
加一个星代表不仅可以从源码路径拿配置文件,还可以从依赖的jar包拿配置文件
依赖就是对jar包的依赖
当依赖jar版本相同时,如果已获得依赖jar包,那么就算依赖jar更新也不会获得最新jar,除非依赖jar改版本
Maven的仓库
- 本地仓库
- 远程仓库(私服)
- 中央仓库
Maven环境变量配置
- 电脑要配置jdk
- 最终要运行的就是maven软件中bin目录的mvn命令,所以要配置maven的环境变量,为的就是在任何命令行处都能运行
环境变量的名称:MAVEN_HOME 变量值:就是maven软件解压的目录F:\maven
把MAVEN_HOME添加到path里
- 验证maven是否配置成功,打开dos窗口输入:mvn -v
注意:maven配置可能会失败,可以把系统变量设为绝对路径,不再用MAVEN_HOME
Maven常用命令
- mvn clean清理编译文件
- mvn compile 编译主目录文件
- mvn test 编译并运行test目录的代码
- mvn package 打包
- mvn install 就是把项目发到本地仓库
- mvn tomcat:run一键启动 默认tomcat6,如果是tomcat7,命令为mvn tomcat7:run
创建项目
处理编译版本
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
启动项目
右击项目–run as–maven build
重建索引 添加依赖
添加servlet-api.jar和jsp-api.jar 注意选择scope为provided,因为tomcat中有内置的jar包
依赖范围
- compile (编译)时需要,测试时需要,运行时需要,打包时需要
- provided (jsp-api.jar,servlet–api.jar)编译(compile)时需要,测试(test)时需要,运行时不需要,打包时不需要
- runntime(数据库驱动包)编译时不需要,测试时需要,运行时需要,打包时需要
- test(junit.jar)编译时不需要,测试时需要,运行时不需要,打包也不需要
依赖传递
只添加一个struts-core依赖,项目中会出现很多jar,这就是传递依赖
依赖版本冲突
- 第一声明优先原则(如下 sping4.2.4会被依赖)
<dependencies>
<!-- spring-beans-4.2.4 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- spring-beans-3.0.5 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24</version>
</dependency>
- 路径优先原则,自己直接添加的依赖,会比依赖中的依赖优先。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
- 排除原则
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
- 版本锁定原则,依赖的版本必须为锁定的版本
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<hibernate.version>5.0.7.Final</hibernate.version>
<struts.version>2.3.24</struts.version>
</properties>
<!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
分模块开发
依赖范围对传递造成的影响
模块B依赖模块A就是直接依赖,A依赖的jar包对于B来说就是传递依赖
分模块开发之创建父工程
创建的父工程如下
在pom添加以下信息
<!-- 属性 -->
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<hibernate.version>5.0.7.Final</hibernate.version>
<struts.version>2.3.24</struts.version>
</properties>
<!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 依赖管理 -->
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- 导入 struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
</dependency>
<!-- servlet jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 设置编译版本为1.7 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- maven内置 的tomcat6插件 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<!-- 可以灵活配置工程路径 -->
<path>/ssh</path>
<!-- 可以灵活配置端口号 -->
<port>8080</port>
</configuration>
</plugin>
</plugins>
</build>
发布到本地仓库
模块开发之创建dao子模块
在ssh-parent项目上右击 ,创建时选择 Maven Module
- 填写子模块名称ssh-dao
- 把属于dao的代码拷贝到 该模块中
- 完成后发布到本地仓库中
模块开发之创建service子模块
- 创建方式如上
模块开发之创建action子模块
- 选择war的打包方式
- 拷贝属于action的代码和配置文件
- 修改web.xml 添加spring监听
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
- 添加页面
私服 nexus
- 下载安装包,安装私服
- 启动服务
- 启动失败的解决办法
- 登录 nexus 默认用户名admin 密码admin123
仓库类型
- virtual 虚拟仓库
- proxy 代理仓库
- hosted 宿主仓库 本地仓库
- group 组
上传dao到私服
- 需要在客户端即部署dao工程的电脑上配置mavenmaven环境,并修改 settings.xml 文件,配置连接私服的用户和密码 。此用户名和密码用于私服校验,因为私服需要知道上传都 的账号和密码 是否和私服中的账号和密码 一致
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
- 配置项目pom.xml
- 配置私服仓库的地址,本公司的自己的jar包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为release则上传到私服的release仓库,如果版本为snapshot则上传到私服的snapshot仓库
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
注意:pom.xml这里 和 settings.xml 配置 对应!
- 执行deploy命令发布到私服
下载dao
- 修改setting.xml
<profile>
<!--profile的id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库id,repositories可以配置多个仓库,保证id不重复-->
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载releases构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>
- 删除本地仓库dao
- uodate service工程,出现以下信息说明成功