maven(二)

 

1.1     Maven的好处

节省空间 对jar包做了统一管理 依赖管理

一键构建

可跨平台

应用在大型项目可提高开发效率

 

1.2     Maven安装部署配置

 

1.3     Maven的仓库

本地仓库

远程仓库(私服)

中央仓库

 

1.4     添加依赖

从网络上搜索  mavenrespository.com

在本地重建索引,以索引的方式搜索

 

1.5     项目构建

 

1.6     依赖范围

Compile   struts2 框架jar

Provided   jsp-api.jar     重点

Runtime   数据库驱动包

Test   junit.jar

 

1.7     总结

<modelVersion> 

坐标  GAV

<groupId>cn.itcast</groupId>

<artifactId>ssh</artifactId>

<version>0.0.1-SNAPSHOT</version>

Packaging  打包方式 

Jar  war  pom

<dependencies>

 <dependency>

<build>  里面放的是插件

 

<plugins>

 <plugin>

1       整合ssh框架

1.1     依赖传递

只添加了一个struts2-core依赖,发现项目中出现了很多jar,

这种情况 叫 依赖传递

1 调节原则

    第一声明优先原则

      谁在前面谁优先

<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>
View Code

    路径近者优先原则

自己添加jar包优先 自己手动添加的优先

    <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>4.2.4.RELEASE</version>
      </dependency>
View Code

2  排除原则

 把<exclusion>里的排除掉

<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>

 

 

整合struts2

1、  创建maven项目(同上)

2、  跳过骨架(同上)

3、  填写坐标信息(同上)

4、  添加web.xml文件(同上)

5、  修改编译版本(同上)

6、  添加坐标 选择Dependencies标签 点击add

 

 

添加插件

Maven  add  plugin

 

 

如果用tomcat7运行用命令:

 

Tomcat7:run

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!-- 配置常量 -->
    <!-- 字符集 -->
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <!-- 开发模式 -->
    <constant name="struts.devMode" value="true"></constant>
    <!-- 主题 -->
    <constant name="struts.ui.theme" value="simple"></constant>
    <!-- 扩展名 -->
    <constant name="struts.action.extension" value="action"></constant>

    <!-- 通用package -->
    <package name="customer" namespace="/" extends="struts-default">

        <action name="findById" class="cn.itcast.crm.action.CustomerAction"        method="findCustomerById">
            <result name="success">info.jsp</result>
        </action>

    </package>
</struts>
struts.xml
package cn.itcast.crm.action;

import com.opensymphony.xwork2.ActionSupport;

public class CustomerAction extends ActionSupport {
    private Long custId;

    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }
    public String findCustomerById(){
        System.out.println("received custId is "+custId);
        return SUCCESS;
    }
    
}
CustomerAction
<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>cn.itcast</groupId>
  <artifactId>ssh</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
   

    <build>
        <plugins>
            <!-- 设置编译版本为1.7 -->
            <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>

            <!-- 配置Tomcat插件 -->
            <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>
    <dependencies>

        <!-- servlet jsp -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.24</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>
pom.xml
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>第一个maven项目</title>
</head>
<body>
接收到的id是<s:property value="custId"/>
</body>
</html>
info.jsp

 

输入http://127.0.0.1:8080/ssh/findById.action?custId=2

 

 

 

整合ssh

 

   分模块开发

父工程来管理   聚合

创建父工程

在ssh项目new  maven project

父工程选pom   子工程选jar     web工程选war

 <!-- 属性 -->
    <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>
pom.xml

4、发布到本地仓库

 

 

创建dao子模块

 

填写子模块名称ssh-dao

完成后发布到本地仓库中

 

 

 创建service子模块  依赖dao包

 

1、创建方式如上:

2、把属于service的代码拷贝到该工程中

 

3、发布到本地仓库中

 

 

碰到问题

需要在pom.xml中添加依赖

 

 

以工程方式依赖

关闭工程 已jar包依赖

 

 

 

  创建Action子模块

 

1、选择war的打包方式

<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>
web.xml

 私服 nexus

 

启动(不建议可能启动不了)

 

 

 conf/nexus.properties显示网页访问地址 相关  访问地址是  localhost:8081/nexus

 

 

 

 

登录nexus

用户名/密码  admin/admin123

 

Virtual   虚拟仓库(不起任何作用)

Proxy  代理仓库

Hosted  宿主仓库  本地仓库

Group 组

需求:

把dao放到私服上,然后service从私服上下载

 

 

需求 :将ssh_dao的这个工程打成jar包,并放入到私服上去.

1.1     上传dao

第一步: 需要在客户端即部署dao工程的电脑上配置 maven环境,并修改 settings.xml 文件,配置连接私服的用户和密码 。

 

此用户名和密码用于私服校验,因为私服需要知道上传都 的账号和密码 是否和私服中的账号和密码 一致。

访问release给一个账户密码

id对应 releases

 

 

 

 

第二步: 配置项目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

 

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

 

第三步:执行deploy命令发布到私服

 

 

显示成功发布

打开发布的地方snapshots 显示dao项目

1.1     下载dao

第一步 修改settings.xml  (maven /conf目录下)

添加到settings.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>
setting.xml

激活

<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>

 

 

查看效果

删除本地仓库下的ssh-dao文件

console打开页面 maven控制页面

 

删除后 update project  ssh-service

显示从私服下载ssh-dao 到本地仓库   本地仓库恢复ssh-dao文件

 

posted on 2019-02-18 22:43  打酱油的地方  阅读(143)  评论(0编辑  收藏  举报

导航