文章目录
相关配置
setting配置文件
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!--本地仓库配置-->
<localRepository>D:/dev/repository</localRepository>
<!--阿里镜像配置-->
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>nexus-aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
</settings>
pom.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>artifactId</artifactId>
<groupId>groupId</groupId>
<version>4.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<artifactId>artifactId</artifactId>
<description>
web服务入口
</description>
<dependencies>
<!-- spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 表示依赖不会传递 -->
<exclusions>
<exclusion>
<!--排除(不导入)包中的jar不导入的配置-->
<groupId></groupId>
<artifactId></artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>mediaSystem</warName>
</configuration>
</plugin>
</plugins>
<finalName>mediaSystem</finalName>
</build>
</project>
Maven坐标
- groupId :the unique identifier of the organization or group that created the project
GroupID 是项目组织唯一的标识符,一般是main目录里java的目录结构,没有硬性要求一定要和JAVA的包的结构一致。 - artifactId :unique base name of the primary artifact being generated by this project
ArtifactID是项目的唯一的标识符,规范要求对应项目的名称,就是项目根目录的名称。
命名规则
- GroupId一般分为多个段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的GroupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,ArtifactId是tomcat。再比如我自己建项目,可以写com.leon.
- ArtifactId 是项目名称,比如:apache的tomcat项目,再比如我自己的项目testProj。
Scope的值及意义
test
、compile
、runtime
、provided
、system
、import
。其中import主要是在dependencyManagement
中使用,平常用的不多,所以主要看其它五种;
- test:仅测试的时候使用,打包编译时,不会使用该包。
- compile:一般默认是compile,编译的时候使用,整个项目周期都会用。打包的时候会将其打包。
- runtime:相比compile,只是不参与编译阶段。
- provided:编译和运行都有参与,但是不参与打包,一般servlet的包都是这个值。
- system: 使用较少,需配合systemPath使用,和provided唯一的区别是从本地获取依赖。
常用功能
将本地jar包安装到本地maven仓库
mvn install:install-file -DgroupId=tk.mybatis -DartifactId=mapper-spring-boot-starter -Dversion=2.1.2 -Dpackaging=jar -Dfile=mapper-spring-boot-starter-2.1.2.jar
解读
mvn install:install-file
固定命令-DgroupId=
后面的值即是引入依赖的groupId
标签的值-DartifactId=
后面的值即是引入依赖的artifactId
的值-Dversion=
是引入依赖的version
的值-Dpackaging=jar
是引入的安装以jar包形式-Dfile=
本地jar的路径,如果执行命令在jar包所在目录,直接写jar包名称即可
IDEA中如何查找jar包之间的依赖关系并忽略依赖的某个包
在idea中的右侧栏的maven工具,点击图中圈中的show Dependencies,
在下面的界面Ctrl + f 搜索要查找的jar包,然后根据图中的关联线即可查找到最终的依赖
根据类名查找依赖的jar包
双击shift键搜索类名
解决依赖冲突
一般有两种解决方案
- 使用exclusions标签排除
<dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-core</artifactId>
<version>2.8.0-release</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
- 直接在pom文件引入冲突依赖的最新依赖,覆盖掉其他版本
打包时跳过test测试
-
方法一:通过执行如下命令
mvn install -DskipTests或者
mvn install -Dmaven.test.skip=true -
方法二:Idea 设置maven打包跳过test目录
如下图:build,Execution,Deployment>build tools >maven >runner ---->VM Options
修改打包名称的配置
<modelVersion>4.0.0</modelVersion>
<artifactId>cat-home</artifactId>
<name>cat-home</name>
<!--打包格式-->
<packaging>war</packaging>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<!--打包包名-->
<warName>${project.artifactId}</warName>
<!--如果没有web.xml文件也不报错-->
<!--<failOnMissingWebXml>false</failOnMissingWebXml>-->
<webResources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<targetPath>WEB-INF/classes</targetPath>
</resource>
<resource>
<directory>src/main/webapp</directory>
<filtering>false</filtering>
<includes>
<include>WEB-INF/web.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>