maven学习(一)

maven安装

jdk环境变量
maven环境变量

mvn -v

settings.xml 最好放在 ~/.m2 目录内 防止升级覆盖

设置http代理

有时候公司基于安全认证考虑,要求你是用通过安全认证的代理访问因特网。

  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <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>
    -->
  </proxies>

编写pom

groupId 定义了项目属于哪个组
artifactId 项目在组中的唯一ID
version 当前版本
name 不是必须 建议写上 方便信息交流

maven编译插件

<!-- 添加项目jdk编译插件 -->
<build>
    <plugins>
        <!-- 设置编译版本为1.7 -->
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

生成可执行jar文件

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.huhu.test.helloword.HelloWorld</mainClass>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

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/maven-v4_0_0.xsd">
  <dependencies>
    <dependency> 
      <groupId>net.sf.json-lib</groupId> 
      <artifactId>json-lib</artifactId> 
      <version>2.2.2</version>
      <type>jar</type>
      <scope>compile</scope>
      <optional>false</optional>
      <exclusions>
        <exclusion>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
        </exclusion>
      </exclusions>
      <classifier>jdk15</classifier> 
    </dependency> 
  <dependencies>
</project>

scope

依赖范围,有如下备选值:
compile
编译依赖范围,对于编译、测试、运行三种classpath都有效,也就是在编译、测试、运行的时候都需要该依赖。
test
测试依赖范围。只对于测试classpath有效,只在测试的时候需要该依赖。典型的如junit。
provided
已提供依赖范围。对于编译、测试两种classpath有效,运行时classpath无效。也就是在编译、测试时候都需要该依赖,运行时不需要。典型的如servlet。
runtime
运行时依赖范围。对于测试、运行两种classpath有效,编译classpath无效。在编译时不需要,典型的如jdbc驱动实现。
system
系统依赖范围。与provided类似,唯一的不同是必须明确的指定被依赖的构件在系统中的路径。典型的如本地的jar包,没有导入仓库中,需要从本地引用。例如:

<dependency> 
    <groupId>javax.sql</groupId> 
    <artifactId>jdbc-stdext</artifactId> 
    <version>2.0</version> 
    <scope>system</scope> 
    <systemPath>${java.home}/lib/rt.jar</systemPath> 
</dependency>

import
导入依赖范围。如果需要完全复制一个已经存在依赖配置,可以使用导入依赖范围,将其导入,节省重复的配置。一般被导入的构件类型为pom。例如:

<dependency> 
    <groupId>com.qupeng.test.maven</groupId> 
    <artifactId>maven-parent</artifactId> 
    <version>1.2.3-beta-1</version> 
    <type>pom</type> 
    <scope>import</scope> 
</dependency>

optional
依赖是否可选。要理解optional元素,必须理解传递性依赖。

假如你的Project A的某个依赖D添加了true,当别人通过pom依赖Project A的时候,D不会被传递依赖进来
当你依赖某各工程很庞大或很可能与其他工程的jar包冲突的时候建议加上该选项,可以节省开销,同时减少依赖冲突

exclusions
用来排除传递依赖。例如有的传递性依赖的构件版本不稳定或与其他依赖有冲突,就可以使用exclusions元素来排除传递性依赖。

  <dependencies>
    <dependency> 
      <groupId>net.sf.json-lib</groupId> 
      <artifactId>json-lib</artifactId> 
      <version>2.2.2</version>
      <type>jar</type>
      <scope>compile</scope>
      <optional>false</optional>
      <exclusions>
        <exclusion>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
        </exclusion>
      </exclusions>
      <classifier>jdk15</classifier> 
    </dependency> 
  <dependencies>

Maven依赖配置最佳实践

自动化的依赖引用很容易造成依赖重复,进而造成冲突。为了优化Maven依赖,我们需要了解Maven对重复依赖的调解的两个原则:

路径最近优先
第一声明者优先


常用技巧:

排除依赖
参考上文中对excludions元素的描述
归类依赖
使用Maven属性统一相同项目不同模块的依赖版本

    <properties>
        <springframework.version>2.5.6</springframework.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springframework.version}</version>
        </dependency>
    </dependencies>

优化依赖

可以使用如下命令分析依赖树,并优化依赖树

mvn dependency:list 
mvn dependency:tree 
mvn dependency:analyze 

重点关注声明未使用和使用未声明的依赖,并进行修改

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值