Maven进阶篇详细笔记,源码可见下载链接
大家阅读时可善用目录功能,可以提高大家的阅读效率
下载地址:Maven笔记+项目源码
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>SSMartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.examplegroupId>
<artifactId>Maven_1artifactId>
<version>1.0-SNAPSHOTversion>
dependency>
...
dependencies>
project>
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>Maven_1artifactId>
<version>1.0-SNAPSHOTversion>
project>
注:如果不执行install之指令,在运行原项目的时候就会报错
如何直观的显示直接/间接依赖
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>Maven_1artifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.examplegroupId>
<artifactId>Maven_2artifactId>
<version>1.0-SNAPSHOTversion>
<optional>falseoptional>
dependency>
dependencies>
project>
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>SSMartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.examplegroupId>
<artifactId>Maven_1artifactId>
<version>1.0-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>org.examplegroupId>
<artifactId>Maven_2artifactId>
exclusion>
exclusions>
dependency>
...
dependencies>
project>
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>MavenartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<modules>
<module>../SSMmodule>
<module>../Maven_1module>
<module>../Maven_2module>
modules>
project>
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>MavenartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.2.10.RELEASEversion>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
dependencyManagement>
project>
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>SSMartifactId>
<packaging>warpackaging>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.examplegroupId>
<artifactId>MavenartifactId>
<version>1.0-SNAPSHOTversion>
<relativePath>../Maven/pom.xmlrelativePath>
parent>
<dependencies>
<dependency>
<groupId>org.examplegroupId>
<artifactId>Maven_1artifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
dependencies>
project>
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>MavenartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
dependencies>
<properties>
<spring.version>5.2.9.RELEASEspring.version>
properties>
project>
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>MavenartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<properties>
<jdbc.url>jdbc:mysql:///ssm_db?useSSL=falsejdbc.url>
properties>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resourcesdirectory>
<filtering>truefiltering>
resource>
resources>
build>
project>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=${jdbc.url}
jdbc.username=root
jdbc.password=123456
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>SSMartifactId>
<packaging>warpackaging>
<version>1.0-SNAPSHOTversion>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-war-pluginartifactId>
<version>3.4.0version>
<configuration>
<failOnMissingWebXml>falsefailOnMissingWebXml>
configuration>
plugin>
plugins>
build>
project>
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>MavenartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<profiles>
<profile>
<id>env_depid>
<properties>
<jdbc.url>jdbc:mysql:///ssm_db?useSSL=falsejdbc.url>
properties>
profile>
<profile>
<id>env_proid>
<properties>
<jdbc.url>jdbc:mysql://127.2.2.2:3306/ssm_db?useSSL=falsejdbc.url>
properties>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
profile>
<profile>
<id>env_testid>
<properties>
<jdbc.url>jdbc:mysql://127.3.3.3:3306/ssm_db?useSSL=falsejdbc.url>
properties>
profile>
profiles>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resourcesdirectory>
<filtering>truefiltering>
resource>
resources>
build>
project>
mvn 指令 -p 环境定义id 范例:
mvn install -p pro_env
应用场景
使用方式
2.maven插件(细粒度控制跳过测试)
<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.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>MavenartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resourcesdirectory>
<filtering>truefiltering>
resource>
resources>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.22.1version>
<configuration>
<skipTests>falseskipTests>
<excludes>
<exclude>**/BookServiceTest.javaexclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
mvn 指令 -D skipTests
mvn install -D skipTests
私服是一台独立的服务器,用于解决团队内部的资源共享与资源同步问题
Nexus
nexus.exe /run nexus
http://localhost:8081
<setting>
<servers>
<server>
<id>lonelysnow-releaseid>
<username>adminusername>
<password>123456password>
server>
<server>
<id>lonelysnow-snapshotid>
<username>adminusername>
<password>123456password>
server>
servers>
<mirrors>
<mirror>
<id>maven-publicid>
<mirrorOf>*mirrorOf>
<url>http://localhost:8081/repository/maven-public/url>
mirror>
mirrors>
setting>
<distributionManagement>
<repository>
<id>lonelysnow-releaseid>
<url>http://localhost:8081/repository/lonelysnow-release/url>
repository>
<snapshotRepository>
<id>lonelysnow-snapshotid>
<url>http://localhost:8081/repository/lonelysnow-snapshot/url>
snapshotRepository>
distributionManagement>
mvn deploy