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>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容