Maven项目构建工具
-
POM(项目对象模型):把项目当做一个对象
-
核心内容是依赖管理,主要过程是项目构建,通过各种插件来产出文件
局部setting配置文件就是在maven文件夹下(与repositroy同级),创建一个setting配置文件,到时会先读取到这一个文件.
断言,junit包下的类
//预期是4,如果num不是4就报错
Assert.assertEquals(4,num);
<!--对象模板版本:就是用哪个版本来构建项目对象-->
<modelVersion>4.0.0</modelVersion>
依赖管理
依赖传递
- 注意被引用的项目的scope,设置时不要是test模式,不然不能引用
引用其它maven项目
<!--千万注意这个作用域-->
<scope>test</scope>
<dependency>
<groupId>org.example</groupId>
<artifactId>Maven</artifactId>
<version>1.0-SNAPSHOT</version>
<!--千万注意这个作用域,在下面的依赖范围章有说-->
<!--<scope>test</scope>-->
</dependency>
传递优先级
- 路径优先
- 声明优先:层级相同时,看父级谁优先
- 特殊优先:后面的覆盖前面的
排除依赖:不要你的依赖
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
可选依赖:不给别人用
<version>4.13.2</version>
<optional>true</optional>
依赖范围
<scope>test</scope>
作用范围
- 主程序:main包下
- 测试程序:test包下
- 参与打包:package范围内
-
默认compile全作用范围
-
test只对测试程序有效
-
provider:不参与打包,如servlet-api依赖,如果参与打包,会和tomcat自带的servlet-api依赖冲突
runtime只参与打包:如jdbc的Dervier类,在主程序中从来没有用过,用的都是jdbc的驱动
依赖范围传递性
只有compile和runtime的直接依赖才能传递.以间接依赖的作用范围为结果
生命周期
Maven的生命周期分为三大部分clean,default(核心部分),site(报告期)
//会在target/surefire-reports下生成一个Test.xml的测试报告
mvn test
- install会将项目打包安装到本地仓库去,路径在maven控制台里有个install开头的标明
插件用法
这个就是site期:项目构建过程中使用插件产出文件的过程
阿帕奇·马文·贾瓦多克插件 – 用法 (apache.org)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<!--打包成只有测试程序的-->
<goals>
<goal>test-jar</goal>
</goals>
<!--在什么阶段执行这个插件-->
<phase>test</phase>
</execution>
</executions>
</plugin>
分模块开发
- 本质就是将把每个包分成项目,引用包资源就是引用这个项目的依赖
聚合:项目统一构建
<!--该工程用于构建管理-->
<packaging>pom</packaging>
<!--父项目连同子项目一同构建-->
<modules>
<module>untitled</module>
<module>untitled1</module>
</modules>
继承
父项目规定好依赖的信息,子项目继承父项目,引用依赖时就不用配置如version这种信息了.
父项目配置
<!--依赖管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!--插件管理-->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
</plugin>
子项目配置
子项目会继承父项目的插件配置
<!--配置父级-->
<parent>
<groupId>org.example</groupId>
<artifactId>Maven</artifactId>
<version>1.0-SNAPSHOT</version>
<!--父级的相对路径-->
<relativePath>../pom.xml</relativePath>
</parent>
<!--子项目只用写自己的项目名就行了-->
<artifactId>untitled</artifactId>
<!--不用写版本号了-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
属性
自定义属性
<properties>
<!--自定义junit版本号-->
<junit.version>4.13</junit.version>
</properties>
<!--使用-->
<version>${junit.version}</version>
<!--maven自带属性:项目版本号-->
<version>${version}</version>
<!--maven配置文件中的属性-->
<version>${settings.offline}</version>
读取电脑系统属性:
控制台属性:PS F:\JAVA\Maven> mvn help:system
会在控制台打印出系统变量和环境变量
<!--系统变量-->
<version>${user.home}</version>
<!--环境变量-->
<version>${HOMEPATH}</version>
版本管理
自定义版本号:Release是发布版,Snapshot是快照版
<version>2.0-RELEASE</version>
资源配置
统一管理项目需要用到的各种资源
<!--1.先定义属性-->
<jdbc.url>1234567</jdbc.url>
</properties>
<!--2.配置扫描文件-->
<build>
<resources>
<resource>
<!--${project.basedir}项目基础路径,找到这个项目下所有的符合这个路径的变量-->
<directory>${project.basedir}/src/main/resources</directory>
<!--允许将maven中的值替换资源文件中的值-->
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<!--3.在propites文件中引用变量-->
url=${jdbc.url}
测试
Properties properties = new Properties();
try {
properties.load(Test.class.getClassLoader().getResourceAsStream("jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(properties.getProperty("url"));
}
多环境配置
<!--多环境配置-->
<profiles>
<profile>
<!--项目名-->
<id>test-mvn</id>
<properties>
<jdbc.url>098765</jdbc.url>
</properties>
<!--激活状态-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
测试时携带参数:打包项目为test-mvn
install -P test-mvn
跳过测试
install -D skipTests
这个是在子项目(要跳过测试的项目中)测试的
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<!--跳过全部测试-->
<!--<skipTests>true</skipTests>-->
<!--包含,但是如果在全局被跳过测试了,这里包含也没有用-->
<includes>
<include>**/T*st.java</include>
</includes>
<!--排除-->
<excludes>
<exclude>**/T*st.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
私服
-
nexue的配置文件在etc下的nexus-default.properties,默认端口号是8081,账号密码是admin和admin
-
bin目录下控制台启动: ./nexus /run nexus
创建
- 在nexus的设置>repositroy中创建maven2私服.group是组, hosted是宿主仓库,proxy是代理仓库
- 在内容栏中可以搜索,浏览和更新仓库.一般在浏览中找到自己的私服进行更新.
- 记得把自己的私服加到maven-public组中去
本地连接私服
- 在maven的setting.xml中配置服务和镜像.也就是本地访问私服的权限
<servers>
<!--本地仓库访问私服上的maven-hosted仓库时的用户名和密码-->>
<server>
<id>maven-hosted</id>
<username>admin</username>
<password>admin</password>
</server>
</servers>
<!-- 所有资源都可以从名为maven-public私服组中访问 -->
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<!--只有访问中央仓库时从阿里云-->
<!--<mirrorOf>central</mirrorOf>-->
<!--这个地址从nexue页面中复制-->
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
将项目发布到私服
<!--发布管理-->
<distributionManagement>
<!--1.自动匹配项目发布模式,将项目发布到指定的url去,
2.用id中的名字去本地setting.xml中找对应的服务的用户权限-->
<repository>
<id>maven-hosted</id>
<url>http://localhost:8081/repository/maven-hosted/</url>
</repository>
<snapshotRepository>
<id>maven-hosted</id>
<url>http://localhost:8081/repository/maven-hosted/</url>
</snapshotRepository>
</distributionManagement>
记得版本号要发布成上线版本
<artifactId>untitled</artifactId>
<version>2.0-RELEASE</version>
发布到私服
<!--发布管理-->
<distributionManagement>
<!--1.自动匹配项目发布模式,将项目发布到指定的url去,
2.用id中的名字去本地setting.xml中找对应的服务的用户权限-->
<repository>
<id>maven-hosted</id>
<url>http://localhost:8081/repository/maven-hosted/</url>
</repository>
<snapshotRepository>
<id>maven-hosted</id>
<url>http://localhost:8081/repository/maven-hosted/</url>
</snapshotRepository>
</distributionManagement>
记得版本号要发布成上线版本
<artifactId>untitled</artifactId>
<version>2.0-RELEASE</version>