Maven开发

一.分模块开发

1.创建maven模块

2.书写模块代码

3.通过maven指令安装到本地仓库(install指令)

二.可选依赖与排除依赖

1.可选依赖指对外隐藏当前所依赖的资源——不透明

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.10.RELEASE</version>
      <optional>false</optional>
    </dependency>

2.排除依赖指主动断开依赖的资源,被排出的资源无需指定版本——不需要``

<dependency>
      <groupId>com.yulong</groupId>
      <artifactId>Meaven_01</artifactId>
      <version>1.0-SNAPSHOT</version>
      <exclusions>
        <exclusion>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

三.聚合与继承

1.聚合工程开发

1.创建maven模块打包为pom

<packaging>pom</packaging>

2.设置当前工程子模块名称

<modules>
  <module>../Meven_01</module>
</modules>

2.继承

概念:继承描述的是两个工程间的关系,与Java中的继承相似,子工程可以继承父工程的的所有配置信息,常见与依赖关系的继承
作用:1.简化配置 2.减少版本冲突

1.创建maven模块打包为pom(建议父工程)

<packaging>pom</packaging>

2.在父工程的pom文件中配置依赖关系(子工程将沿用父工程中的依赖关系)

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        ...
</dependencies>

这些依赖项子工程可以直接使用

3.配置子工程中可选的依赖项

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

这些依赖项子工程可以直接使用或者自定义版本

4.在子工程中配置当前工程所继承的父工程

<parent>
    <groupId>com.itheima</groupId>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-RELEASE</version>
    <relativePath>../maven_01_parent/pom.xml</relativePath>
  </parent>

5.在子工程配置父工程中的可选依赖坐标

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>

      <scope>test</scope>
    </dependency>

四.属性

1.属性配置与使用

1.定义属性

<properties>
        <spring.version>5.2.10.RELEASE</spring.version>
        <junit.version>4.12</junit.version>
        <mybatis-spring.version>1.3.0</mybatis-spring.version>
        <!--<jdbc.url>jdbc:mysql://127.0.0.1:3306/ssm_db</jdbc.url>-->
    </properties>

2.配置文件中引用属性

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=${jdbc.url}
jdbc.username=root
jdbc.password=root

3.开启资源文件目录加载的过滤器

<build>
        <resources>
            <!--设置资源目录,并设置能够解析${}-->
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
</build>

4.配置maven打包war时忽略web.xml检查

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
</plugins>

五.多环境配置与应用

1.多环境开发

1.定义多环境

<profiles>
        <!--开发环境-->
        <profile>
            <id>env_dep</id>
            <properties>
                <jdbc.url>jdbc:mysql://127.1.1.1:3306/ssm_db</jdbc.url>
            </properties>
            <!--设定是否为默认启动环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!--生产环境-->
        <profile>
            <id>env_pro</id>
            <properties>
                <jdbc.url>jdbc:mysql://127.2.2.2:3306/ssm_db</jdbc.url>
            </properties>
        </profile>
        <!--测试环境-->
        <profile>
            <id>env_test</id>
            <properties>
                <jdbc.url>jdbc:mysql://127.3.3.3:3306/ssm_db</jdbc.url>
            </properties>
        </profile>
    </profiles>

2.使用多环境

mvn 指令 -p 环境定义id
ps:mvn intall -p evn_test

2.跳过测试

指令:mvn 指令 -D skiptest
细粒度控制跳过测试

<plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skipTests>false</skipTests>
                    <!--排除掉不参与测试的内容-->
                    <excludes>
                        <exclude>**/BookServiceTest.java</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>

六.私服资源上传与访问

1.下载解压latest-win64.zip

下载地址:https://help.sonatype.com/en/download.html

2.使用cmd进入到解压目录下的 nexus-3.30.1-01\bin ,执行如下命令:

nexus.exe /run nexus

3.浏览器访问localhost:8081进行登录

4.登录后创建仓库

5.进入maven的setting.xml添加

	<mirrors>
	 <mirror>
      <id>maven-public</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>
  <servers>
	<!--配置访问私服权限-->
	<server>
      <id>yulong-snapshot</id>
      <username>admin</username>
      <password>admin</password>
    </server>
	<server>
      <id>yulong-release</id>
      <username>admin</username>
      <password>admin</password>
    </server>
  </servers>

6.在idea中配置上传路径

<!--配置当前工程保存在私服中的具体位置-->
    <distributionManagement>


        <repository>
            <id>yulong-release</id>
            <url>http://localhost:8081/repository/yulong-release/</url>
        </repository>
        <snapshotRepository>
            <id>yulong-snapshot</id>
            <url>http://localhost:8081/repository/yulong-snapshot/</url>
        </snapshotRepository>
    </distributionManagement>
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值