关闭

Maven笔记

83人阅读 评论(0) 收藏 举报

配置文件范例

   

<?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 // 依赖分析,列出未显示声明的依赖(应该显示声明项目中直接使用到的依赖),未使用的依赖声明等

   

   

   

   

   

   

   

   

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3466次
    • 积分:209
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论