欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

maven的依赖 博客分类: maven项目实践 maven依赖 

程序员文章站 2024-03-21 21:29:58
...
<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.caiz.maven</groupId>
  <artifactId>hello-first</artifactId>
  <version>SNAPSHOT-0.0.1</version>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

</project>

 

一、maven的pom.xml配置依赖

1、结构

<!--依赖集合-->

 <dependencies> 

<!--依赖-->
    <dependency>

<!--组id-->
      <groupId>junit</groupId>

<!--模块id-->
      <artifactId>junit</artifactId>

<!--版本号-->
      <version>4.10</version>

<!--依赖范围-->
      <scope>test</scope>
    </dependency>
  </dependencies>

2、依赖是通过坐标来搜索的(GAV的方式)

G—— groupId

A——artifactId

V——version

 

3、依赖的查询

jar包查询顺序

本地仓库——>私服仓库(企业)——>*仓库

本机在settings.xml文件配置:

      <localRepository>E:/apache-maven-3.1.0/Repository</localRepository>

代理方式访问私服

     <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>

*仓库查询访问

     1)http://www.mvnrepository.com/

     2)http://www.sonatype.org/nexus/

4、scope代表范围

1)test 测试范围依赖,编译和打包时候无效

2)compile编译时有效,是scope默认值

3)provided 在编译和测试的时候依赖,打成.war的时候不依赖

4)runtime运行时依赖,编译时候不依赖

 

 二、依赖的冲突

1、依赖存在传递性,当依赖冲突时,会先加载配置考前的依赖

a——>b1.0 c——>b1.2 ,e——>a+c,则a依赖b1.0;

 

2、路径最短原则

a——>b——>c1.0,e——>c1.1 ,f——>a+e 则f依赖c1.1

 

 

3、2例中如果f要依赖c1.0 则在依赖e时用,这样可以精确依赖

<exclusions>

    <groupId></groupId>

    <artifactId></artfactId>

</exclusions>

 

4、直接添加依赖,不通过传递依赖加入

 

 三、依赖的聚合问题

 

 

<?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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.cai.maven</groupId>
  <artifactId>hello-second</artifactId>
  <version>SNAPSHOT-0.0.1</version>
<!--打包的方式-->
<pakaging>pom<packaging> 
 
    

<!--模块方案-->
 <modules>
	<module>../user-vo<module>
	<module>../user-service<module>
	<module>../user-dao<module>
 <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.0</modelVersion>

  <groupId>com.caiz.core</groupId>
  <artifactId>user-core</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>user-core</name>
  <url>http://maven.apache.org</url>

<parent>
<groupId>com.caiz.user</groupId>
<artfactId>user-parent</artfactId>
<version>snapshot0.0.1</version>
<ralativePath>../user-parent/pom.xml</relativePaht>
<parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
  
  <junit.version>4.10<junit.version>
</properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    </dependencies>
</project>

 

设定子类不全部继承用dependencyManagement

 

<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.0</modelVersion>

  <groupId>com.caiz.core</groupId>
  <artifactId>user-core</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>user-core</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
  </properties>

<dependencyManageMent>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
    <dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
	</dependency>
  </dependencies>
</dependencyManageMent>
</project>

 

相关标签: maven 依赖