Maven的点滴

一、下载安装配置

  1. 下载,解压:
$: wget http://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.1/binaries/apache-maven-3.6.1-bin.tar.gz

$: tar -zxvf apache-maven-3.6.1-bin.tar.gz -C /opt/
  1. 配置环境变量
$: vim ~/.bashrc

进行如下配置

JAVA_HOME=/opt/jdk1.8.0_221
JRE_HOME=/opt/jdk1.8.0_221/jre
MAVEN_HOME=/tools/apache-maven-3.6.1

PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin

二、指定阿里的镜像

  1. copy:
cp $MAVEN_HOME/conf/settings.xml $HOME/.m2/
  1. 打开并加入:
vim ~/.m2/settings.xml

在<mirrors>中:

<?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">
  <mirrors>
    <mirror>
        <id>ali-public</id>
        <mirrorOf>*</mirrorOf>
        <name>阿里云公共仓库</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>
</settings>

三、项目骨架的生成

命令: mvn archetype:generate

此方式是使用了 archetype 插件的 generate goal 以交互方式(settings.xml中默认 interactiveMode=true)创建 maven 项目,接下来将会进行一系列的交互,最终完成项目骨架的创建。

四、指定项目的JDK版本

1. 第一种方式:

在项目的pom.xml文件中加入以下:

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

命名规则: compiler 插件的 configuration 的 source 属性,此方式也可以进行如下等价形式的配置:

2. 第二种方式

      <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <target>1.8</target>
                    <source>1.8</source>
                </configuration>
            </plugin>
        </plugins>
    </build>

3. 第三种方式:

在settings.xml中,配置 profile 并以某种方式激活:

  <profiles>
    <!-- jdk8的profile,默认激活,但优先级低于其它的配置 -->
    <profile>    
        <id>jdk8</id>     
        <properties>    
            <maven.compiler.source>1.8</maven.compiler.source>    
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>  
        <!-- 激活方式一 -->
        <activation>    
            <activeByDefault>true</activeByDefault>
            <!-- 此处还有其它的激活方式,如:jdk,os,property,file等 -->
        </activation>    
    </profile>

    <!-- jdk11的profile,默认没有激活 -->
    <profile>    
        <id>jdk11</id>     
        <properties>    
            <maven.compiler.source>11</maven.compiler.source>    
            <maven.compiler.target>11</maven.compiler.target>
        </properties>  
    </profile>
  </profiles>

  <activeProfiles>
    <!-- 激活方式二 ,优先级较高,但低于 pom.xml 的property 配置 -->
    <activeProfile>jdk11</activeProfile>
  </activeProfiles>

五、指定Main-Class

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
          <mainClass>cn.johnyu.z1.Test3</mainClass>
           <!-- 以下的都是选配项 -->
          <addClasspath>true</addClasspath>
          <classpathPrefix>lib/</classpathPrefix>
          <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
          <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
        </manifest>
      </archive>
    </configuration>
</plugin>

六、关于maven-dependency-plugin

1. 将所有依赖导出,会将所有的jar包copy到target/dependency/中

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
</plugin>

执行 mvn dependency:copy-dependencies

2. 如何将【插件的goal】配置到 【maven的phase】中

有些插件的goal与phase是绑定的,如maven-jar-plugin:jar 是绑定到了 package上,这样执行 mvn package和执行 mvn jar:jar是等价操作的,但我们也可以将某些插件的 goal 绑定到指定的 phase 上,下面以maven-dependency-plugin为例进行说明:
以下配置是将copy-dependencies 绑定到了 compile 上。

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>cpy-dep</id>
            <phase>compile</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <!-- configure the plugin here -->
            </configuration>
          </execution>
        </executions>
      </plugin>

七、仓库结构

Maven仓库结构

八、排除依赖、阻止传递依赖

  • 排除对Project-E的依赖
<dependency>
      <groupId>sample.ProjectB</groupId>
      <artifactId>Project-B</artifactId>
      <version>1.0-SNAPSHOT</version>
      <exclusions>
        <exclusion>
          <groupId>sample.ProjectE</groupId> <!-- Exclude Project-E from Project-B -->
          <artifactId>Project-E</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  • 这是Project-A中的pom, 如果其它项目X依赖Project-A,那么Project-B 不会被引入到X中。
    <dependency>
      <groupId>sample.ProjectB</groupId>
      <artifactId>Project-B</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
      <optional>true</optional> 
    </dependency>

完整的settings.xml配置如下:

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

  <proxies></proxies>

  <servers></servers>

  <mirrors>
    <mirror>
        <id>ali-public</id>
        <mirrorOf>central</mirrorOf>
        <name>阿里云公共仓库</name>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
    <mirror>
        <id>ali-google</id>
        <mirrorOf>google</mirrorOf>
        <name>阿里云Google仓库</name>
        <url>https://maven.aliyun.com/repository/google</url>
    </mirror>

    <mirror>
        <id>ali-spring</id>
        <mirrorOf>spring</mirrorOf>
        <name>阿里云Spring仓库</name>
        <url>https://maven.aliyun.com/repository/spring</url>
    </mirror>
  </mirrors>

  <profiles>
    <profile>    
    <id>jdk-1.8</id>    
     <activation>    
        <activeByDefault>true</activeByDefault>    
        <jdk>1.8</jdk>    
      </activation>    
    <properties>    
        <maven.compiler.source>1.8</maven.compiler.source>    
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> 
    </properties>    
</profile>
  </profiles>
</settings>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,547评论 4 374
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,787评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,175评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,752评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,169评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,056评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,195评论 2 321
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,980评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,724评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,872评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,344评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,667评论 3 264
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,379评论 3 245
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,202评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,992评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,189评论 2 286
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,987评论 2 279

推荐阅读更多精彩内容