02-maven

1.1、Maven 的好处 
节省磁盘空间 
可以一键构建 
可以跨平台 
应用在大型项目时可以提高开发效率


1.2、安装配置 maven 
  注意:3.3+版本需要 jdkj.7+以上的支持 

1.3、 三种仓库 
  本地仓库 
  远程仓库(私服) 
  中央仓库 

1.4、常见的命令 
  Compile 
  Test 
  Package 
  Install 
  Deploy 
  Clean

1.5、坐标的书写规范 
  groupId 公司或组织域名的倒序 
  artifactId 项目名或模块名 
  version 版本号 

1.6、如何添加坐标 
  1、在本地仓库中搜索 
  2、互联网上搜,推荐网址 http://www.mvnrepository.com/

7. 依赖范围 
  Compile 
  Test 
  Runtime 
  Provided

二、 maven 构建 SSM 工程
2.1、需求
2.2、准备数据库
2.3、创建一个maven工程
修改编译版本,在pom.xml文件中添加

2.4、知识点准备
2.4.1、传递依赖
先添加 spring-beans的核心依赖的坐标 
会发现出现除了 spring-beans 以外的其他 jar。因为我们的项目依赖 spring-beans.jar,而spring-beans.jar 会依赖spring-core.jar 等等,所以 spring-core.jar 这些 jar 包也出现在了我们的 maven 工程中,这种现象我们称为依赖传递。

2.4.2、依赖冲突的解决

<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
  </dependencies>
依赖冲突


两个jar包依赖了spring-core。但是版本不同。
而我们希望 spring-core-5.0.2 加入工程。这就造成了依赖冲突。解决依赖冲突有以下原则:

2.4.2.1、依赖调解原则 
  maven 自动按照下边的原则调解: 
1、第一声明者优先原则 
  在 pom 文件定义依赖,先声明的依赖为准。
2、路径近者优先原则 
  例如:还是上述情况如果直接把 spring-core 的依赖直接写到 pom 文件中,那么项目就不会再使用其他依赖传递来的 spring-beans,因为自己直接在 pom 中定义 spring-beans要比其他依赖传递过来的路径要近。

2.4.2.2、排除依赖
  上边的问题也可以通过排除依赖方法辅助依赖调解,如下: 
  比如在依赖 spring-beans 的设置中添加排除依赖,排除 spring-core, 
  下边的配置表示:依赖 spring-beans,但排除 spring-beans 所依赖的 spring-core。

<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.8.RELEASE</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.0.3.RELEASE</version>
    </dependency>
  </dependencies>
排除依赖

2.4.2.3、锁定版本
面对众多的依赖,有一种方法不用考虑依赖路径、声明优化等因素可以采用直接锁定版本的方法确定依赖构件的版本,版本锁定后则不考虑依赖的声明顺序或依赖的路径,以锁定的版本的为准添加到工程中,此方法在企业开发中常用。 

2.5、定义pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>com.itheima</groupId>
  <artifactId>mavenday02_ssm</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <!--
    直接依赖:A项目中导入了B包,我们可以说A直接依赖于B。
    传递依赖:A项目中导入了B包,B包直接依赖于C,最终A项目中也可以使用C。那么我们就可以说A传递依赖于C。

    解决jar包冲突的三个原则:
    第一声明优先原则。
        哪个jar包的坐标在靠上的位置,哪个就是第一声明的包,最终进入项目的就是哪个包的依赖包。
    路径近者优先原则。
        直接依赖路径比传递依赖路径近,哪个路径近,进入到项目中的就是哪个的依赖包。
    直接排除法。
        使用exclusion标签直接来排除某个包下的依赖包。
  -->
  <!-- 统一管理jar包版本 -->
  <properties>
    <spring.version>5.0.2.RELEASE</spring.version>
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
    <shiro.version>1.2.3</shiro.version>
    <mysql.version>5.1.6</mysql.version>
    <mybatis.version>3.4.5</mybatis.version>
    <spring.security.version>5.0.1.RELEASE</spring.security.version>
  </properties>

  <!-- 锁定jar包版本 
      其他的项目依赖于此项目,那么我们项目中的依赖会变成对方项目中的传递依赖。直接依赖优先于传递依赖。
      如果对方项目中有导入spring的一套jar包。优先使用其依赖的jar包,而覆盖此处的jar包。
  -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</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-webmvc</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-test</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <!-- 项目依赖jar包 -->
  <dependencies>
    <!-- spring -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</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-context-support</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-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</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-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <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>
    <!-- log end -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</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>
    <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-taglibs</artifactId>
      <version>${spring.security.version}</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.9</version>
    </dependency>
  </dependencies>
  <!-- 添加tomcat7插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
      </plugin>
    </plugins>
  </build>
</project>
pom.xml


spring和mybatis的整合:

<!--dao层配置开始-->
    <!--配置一个数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///maven"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <!--创建一个生产SqlSeesion对象的工厂对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--引入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--给pojo对象起别名【此处注解开发可以省略】-->
        <property name="typeAliasesPackage" value="com.itheima.domain"/>
    </bean>

    <!--我们对所有dao接口包下的接口进行扫描,使用SqlSeesion对象给其创建代理对象,并且放入到容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.dao"/>
    </bean>
<!--dao层配置结束-->
整合mybatis


 <!--service配置开始-->
  <!--组件扫描-->
  <context:component-scan base-package="com.itheima.service"/>
  <!--配置事务管理器-->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入连接池-->
    <property name="dataSource" ref="dataSource"></property>
  </bean>
  <!--开启事务注解的支持-->
  <tx:annotation-driven/>
  <!--service配置结束-->
service配置事务、注解支持

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  
  <!--配置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>
  
  <!--配置前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!--配置中文过滤器-->
  <filter>
    <filter-name>characterEncodingFilter</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>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  

</web-app>
web.xml





三、分模块构建工程
基于上边的三个工程分析 
继承:
  创建一个 parent 工程将所需的依赖都配置在 pom 中 
聚合:
  聚合多个模块运行。

3.1、需求
需求描述
将 SSM 工程拆分为多个模块开发: 
  ssm_dao 
  ssm_service 
  ssm_web 
maven工程拆分聚合思想

父子工程的作用及关系

理解继承和聚合
通常继承和聚合同时使用。 
何为继承? 
  继承是为了消除重复,如果将 dao、service、web 分开创建独立的工程则每个工程的 pom.xml文件中的内容存在重复,比如:设置编译版本、锁定 spring的版本的等,可以将这些重复的配置提取出来在父工程的 pom.xml 中定义。
何为聚合? 
  项目开发通常是分组分模块开发,每个模块开发完成要运行整个工程需要将每个模块聚合在一起运行,比如:dao、service、web 三个工程最终会打一个独立的war 运行。

传递依赖包丢失情况:

3.2、实现
3.2.1、创建父工程
  1、选择骨架创建父工程
  2、填写坐标
  3、确认使用的是本地仓库
  4、注意代码的路径(默认)
  5、设置项目的打包方式  pom

3.2.1.2、定义pom.xml
  在父工程的 pom.xml 中抽取一些重复的配置的,比如:锁定 jar 包的版本、设置编译版本等。 

<?xml version="1.0" encoding="UTF-8"?>
<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>com.itheima</groupId>
  <artifactId>mavenday02_parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>mavenday02_dao</module>
    <module>mavenday02_service</module>
    <module>mavenday02_web</module>
  </modules>
  <packaging>pom</packaging>

  <!-- 统一管理jar包版本 -->
  <properties>
    <spring.version>5.0.2.RELEASE</spring.version>
    <slf4j.version>1.6.6</slf4j.version>
    <log4j.version>1.2.12</log4j.version>
    <shiro.version>1.2.3</shiro.version>
    <mysql.version>5.1.6</mysql.version>
    <mybatis.version>3.4.5</mybatis.version>
    <spring.security.version>5.0.1.RELEASE</spring.security.version>
  </properties>

  <!-- 锁定jar包版本 -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</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-webmvc</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-test</artifactId>
        <version>${spring.version}</version>
      </dependency>
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <!-- 项目依赖jar包 -->
  <dependencies>
    <!-- spring -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</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-context-support</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-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</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-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql.version}</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <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>
    <!-- log end -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis.version}</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.0</version>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</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>
    <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-taglibs</artifactId>
      <version>${spring.security.version}</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.9</version>
    </dependency>
  </dependencies>
  <!-- 添加tomcat7插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
      </plugin>
      
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <target>1.8</target>
          <source>1.8</source>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
</project>
mavenday02_parent  pom.xml

3.2.1.3、将父工程发布至仓库
  父工程创建完成执行 install 将父工程发布到仓库方便子工程继承

3.2.2、ssm_dao子模块
3.2.2.1、创建dao子模块
  1、在父工程上右击创建 maven 模块
  2、选择“跳过骨架选择” 
  3、填写模块名称 
  4、下一步,确定项目的目录 
  5、打包方式是 jar 

3.2.2.2、定义pom.xml
  只添加到层的 pom,mybatis 和 spring的整合相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <!--parent  父工程的坐标-->
  <parent>
    <artifactId>mavenday02_parent</artifactId>
    <groupId>com.itheima</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <!--项目名-->
  <artifactId>mavenday02_dao</artifactId>


</project>
dao pom.xml

3.2.2.3、dao代码

3.2.2.4、配置文件
  将 applicationContext.xml拆分出一个applicationContext-dao.xml,此文件中只配置 dao 相关 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <!--dao配置开始-->
  <!--配置一个数据源-->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql:///maven"></property>
    <property name="user" value="root"></property>
    <property name="password" value="root"></property>
  </bean>
  <!--配置一个SqlSessionFactory-->
  <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--引入数据源-->
    <property name="dataSource" ref="dataSource"></property>
    <!--给pojo对象起别名【此处注解开发可省略】-->
    <property name="typeAliasesPackage" value="com.itheima.dao"></property>
  </bean>
  
  <!--对所有dao接口包下的接口进行扫描,使用SqlSeesion对象给其创建代理对象,并且放入到容器中-->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.itheima.dao"></property>
  </bean>
  <!--dao配置结束-->

  
</beans>
dao  applicationContext-dao.xml配置

3.2.2.5、单元测试

3.2.2.6、把dao模块install到本地仓库

3.2.3、ssm_service子模块
3.2.3.1、创建service子模块

3.2.3.2、定义pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <parent>
    <artifactId>mavenday02_parent</artifactId>
    <groupId>com.itheima</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>mavenday02_service</artifactId>

  <dependencies>
    <!--此处的坐标指向本地的源码。此时service就可以使用dao的代码了-->
    <dependency>
      <groupId>com.itheima</groupId>
      <artifactId>mavenday02_dao</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>


</project>
service pom.xml

3.2.3.3、service接口

3.2.3.4、配置文件
  创建 applicationContext-service.xml,此文件中定义的service。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  
  <!--引入dao层配置文件-->
  <import resource="classpath:applicationContext-dao.xml"/>
  
  <!--service配置开始-->
  <!--组件扫描-->
  <context:component-scan base-package="com.itheima.service"/>
  <!--配置事务管理器-->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入连接池-->
    <property name="dataSource" ref="dataSource"></property>
  </bean>
  <!--开启事务注解的支持-->
  <tx:annotation-driven/>
  <!--service配置结束-->
  

  
</beans>
applicationContext-service.xml

3.2.3.5、依赖范围对传递依赖的影响

3.2.4、ssm_web子模块
3.2.4.1、创建web子模块
1、选择骨架创建web 子模块    webapp
2、确认使用自己的本地仓库 
3、填写模块名称 
4、创建 java 和 resources 文件夹,转成source root 

3.2.4.2、定义pom.xml
ssm_web 模块的 pom.xml 文件中需要继承父模块,ssm_web 依赖 ssm_service 模块,和springmvc 的依赖。

<?xml version="1.0" encoding="UTF-8"?>

<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">
  <parent>
    <artifactId>mavenday02_parent</artifactId>
    <groupId>com.itheima</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>mavenday02_web</artifactId>
  <packaging>war</packaging>
  
  <dependencies>
    <!--依赖service-->
    <dependency>
      <groupId>com.itheima</groupId>
      <artifactId>mavenday02_service</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

</project>
service pom.xml

3.2.4.3、controller
3.2.4.4、配置文件
项目结构:


3.2.5、运行调试
方法 1:在ssm_web 工程的 pom.xml 中配置 tomcat 插件运行 。
运行 ssm_web 工程它会从本地仓库下载依赖的 jar 包,所以当 ssm_web 依赖的 jar 包内容修改了必须及时发布到本地仓库,比如:ssm_web 依赖的 ssm_service 修改了,需要及时将ssm_service 发布到本地仓库。
否则会出现如下错误:

[ERROR] Failed to execute goal on project mavenday02_web: Could not resolve dependencies for project com.itheima:mavenday02_web:war:1.0-SNAPSHOT: Could not find artifact com.itheima:mavenday02_service:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
错误

方法 2:在父工程的 pom.xml 中配置 tomcat插件运行,自动聚合并执行。
推荐方法2,如果子工程都在本地,采用方法2则不需要子工程修改就立即发布到本地仓库,父工程会自动聚合并使用最新代码执行。
注意:
  如果子工程和父工程中都配置了tomcat 插件,运行的端口和路径以子工程为准。 
项目打包信息:

F:\Development\java\jdk1.8.0_144\bin\java -Dmaven.multiModuleProjectDirectory=F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent -DarchetypeCatalog=internal -Dmaven.home=F:\Maven\apache-maven-3.5.2 -Dclassworlds.conf=F:\Maven\apache-maven-3.5.2\bin\m2.conf "-javaagent:F:\Intellij\JetBrains\IntelliJ IDEA 2017.3.4\lib\idea_rt.jar=58435:F:\Intellij\JetBrains\IntelliJ IDEA 2017.3.4\bin" -Dfile.encoding=UTF-8 -classpath F:\Maven\apache-maven-3.5.2\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2017.3.4 -s F:\Maven\apache-maven-3.5.2\conf\settings.xml install
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.itheima:mavenday02_service:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ com.itheima:mavenday02_service:[unknown-version], F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_service\pom.xml, line 15, column 15
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] mavenday02_parent
[INFO] mavenday02_dao
[INFO] mavenday02_service
[INFO] mavenday02_web
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building mavenday02_parent 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mavenday02_parent ---
[INFO] Installing F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\pom.xml to F:\Maven\maven_repository\com\itheima\mavenday02_parent\1.0-SNAPSHOT\mavenday02_parent-1.0-SNAPSHOT.pom
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building mavenday02_dao 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenday02_dao ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavenday02_dao ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_dao\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenday02_dao ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_dao\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavenday02_dao ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenday02_dao ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mavenday02_dao ---
[INFO] Building jar: F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_dao\target\mavenday02_dao-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mavenday02_dao ---
[INFO] Installing F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_dao\target\mavenday02_dao-1.0-SNAPSHOT.jar to F:\Maven\maven_repository\com\itheima\mavenday02_dao\1.0-SNAPSHOT\mavenday02_dao-1.0-SNAPSHOT.jar
[INFO] Installing F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_dao\pom.xml to F:\Maven\maven_repository\com\itheima\mavenday02_dao\1.0-SNAPSHOT\mavenday02_dao-1.0-SNAPSHOT.pom
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building mavenday02_service 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenday02_service ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
打包信息

3.3、分模块构建项目-依赖整合
每个模块都需要 spring 或者 junit 的 jar,况且最终 package 打完包最后生成的项目中的jar 就是各个模块依赖的整合,所以我们可以把项目中所需的依赖都可以放到父工程中,模块中只留模块和模块之间的依赖。

四、maven私服[了解]
4.1、需求
  正式开发,不同的项目组开发不同的工程。 
  ssm_dao工程开发完毕,发布到私服
  ssm_service 从私服下载 dao 

F:\SOFTWARE\nexus\nexus-2.12.0-01\conf\nexus.properties

#
# Sonatype Nexus (TM) Open Source Version
# Copyright (c) 2008-present Sonatype, Inc.
# All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
#
# This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
# which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
#
# Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
# of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
# Eclipse Foundation. All other trademarks are the property of their respective owners.
#

# Sonatype Nexus
# ==============
# This is the most basic configuration of Nexus.

# Jetty section
application-port=8081           # nexus 的访问端口配置 
application-host=0.0.0.0        # nexus 主机监听配置(不用修改) 
nexus-webapp=${bundleBasedir}/nexus        # nexus 工程目录 
nexus-webapp-context-path=/nexus        # nexus 的 web 访问路径 

# Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus     # nexus 仓库目录 
runtime=${bundleBasedir}/nexus/WEB-INF                 # nexus 运行程序目录 
nexus.properties配置文件

4.2、需求
公司在自己的局域网内搭建自己的远程仓库服务器,称为私服,私服服务器即是公司内部的 maven 远程仓库,每个员工的电脑上安装 maven 软件并且连接私服服务器,员工将自己开发的项目打成 jar 并发布到私服服务器,其它项目组从私服服务器下载所依赖的构件(jar)。
私服还充当一个代理服务器,当私服上没有 jar 包会从互联网中央仓库自动下载。如下图。

4.3、搭建私服
4.3.1、下载nexus
Nexus 是 Maven 仓库管理器,通过 nexus 可以搭建 maven 仓库,同时 nexus 还提供强大的仓库管理功能,构件搜索功能等。

4.3.2、安装nexus
  cmd进入 bin目录,执行 nexus.bat install 

4.3.3、卸载nexus
  cmd进入 nexus 的 bin目录,执行:nexus.bat uninstall 

4.3.4、启动nexus
方法 1: 
  cmd 进入 bin目录,执行 nexus.bat start 
方法 2: 
  直接启动 nexus 服务
访问: 
  http://localhost:8081/nexus/
使用 Nexus 内置账户
  admin  /  admin123 登陆: 

4.3.5、仓库类型
查看 nexus 的仓库: 

附注:
  snapshot 版本代表不稳定、尚处于开发中的版本,即快照版本
  release 版本代表功能趋于稳定、当前更新停止,可以用于发行的版本
1、hosted,宿主仓库
  部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 两部分,Releases公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库 。
2、proxy,代理仓库
  用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私服自动去中央仓库下载 jar 包或者插件。 
3、group,仓库组
  用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓库组。 
4、virtual(虚拟):兼容 Maven1 版本的 jar 或者插件
nexus 仓库默认在 sonatype-work 目录中: 


central:代理仓库,代理中央仓库 

apache-snapshots:代理仓库 
存储 snapshots 构件,代理地址 https://repository.apache.org/snapshots/ 
central-m1:virtual 类型仓库,兼容 Maven1 版本的 jar 或者插件 
releases:本地仓库,存储 releases构件。 
snapshots:本地仓库,存储 snapshots构件。 
thirdparty:第三方仓库 
public:仓库组

4.4、将项目发布到私服
4.4.1、需求
  企业中多个团队协作开发通常会将一些公用的组件、开发模块等发布到私服供其它团队或模块开发人员使用。 
  本例子假设多团队分别开发 ssm_dao、ssm_service、ssm_web,某个团队开发完在ssm_dao会将 ssm_dao 发布到私服供 ssm_service团队使用,本例子会将 ssm_dao 工程打成jar 包发布到私服。

4.4.2、配置
第一步:
需要在客户端即部署 ssm_dao工程的电脑上配置 maven环境,并修改 settings.xml 文件,配置连接私服的用户和密码 。 
此用户名和密码用于私服校验,因为私服需要知道上传的账号和密码是否和私服中的账号和密码一致。
maven安装目录设置连接私服。

<server>
    <id>releases</id> 
    <username>admin</username> 
    <password>admin123</password> 
</server>
<server>
    <id>snapshots</id> 
    <username>admin</username> 
    <password>admin123</password> 
</server>
maven连接私服配置


第二步: 配置项目 pom.xml 
配置私服仓库的地址,本公司的自己的 jar 包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为 release 则上传到私服的 release 仓库,如果版本为snapshot 则上传到私服的 snapshot仓库。
distribution  (n. 分布、分配)

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <!--parent  父工程的坐标-->
  <parent>
    <artifactId>mavenday02_parent</artifactId>
    <groupId>com.itheima</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <!--项目名-->
  <artifactId>mavenday02_dao</artifactId>

  <!--配置上传的私服-->
  <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>

</project>
dao pom.xml配置上传的私服

注意:pom.xml 这里<id> 和 settings.xml 配置 <id> 对应。

deploy信息---
F:\Development\java\jdk1.8.0_144\bin\java -Dmaven.multiModuleProjectDirectory=F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_dao -DarchetypeCatalog=internal -Dmaven.home=F:\Maven\apache-maven-3.5.2 -Dclassworlds.conf=F:\Maven\apache-maven-3.5.2\bin\m2.conf "-javaagent:F:\Intellij\JetBrains\IntelliJ IDEA 2017.3.4\lib\idea_rt.jar=61454:F:\Intellij\JetBrains\IntelliJ IDEA 2017.3.4\bin" -Dfile.encoding=UTF-8 -classpath F:\Maven\apache-maven-3.5.2\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2017.3.4 deploy
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.itheima:mavenday02_dao:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ com.itheima:mavenday02_parent:1.0-SNAPSHOT, F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\pom.xml, line 224, column 15
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building mavenday02_dao 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenday02_dao ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavenday02_dao ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenday02_dao ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_dao\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavenday02_dao ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenday02_dao ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mavenday02_dao ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mavenday02_dao ---
[INFO] Installing F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_dao\target\mavenday02_dao-1.0-SNAPSHOT.jar to F:\Maven\maven_repository\com\itheima\mavenday02_dao\1.0-SNAPSHOT\mavenday02_dao-1.0-SNAPSHOT.jar
[INFO] Installing F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_dao\pom.xml to F:\Maven\maven_repository\com\itheima\mavenday02_dao\1.0-SNAPSHOT\mavenday02_dao-1.0-SNAPSHOT.pom
[INFO] 
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ mavenday02_dao ---
Downloading from snapshots: http://localhost:8081/nexus/content/repositories/snapshots/com/itheima/mavenday02_dao/1.0-SNAPSHOT/maven-metadata.xml
Uploading to snapshots: http://localhost:8081/nexus/content/repositories/snapshots/com/itheima/mavenday02_dao/1.0-SNAPSHOT/mavenday02_dao-1.0-20190115.122553-1.jar
Uploaded to snapshots: http://localhost:8081/nexus/content/repositories/snapshots/com/itheima/mavenday02_dao/1.0-SNAPSHOT/mavenday02_dao-1.0-20190115.122553-1.jar (4.1 kB at 18 kB/s)
Uploading to snapshots: http://localhost:8081/nexus/content/repositories/snapshots/com/itheima/mavenday02_dao/1.0-SNAPSHOT/mavenday02_dao-1.0-20190115.122553-1.pom
Uploaded to snapshots: http://localhost:8081/nexus/content/repositories/snapshots/com/itheima/mavenday02_dao/1.0-SNAPSHOT/mavenday02_dao-1.0-20190115.122553-1.pom (898 B at 6.5 kB/s)
Downloading from snapshots: http://localhost:8081/nexus/content/repositories/snapshots/com/itheima/mavenday02_dao/maven-metadata.xml
Uploading to snapshots: http://localhost:8081/nexus/content/repositories/snapshots/com/itheima/mavenday02_dao/1.0-SNAPSHOT/maven-metadata.xml
Uploaded to snapshots: http://localhost:8081/nexus/content/repositories/snapshots/com/itheima/mavenday02_dao/1.0-SNAPSHOT/maven-metadata.xml (769 B at 5.0 kB/s)
Uploading to snapshots: http://localhost:8081/nexus/content/repositories/snapshots/com/itheima/mavenday02_dao/maven-metadata.xml
Uploaded to snapshots: http://localhost:8081/nexus/content/repositories/snapshots/com/itheima/mavenday02_dao/maven-metadata.xml (283 B at 2.5 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.102 s
[INFO] Finished at: 2019-01-15T20:25:54+08:00
[INFO] Final Memory: 16M/196M
[INFO] ------------------------------------------------------------------------
deploy日志信息

4.4.3、测试
将项目 dao 工程打成 jar 包发布到私服: 
1、首先启动 nexus 
2、对 ssm_dao工程执行 deploy 命令   --->也可以使用install命令。在父工程上使用install命令,子工程也会被打成相应的包。
根据本项目pom.xml中version定义决定发布到哪个仓库,如果version定义为snapshot,执行deploy后查看nexus的snapshot仓库,如果 version定义为release则项目将发布到nexus的 release仓库,本项目将发布到 snapshot仓库: 

也可以通过http方式查看:


4.5、从私服下载jar包
4.5.1、需求

没有配置 nexus 之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器,有了私服本地项目首先去本地仓库找 jar,如果没有找到则连接私服从私服下载 jar 包,如果私服没有 jar 包私服同时作为代理服务器从中央仓库下载 jar 包,这样做的好处是一方面由私服对公司项目的依赖 jar 包统一管理,一方面提高下载速度,项目连接私服下载 jar 包的速度要比项目连接中央仓库的速度快的多。 

4.5.2、管理仓库组
nexus中包括很多仓库,hosted中存放的是企业自己发布的jar包及第三方公司的jar包,proxy 中存放的是中央仓库的 jar,为了方便从私服下载 jar 包可以将多个仓库组成一个仓库组,每个工程需要连接私服的仓库组下载 jar 包。 
打开 nexus 配置仓库组,如下图:

上图中仓库组包括了本地仓库、代理仓库等。 

删除掉本地仓库的jar包:mavenday02_dao
然后运行项目。报错。

此时本地仓库没有mavenday02_dao的jar包,默认从中央仓库(也没有)下载。
所以,上传和下载jar包的在maven的config目录下的settings.xml配置是不一样的。
错误信息:

F:\Development\java\jdk1.8.0_144\bin\java -Dmaven.multiModuleProjectDirectory=F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_web -DarchetypeCatalog=internal -Dmaven.home=F:\Maven\apache-maven-3.5.2 -Dclassworlds.conf=F:\Maven\apache-maven-3.5.2\bin\m2.conf "-javaagent:F:\Intellij\JetBrains\IntelliJ IDEA 2017.3.4\lib\idea_rt.jar=61902:F:\Intellij\JetBrains\IntelliJ IDEA 2017.3.4\bin" -Dfile.encoding=UTF-8 -classpath F:\Maven\apache-maven-3.5.2\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2017.3.4 --offline -s F:\Maven\apache-maven-3.5.2\conf\settings.xml org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:run
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.itheima:mavenday02_web:war:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ com.itheima:mavenday02_parent:1.0-SNAPSHOT, F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\pom.xml, line 224, column 15
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building mavenday02_web 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ mavenday02_web >>>
[WARNING] The POM for com.itheima:mavenday02_dao:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.538 s
[INFO] Finished at: 2019-01-15T20:44:02+08:00
[INFO] Final Memory: 7M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project mavenday02_web: Could not resolve dependencies for project com.itheima:mavenday02_web:war:1.0-SNAPSHOT: Could not find artifact com.itheima:mavenday02_dao:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
错误信息

4.5.3、在setting.xml中配置仓库--->下载jar的设置
在maven的 setting.xml 中配置私服的仓库,由于 setting.xml 中没有 repositories 的配置标签需要使用 profile 定义仓库。

<!-- 下载jar包配置 -->
    <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>
maven settins.xml配置

然后,使用 profile 定义仓库需要激活才可生效。

<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>
配置

4.5.4、测试从私服下载jar包
测试 1:局域网环境或本地网络即可 
在 ssm_service 工程中添加以上配置后,添加 ssm_dao 工程的依赖,删除本地仓库中 ssm_dao工程,同时在 eclipse 中关闭 ssm_dao工程。 
项目先从本地仓库找 ssm_dao,找不到从私服找,由于之前执行 deploy 将 ssm_dao 部署到私服中,所以成功从私服下载 ssm_dao 并在本地仓库保存一份。
包含从私服中下载 jar 的日志信息。

F:\Development\java\jdk1.8.0_144\bin\java -Dmaven.multiModuleProjectDirectory=F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_web -DarchetypeCatalog=internal -Dmaven.home=F:\Maven\apache-maven-3.5.2 -Dclassworlds.conf=F:\Maven\apache-maven-3.5.2\bin\m2.conf "-javaagent:F:\Intellij\JetBrains\IntelliJ IDEA 2017.3.4\lib\idea_rt.jar=62679:F:\Intellij\JetBrains\IntelliJ IDEA 2017.3.4\bin" -Dfile.encoding=UTF-8 -classpath F:\Maven\apache-maven-3.5.2\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2017.3.4 tomcat7:run
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.itheima:mavenday02_web:war:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ com.itheima:mavenday02_parent:1.0-SNAPSHOT, F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\pom.xml, line 224, column 15
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building mavenday02_web 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) > process-classes @ mavenday02_web >>>
Downloading from nexus: http://localhost:8081/nexus/content/groups/public/com/itheima/mavenday02_dao/1.0-SNAPSHOT/maven-metadata.xml
Downloaded from nexus: http://localhost:8081/nexus/content/groups/public/com/itheima/mavenday02_dao/1.0-SNAPSHOT/maven-metadata.xml (769 B at 100 B/s)
Downloading from nexus: http://localhost:8081/nexus/content/groups/public/com/itheima/mavenday02_dao/1.0-SNAPSHOT/mavenday02_dao-1.0-20190115.122553-1.pom
Downloaded from nexus: http://localhost:8081/nexus/content/groups/public/com/itheima/mavenday02_dao/1.0-SNAPSHOT/mavenday02_dao-1.0-20190115.122553-1.pom (898 B at 28 kB/s)
Downloading from nexus: http://localhost:8081/nexus/content/groups/public/com/itheima/mavenday02_dao/1.0-SNAPSHOT/mavenday02_dao-1.0-20190115.122553-1.jar
Downloaded from nexus: http://localhost:8081/nexus/content/groups/public/com/itheima/mavenday02_dao/1.0-SNAPSHOT/mavenday02_dao-1.0-20190115.122553-1.jar (4.1 kB at 113 kB/s)
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenday02_web ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavenday02_web ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) < process-classes @ mavenday02_web <<<
[INFO] 
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ mavenday02_web ---
[INFO] Running war on http://localhost:8080/mavenday02_web
[INFO] Using existing Tomcat server configuration at F:\CODE_SPACE\SouceCode\ITHEIMA\foursection\mavenday02_parent\mavenday02_web\target\tomcat
[INFO] create webapp with contextPath: /mavenday02_web
日志

如果此时删除私服中的 ssm_dao,执行 update project 之后是否正常? 
如果将本地仓库的 ssm_dao 和私服的 ssm_dao 全部删除是否正常?
测试 2:需要互联网环境 
在项目的 pom.xml 添加一个依赖,此依赖在本地仓库和私服都不存在,maven 会先从本地仓库找,本地仓库没有再从私服找,私服没有再去中央仓库下载,jar 包下载成功在私服、本地仓库分别存储一份。

五、把第三方jar包放入本地仓库
5.1、导入本地仓库
----进入jar包所在目录运行

mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile=fastjson-1.1.37.jar -Dpackaging=jar

----打开cmd直接运行

mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=E:\HEIMA\第三阶段\maven高级\资料\安装第三方jar包\fastjson-1.1.37.jar

5.2、导入私服
需要在 maven 软件的核心配置文件 settings.xml 中配置第三方仓库的 server 信息 。

<server>
      <id>releases</id> 
      <username>admin</username> 
      <password>admin123</password> 
</server>
<server>
      <id>snapshots</id> 
      <username>admin</username> 
      <password>admin123</password> 
</server>
    
<server>    
    <id>thirdparty</id>    
     <username>admin</username> 
     <password>admin123</password>    
</server>
settins.xml

配置完成,才能执行以下命令:
----进入jar包所在目录运行

mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty

----打开cmd直接运行

mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=E:\HEIMA\第三阶段\maven高级\资料\安装第三方jar包\fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty



5.3、参数说明
DgroupId 和 DartifactId 构成了该 jar 包在 pom.xml 的坐标,项目就是依靠这两个属性定位。
自己起名字也行。 
Dfile 表示需要上传的 jar 包的绝对路径。 
Durl 私服上仓库的位置,打开 nexus——>repositories菜单,可以看到该路径。 
DrepositoryId 服务器的表示 id,在 nexus 的configuration 可以看到。 
Dversion 表示版本信息,

关于 jar 包准确的版本: 
包的名字上一般会带版本号,如果没有那可以解压该包,会发现一个叫 MANIFEST.MF 的文件,
这个文件就有描述该包的版本信息。 
比如 Specification-Version: 2.2 可以知道该包的版本了。 
上传成功后,在 nexus 界面点击3rd party 仓库可以看到这包。

posted @ 2019-01-14 22:24  payn  阅读(293)  评论(0)    收藏  举报