Maven笔记

配置文件范例

   

<?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">

    <groupId>us.codecraft</groupId>

    <artifactId>tiny-spring</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <modelVersion>4.0.0</modelVersion>

    <packaging>jar</packaging>

    <properties>

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

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    </properties>

    <name>tiny-spring</name>

    <description>

        A tiny implementation of Spring for study.

    </description>

    <url>https://github.com/code4craft/xsoup/</url>

    <developers>

        <developer>

            <id>code4craft</id>

            <name>Yihua huang</name>

            <email>code4crafer@gmail.com</email>

        </developer>

    </developers>

   <licenses>

    <license>

        <name>The MIT License</name>

        <distribution>repo</distribution>

    </license>  

  </licenses>

        <dependencies>

            <dependency>

                <groupId>junit</groupId>

                <artifactId>junit</artifactId>

                <version>4.7</version>

                <scope>test</scope>

            </dependency>

            <dependency>

                <groupId>aopalliance</groupId>

                <artifactId>aopalliance</artifactId>

                <version>1.0</version>

            </dependency>

        </dependencies>

   

    <build>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>3.1</version>

                <configuration>

                    <source>1.6</source>

                    <target>1.6</target>

                    <encoding>UTF-8</encoding>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-resources-plugin</artifactId>

                <version>2.6</version>

                <configuration>

                    <encoding>UTF-8</encoding>

                </configuration>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-source-plugin</artifactId>

                <version>2.2.1</version>

                <executions>

                    <execution>

                        <id>attach-sources</id>

                        <goals>

                            <goal>jar</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-javadoc-plugin</artifactId>

                <version>2.9.1</version>

                <configuration>

                    <encoding>UTF-8</encoding>

                </configuration>

                <executions>

                    <execution>

                        <id>attach-javadocs</id>

                        <goals>

                            <goal>jar</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </build>

</project>

   

   

maven坐标

  1. groupId:实际项目(比如SpringFramework),与域名反向一一对应(比如org.sonatype.nexus)
  2. aritfactId:实际项目中的一个maven项目(模块),比如spring-core,spring-context等。推荐使用groupId作为前缀。
  3. version:版本
  4. Packaging:打包方式,默认为jar
  5. classifier:附属构件,比如nexus-indexer-2.0.0.jar(主构件),nexus-indexer-2.0.0-javadoc.jar(附属构件),nexus-indexer-2.0.0-sources.jar(附属构件)

   

依赖<dependency>配置

   

  • groupId、artifactId、version:确定坐标
  • type:默认为jar
  • scope:
    • compile:默认依赖范围,对编译,测试,运行有效
    • test:测试有效,编译和运行时无法使用此依赖
    • provided:编译和测试有效,运行时无效(例如servlet-api.jar)
    • runtime:测试和运行有效,编译无效(例如JDBC驱动实现,编译时只需要JDBC驱动接口)
    • system:与provided范围一致,但需要systemPath元素显示指定jar包路径
    • import:导入依赖范围

    图例:

  • optional:标记依赖是否可选
  • exclusions:用来排除传递性依赖

   

   

传递性依赖

   

   

注:以上表第一行为例说明:

当第一直接依赖范围是compile,第二直接依赖范围是compile,传递性依赖的范围是compile。

当第一直接依赖范围是compile,第二直接依赖范围是test,依赖不会传递。

同理可得表中其他情况。

   

   

依赖调解

A->B->C->X(1.0)

A->D->X(2.0)

短路径优先,使用x(2.0)。

   

A->B->Y(1.0)

A->C->Y(2.0)

路径长度相等,使用先声明的依赖。

   

   

优化依赖

mvn dependency:list // 显示所有已解析的依赖

mvn dependency:tree // 显示项目的依赖树

mvn dependency:analyze // 依赖分析,列出未显示声明的依赖(应该显示声明项目中直接使用到的依赖),未使用的依赖声明等

   

   

   

   

   

   

   

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值