POM(项目对象模型):把项目当做一个对象
核心内容是依赖管理,主要过程是项目构建,通过各种插件来产出文件
局部setting配置文件就是在maven文件夹下(与repositroy同级),创建一个setting配置文件,到时会先读取到这一个文件.
断言,junit包下的类
//预期是4,如果num不是4就报错
Assert.assertEquals(4,num);
<modelVersion>4.0.0modelVersion>
引用其它maven项目
<scope>testscope>
<dependency>
<groupId>org.examplegroupId>
<artifactId>MavenartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<version>1.0-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>junitgroupId>
<artifactId>junitartifactId>
exclusion>
exclusions>
dependency>
<version>4.13.2version>
<optional>trueoptional>
<scope>testscope>
默认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
这个就是site期:项目构建过程中使用插件产出文件的过程
阿帕奇·马文·贾瓦多克插件 – 用法 (apache.org)
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-javadoc-pluginartifactId>
<version>3.4.1version>
<executions>
<execution>
<goals>
<goal>test-jargoal>
goals>
<phase>testphase>
execution>
executions>
plugin>
<packaging>pompackaging>
<modules>
<module>untitledmodule>
<module>untitled1module>
modules>
父项目规定好依赖的信息,子项目继承父项目,引用依赖时就不用配置如version这种信息了.
父项目配置
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
dependency>
dependencies>
dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-javadoc-pluginartifactId>
<version>3.4.1version>
<executions>
<execution>
<goals>
<goal>test-jargoal>
goals>
<phase>testphase>
execution>
executions>
plugin>
子项目配置
子项目会继承父项目的插件配置
<parent>
<groupId>org.examplegroupId>
<artifactId>MavenartifactId>
<version>1.0-SNAPSHOTversion>
<relativePath>../pom.xmlrelativePath>
parent>
<artifactId>untitledartifactId>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-javadoc-pluginartifactId>
plugin>
<properties>
<junit.version>4.13junit.version>
properties>
<version>${junit.version}version>
<version>${version}version>
<version>${settings.offline}version>
读取电脑系统属性:
控制台属性:PS F:\JAVA\Maven> mvn help:system
会在控制台打印出系统变量和环境变量
<version>${user.home}version>
<version>${HOMEPATH}version>
自定义版本号:Release是发布版,Snapshot是快照版
<version>2.0-RELEASEversion>
统一管理项目需要用到的各种资源
<jdbc.url>1234567jdbc.url>
properties>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resourcesdirectory>
<filtering>truefiltering>
resource>
resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resourcesdirectory>
<filtering>truefiltering>
testResource>
testResources>
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-mvnid>
<properties>
<jdbc.url>098765jdbc.url>
properties>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
profile>
profiles>
测试时携带参数:打包项目为test-mvn
install -P test-mvn
install -D skipTests
这个是在子项目(要跳过测试的项目中)测试的
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.2version>
<configuration>
<includes>
<include>**/T*st.javainclude>
includes>
<excludes>
<exclude>**/T*st.javaexclude>
excludes>
configuration>
plugin>
plugins>
build>
nexue的配置文件在etc下的nexus-default.properties,默认端口号是8081,账号密码是admin和admin
bin目录下控制台启动: ./nexus /run nexus
<servers>
>
<server>
<id>maven-hostedid>
<username>adminusername>
<password>adminpassword>
server>
servers>
<mirror>
<id>nexusid>
<mirrorOf>*mirrorOf>
<url>http://localhost:8081/repository/maven-public/url>
mirror>
mirrors>
将项目发布到私服
<distributionManagement>
<repository>
<id>maven-hostedid>
<url>http://localhost:8081/repository/maven-hosted/url>
repository>
<snapshotRepository>
<id>maven-hostedid>
<url>http://localhost:8081/repository/maven-hosted/url>
snapshotRepository>
distributionManagement>
记得版本号要发布成上线版本
<artifactId>untitledartifactId>
<version>2.0-RELEASEversion>
发布到私服
<distributionManagement>
<repository>
<id>maven-hostedid>
<url>http://localhost:8081/repository/maven-hosted/url>
repository>
<snapshotRepository>
<id>maven-hostedid>
<url>http://localhost:8081/repository/maven-hosted/url>
snapshotRepository>
distributionManagement>
记得版本号要发布成上线版本
<artifactId>untitledartifactId>
<version>2.0-RELEASEversion>