Idea操作Maven 高级篇:

  • 通过下面学习大抵曾经理解了: Maven根底篇
    什么是Maven——为啥要用——根底的构建:
  • Maven 能够说是Java开发必不可少的工具了:
    ok!, 接下来深刻理解 Maven的高级篇!

分模块构建工程[利用]

Maven 继承[依赖] 和聚合

何为聚合?

  • 我的项目开发通常是分组分模块开发
  • 每个模块开发实现一个独自的性能。
  • 如果: 要运行整个工程须要将每个模块聚合在 一起运行;
  • 比方: dao、service、web 三个工程最终会打一个独立的 war 运行.

聚合: 能够在 dao service web 模块之上在建设一个 父模块: 通过父模块来聚合工程, 运行只须要编译 打包父工程即可!

何为继承?

  • 和Java 很相似: 子类 继承extends 父类,便具备了父类的个性;
  • Maven继承:
    是为了打消反复,如果将 dao、service、web 离开创立独立的工程则每个工程的;
    pom.xml 会有很多雷同的依赖Jar包~
    能够将这些反复的配置,提取进去在父工程的 pom.xml 中定义对立治理申明公共 Jar
  • 父模块的打包形式必须为pom,否则无奈构建我的项目。 子模块中通过配置来表明其继承与哪一个父模块:
  • 通常继承和聚合同时应用。

Maven聚合 Demo案例:

数据库:

父工程 bbs-parent

  • 创立一个一般的Maven工程删掉 src 父工程是不写代码的, 只是做了一个聚合治理的性能...
  • 增加所须要的Maven模块:
  • dao 数据层
  • entity 实体层
  • service 业务逻辑层
  • web 展现层,以上都是一般的Maven工程即可,而这里须要指定为 web Maven模板的工程...(可参考上一篇文~)

父工程 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>org.example</groupId>    <artifactId>bbs-parent</artifactId>    <version>1.0-SNAPSHOT</version>    <!-- 父工程的打包形式! -->    <!-- 咱们见过打包形式为jar和war的,然而聚合模块的打包形式必须为pom,否则无奈实现构建。 -->    <packaging>pom</packaging>    <!-- 父工程集成的子工程! -->    <modules>        <module>bbs-dao</module>        <module>bbs-service</module>        <module>bbs-entity</module>        <module>bbs-web</module>        <!--<module>..聚合的子工程..</module>-->    </modules>    <!-- 通用配置.. -->    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <maven.compiler.source>1.7</maven.compiler.source>        <maven.compiler.target>1.7</maven.compiler.target>        <spring.version>4.2.4.RELEASE</spring.version>    </properties>    <!-- SSM工程所须要的Jar,申明在父工程中,子工程也能够应用.... -->    <dependencies>        <!-- spring-命名空间:apl -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>${spring.version}</version>        </dependency>        <!-- spring-beans: Bean的Apl -->        <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-expression</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</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-web</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.aspectj</groupId>            <artifactId>aspectjrt</artifactId>            <version>1.7.4</version>        </dependency>        <dependency>            <groupId>org.aspectj</groupId>            <artifactId>aspectjweaver</artifactId>            <version>1.7.4</version>        </dependency>        <!-- 日志依赖 -->        <dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>1.2.14</version>        </dependency>                <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>            <version>1.6.1</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>1.6.1</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-nop</artifactId>            <version>1.6.1</version>        </dependency>        <!-- JSON依赖 -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>fastjson</artifactId>            <version>1.2.31</version>        </dependency>    </dependencies></project>

留神:

  • <spring.version>4.2.4.RELEASE</spring.version> Spring版本 4.0以上是JDK 1.8应用的!!
  • 3.几 的版本是JDK1.7的!!哭死,这个谬误找半天!!

bbs-entity 模块

实体层:定义我的项目所须要的pojo...

  • 这就特地简略了.. 定义包, 申明一个类: 对着数据库敲实体属性即可!
  • 须要留神的是:子工程的Pom.xml

Entity 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>bbs-parent</artifactId>        <groupId>org.example</groupId>        <version>1.0-SNAPSHOT</version>    </parent>        <modelVersion>4.0.0</modelVersion>    <!-- 子工程:我的项目名 -->    <artifactId>bbs-entity</artifactId></project>

bbs-dao

长久层:与数据库打交道负责 读写操作..

  • 代码这里就不一一解读了,失常的SSM 查问全表操作!须要理解SSM能够点击这里!
  • SortMapper.Java 接口
  • SortMapper.xml Sql映射文件
  • mybatis-config.xml mybatis外围配置文件
  • applicationContext-mybatis.xml Spring外围配置文件

Dao pom.xml

子工程不仅能够,继承父工程的公共依赖 当然也能够定义一些本人特有的依赖

Dao模块 依赖 Entity模块 所有要引入其它子工程的依赖

<?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>bbs-parent</artifactId>        <groupId>org.example</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>     <!-- 子工程:我的项目名 -->    <artifactId>bbs-dao</artifactId>    <!-- 子工程定义特有依赖! -->    <dependencies>        <!-- mybatis:集成SpringJar -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis-spring</artifactId>            <version>1.1.1</version>        </dependency>        <!-- c3p0数据源依赖 -->        <dependency>            <groupId>c3p0</groupId>            <artifactId>c3p0</artifactId>            <version>0.9.1.2</version>            <type>jar</type>        </dependency>        <!-- mybatis依赖 -->        <dependency>            <groupId>org.mybatis</groupId>            <artifactId>mybatis</artifactId>            <version>3.2.8</version>        </dependency>        <!-- mysql驱动 -->        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.34</version>        </dependency>        <!-- 增加实体模块坐标 -->        <dependency>            <groupId>org.example</groupId>            <version>1.0-SNAPSHOT</version>            <artifactId>bbs-entity</artifactId>        </dependency>    </dependencies></project>

Spring外围配置文件:applicationContext-mybatis.xml
C3P0 数据源定义:啥的兴许有敌人,数据源不一样就不必独自去找了...

<?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"       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">    <!-- Spring:扫描包下注解 -->    <context:component-scan base-package="com.wsm.mapper"></context:component-scan>    <!-- 数据源配置 C3P0 -->    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">        <!-- 连贯驱动 库 用户 明码.. -->        <property name="driverClass" value="com.mysql.jdbc.Driver"/>        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bbs"></property>  <!-- 指定数据库别忘了! -->        <property name="user" value="root"></property>        <property name="password" value="ok"></property>    </bean>    <!-- 连贯工厂 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"></property>        <property name="configLocation" value="classpath:mybatis-config.xml"></property>    </bean>    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.wsm.mapper"></property>        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>    </bean></beans>

bbs-servc

业务逻辑层: 给页面申请进行解决做出封装响应...redi缓存等..操作; 本次就简略解决调用Dao办法l

  • 失常的申明接口,实现接口调用 Dao
  • 因为是分模块开发了. Dao —— Service 曾经相当于是两个独自的Maven我的项目, 模块之间通过依赖来进行交互
    Service 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>bbs-parent</artifactId>        <groupId>org.example</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <!-- 子工程:我的项目名 -->    <artifactId>bbs-service</artifactId>    <!-- 引入dao依赖: service引入——dao引入——entity-->    <dependencies>        <dependency>            <groupId>org.example</groupId>            <version>1.0-SNAPSHOT</version>            <artifactId>bbs-dao</artifactId>        </dependency>    </dependencies></project>

bbs-web

视图层:次要组成 Controller控制器 和 JSP页面!负责展现成果!

  • SSM 的惯例代码:
  • Controller控制器 SpringMVC配置! web.xml  jsp页面.... pom.xml

留神:

  • Maven web工程的打包形式是 war包Tomcat只能运行 war包~ 而其它模块是Jar包(默认) Maven的父工程是 pom类型打包!
  • 本这次演示的是SSM工程:Demo下载
    Entity——Dao——Service——Web: 都是通过Maven依赖进行聚合的;
    应用前要先上传至本地仓库install:通过依赖去本地仓库中查找... ~当然能够通过父工程一键上传至本地仓库!~

maven 私服 搭建:

  • 正式开发,不同的项目组开发不同的工程。
    ssm_dao 工程开发结束,公布到私服。 ssm_service 从私服下载 dao 不能在公司,拿着u盘 到处跑找人copy 依赖吧...

公司本人搭建的Maven仓库:

不同的人将本人开发好的模块,上传至私服上 , 当然须要的模块也能够去私服下载

~上一篇曾经解说过私服的概念了...~

搭建私服环境

下载 nexus

  • Nexus 是 Maven 仓库管理器
  • 通过 nexus 能够搭建 maven 仓库,同时 nexus 还提供弱小的仓库治理性能,构件搜寻性能等。
  • 下载 Nexus, 下载地址:http://www.sonatype.org/nexus/archived/

装置 nexus

解压 nexus-2.12.0-01-bundle.zip,本教程将它解压在D盘,进入 bin 目录:

执行 nexus.bat install 如果不行就管理员运行

卸载nexus

cmd 进入 nexus 的 bin 目录,执行:nexus.bat uninstall 查看 window 服务列表 nexus 已被删除。

启动 nexus

办法一

cmd 进入 bin 目录,执行 nexus.bat start

办法二

间接启动 nexus 服务

拜访: http://localhost:8081/nexus/ 默认 端口8081

查看 nexus 的配置文件 conf/nexus.properties

罕用配置:

# 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 内置账户admin 明码admin123

仓库类型

仓库分类

  • Public Repositories:
    该仓库组将Policy(策略)为Release的仓库聚合并通过一个地址对外提供服务; 下载的上传的这里都会保留·
  • 3rd party:
    用来部署无奈从公共仓库获取的第三方公布版本的jar包
  • Apache Snapshots
    用来代理Apache阿帕奇 Maven仓库的快照版本jar包 一种代理仓库,近程下载阿帕奇的依赖Jar
  • Central
    该仓库代理Maven地方仓库,其Policy(策略)为Release,因而只会下载和缓存地方仓库中的公布版本jar包 和Apache 一样
  • Codehaus Snapshots
    用来代理CodeHaus Maven仓库的快照版本jar包
  • Release
    用户部署组织外部的公布版本的jar包
  • Snapshots
    用来部署组织外部的快照(测试)版本的jar包

nexus 的仓库有 4 种类型

  • group:仓库组
    用来合并多个 hosted/proxy 仓库,通常咱们配置本人的 maven 连贯仓 库组。
  • hosted:宿主
    宿主仓库,部署本人的 jar 到这个类型的仓库 包含 releases 和 snapshot 两局部:
    Releases 公司外部公布版本仓库、
    Snapshots 公司外部测试版本仓库
  • proxy:代理
    代理仓库 用于代理近程的公共仓库
    maven 地方仓库,用户连贯私服,私服主动去地方仓库下载 jar 包或者插件。
  • virtual:虚构
    兼容 Maven1 版本的 jar 或者插件

仓库Format(格局)

有两种maven1和maven2,上面的仓库分类只介绍maven2

仓库Policy(策略)介绍

Release:公布版本
Snapshots:快照版本

nexus 仓库默认在 sonatype-work 目录中:

central:代理仓库,代理地方仓库

将我的项目公布到私服

ok,理解了私服 和 搭建好了私服, 就是将代码模块:上传至私服上并下载应用了!!

  • 企业中多个团队合作开发通常会将一些专用的组件、开发模块等公布到私服供其它团队或模块开发人员应用。
  • 本例子假如多团队别离开发 ssm_dao、ssm_service、ssm_web
    某个团队开发完在 ssm_dao 会将 ssm_dao 公布到私服供 ssm_service 团队应用


上传/下载:当然是先上在下拉...

上传

第一步: 配置连贯私服的用户和明码;

  • 须要在客户端即:要上传模块工程的电脑上配置 maven环境 指定要上传的仓库地位!批改 settings.xml

上传设施Maven的Setting.xml
Maven装置目录: apache-maven-3.5.4-bin\apache-maven-3.5.4\conf 目录下 <servers> 标签下配置 </servers>

<!-- releases 连贯公布版本我的项目仓库 --><server>     <id>releases</id>     <username>admin</username>     <password>admin123</password> </server> <!-- snapshots 连贯测试版本我的项目仓库 --><server>     <id>snapshots</id>    <username>admin</username>     <password>admin123</password> </server><!-- 更多依据状况进行配置... -->

第二步: 要上传模块的 配置我的项目 pom.xml

  • 配置私服仓库的地址,要上传公司私服的宿主仓库
  • 依据工程的版本号决定上传到哪个宿主仓库
    如果版本为 release 则上传到私服的 release 仓库
    如果版本为snapshot 则上传到私服的 snapshot 仓库
    在创立模块时候能够在pom.xml 中查看设置:
    轻易找一个模块发现它的配置版本是:<version>1.0-SNAPSHOT</version> 默认是SNAPSHOT 测试版本!

pom.xml

<distributionManagement>    <!-- 非测试版本上传地位配置 -->    <repository>        <!-- 这里的id 就是本地的Maven:Setting.xml配置的id -->        <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>

因为自己的:上传设施 和 私服设施是同一个电脑, 所有这里就是localhost 理论开发依据公司的 端口更改!
而这个地位就是:

测试

将我的项目 dao 工程打成 jar 包公布到私服:

  • 首先启动 nexus
  • 确保本地仓库存在模块, 当时打包package 和 公布install 的本地仓库对 dao 工程执行Maven deploy上传 命令:

    能够看到控制台。呈现uploding 上传的提醒:且能够在连贯上查看到刚刚上传的模块. 当然本地仓库也是存在的!上传胜利!
  • 依据本我的项目pom.xml中version定义决定公布到哪个仓库<version>1.0-SNAPSHOT</version>
  • 如果version定义为snapshot, 执行 deploy后查看 nexus 的 snapshot仓库
  • 如果 version定义为 release则我的项目将公布到 nexus的 release 仓库,本我的项目将公布到 snapshot 仓库:

下载

要下载设施的Meven setting.xml 中配置仓库

<profiles></profiles> 标签下配置:

<profile>     <!--profile 的 id-->     <id>dev</id>     <repositories>     <repository>             <!--仓库 id,repositories 能够配置多个仓库,保障 id 不反复-->         <id>nexus</id>         <!--仓库地址,即 nexus 仓库组的地址:去public里找-->         <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>

<settings></settings> 节点下配置:开启仓库配置

  <!-- 激活下载仓库配置! -->  <activeProfiles>    <activeProfile>dev</activeProfile>  </activeProfiles>

测试:

为了更好看到成果倡议: 删除本地Dao模块 删除本地仓库的Jar 间接编译Service模块就会开始依据依赖找Dao 模块了

  • 找不到就去私服下载, 私服没有就会去终于仓库 私服怎么会没有刚上传的...
  • 下载之后, 本地仓库就会主动的存储一个, 下次在调用就会间接去本地仓库找了!!
  • 以下能够看的 Downloading 下载的标识!

    欧克, 到这Maven的学习就大抵理解了, 根本满足开发须要了!倡议三联哦~
    新的一年了, 祝各位新年快乐!Happy New Year
    这两天有工夫多更新几章(〃 ̄︶ ̄)人( ̄︶ ̄〃)