maven私服

maven进阶

分模块开发与设计

maven导入maven-02,需要在pom.xml里面导入maven-02的依赖

<dependency>
    <groupId>org.example</groupId>
    <artifactId>maven_02</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

导入之前maven-02需要install打包完成,这样共有依赖

可选依赖

<dependency>
    <groupId>org.example</groupId>
    <artifactId>maven_02</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--选择对外隐藏02引用的依赖-->
    <optional>true</optional>
</dependency>

排除依赖

<exclusions>
    <exclusion>
        <!--排除依赖,排除02使用的这个依赖,不需要指定版本-->
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
    </exclusion>
</exclusions>

聚合

将多个模块组织成一个整体,同时践行项目构建的过程成为聚合。

聚合工程:通常是一个不具有业务功能的空工程,有且仅有一个pom.xml,使用聚合工程可以将多个工程编组,通过对聚合工程进行构建,实现对所包含的模块进行同步构建【当工程中某个某块发生更新时,必须保障工程中有关联的其他模块同步更新】

创建一个空的maven模块,设置打包类型为pom,并添加管理module

<packaging>pom</packaging>
<modules>
    <module>../../maven_02/maven_02</module>
    <module>../../UserProj</module>
</modules>

继承

子工程可以继承父工程的配置信息,子工程中添加

<parent>
    <artifactId>maven_01_parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <groupId>org.example</groupId>
    <relativePath>../maven_01_parent/pom.xml</relativePath>
</parent>

子工程就可以继承父工程中的依赖

依赖管理

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>

子工程使用的时候不需要提供版本,从父类那边取

属性

   <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-version}</version>
    </dependency>
</dependencies>
<properties>
    <!--自定义spring版本的变量-->
    <spring-version>5.3.7</spring-version>
</properties>

maven控制配置文件内容

在properties标签中添加变量:

<!--设置配置文件的内容-->
<jdbc.url>jdbc:mysql://localhost:3306/4031</jdbc.url>
<jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>

resources下面的配置文件jdbc.properties如下:

jdbc.driver=${jdbc.driver}
jdbc.url=${jdbc.url}

为了让maven能够锁定目录,在build里面添加:

<resources>
    <resource>
        <!--开启资源文件目录加载属性的过滤器,设置内置属性${project.basedir}表示当前工程同级所有工程目录-->
        <directory>${project.basedir}/src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>

配置类替代web.xml导致没有web.xml的时候,编译会报错,在build添加配置

<plugin>
    <!--找到war插件的本地仓库位置-->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <!--忽略没有web.xml配置文件【当文件中使用配置类替代web.xml的时候】-->
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
</plugin>

版本管理

工程版本:

snapshot:快照版本,开发中临时输出的版本

release:发布版本,稳定的时候发布

多环境开发

通过

<profiles>
    <profile>
        <!--在生产环境下的配置-->
        <id>develop</id>
        <properties>
            <!--设置配置文件的内容-->
            <jdbc.url>jdbc:mysql://localhost:3306/4031</jdbc.url>
            <jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
        </properties>
    </profile>
    <profile>
        <!--在测试环境下的配置-->
        <id>test</id>
        <properties>
            <jdbc.url>jdbc:mysql://localhost:3306/4031</jdbc.url>
            <jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
        </properties>
    </profile>
</profiles>

设置默认环境:

<profile>
    <!--在生产环境下的配置-->
    <id>develop</id>
    <properties>
        <!--设置配置文件的内容-->
        <jdbc.url>jdbc:mysql://localhost:3306/4031</jdbc.url>
        <jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
    </properties>
    <activation>
        <!--设定是否为默认环境-->
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>

maven编译的时候可以指定环境,在execute maven goal里面输入:

maven install -P develop

跳过测试

当功能更新中并没有开发完毕的时候,希望打包maven,测试会报错,通过下面的方式来跳过测试

点击skip test,然后再运行就不会报错

通过下面方式可以跳过指定测试

<build>
    <plugins>
        <plugin>
            <!--找到测试的插件所在位-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>
                <!--跳过测试-->
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

等价于上面的跳过测试,实际一般用这种方法,还可以排除一些要测试或者不需要测试的在configuration里面添加:

<configuration>
    <!--跳过测试-->
    <skipTests>false</skipTests>
    <excludes>
        <!--排除在外-->
        <exclude>../maven_02/src/main/java/org/example/Main.java</exclude>
    </excludes>
</configuration>

用指令:

mvn package -D skipTests

私服

团队内部的资源共享和资源同步,下载好的压缩包latest

文件一:nexus-3.30.1-01 输入命令

nexus.exe /run nexus

文件二:sonatype-work登录密码存储在该位置。

私服分类

1、宿主仓库hosted

2、代理仓库proxy

3、仓库组group

资源上传与下载

创建两个仓库:一个发布,一个快照

配置conf下面的setting.xml

1、配置私服服务器

<!-- 私服服务器 -->
 <server>
    <id>liku_release</id>
    <username>admin</username>
    <password>123456</password>
  </server>
 <server>
    <id>liku_snapshot</id>
    <username>admin</username>
    <password>123456</password>
  </server>xml

2、配置私服访问路径,maven仓库组

<!-- 私服的访问路径 -->
	<mirror>
		<id>maven-public</id>
		<mirrorOf>*</mirrorOf>
		<url>http://localhost:8081/repository/maven-public/</url>
	</mirror>

仓库组的镜像可以修改:


配置工程保存到私服的管理位置:

<!--配置当前工程保存在私服的具体位置-->
<distributionManagement>
    <repository>
        <id>liku_release</id>
        <url>http://localhost:8081/repository/liku_release/</url>
    </repository>
    <snapshotRepository>
        <id>liku_snapshot</id>
        <url>http://localhost:8081/repository/liku_snapshot/</url>
    </snapshotRepository>
</distributionManagement>

然后点击maven的插件deploy,就可以了

posted @ 2023-03-03 19:52  Liku007  阅读(92)  评论(0编辑  收藏  举报