Maven总结

介绍

Apache Maven是一个软件项目管理和理解工具。基于项目对象模型(POM)的概念,Maven可以从中心信息段管理项目的构建、报告和文档。

下载

http://maven.apache.org/download.cgi

安装

下载安装包

解压到磁盘

配置环境变量

M2_HOME=C:\Program Files\apache-maven-3.3.9

Path=%M2_HOME%\bin

安装测试

mvn -v

配置本地仓库settings.xml

<localRepository>D:/.m2/repos</localRepository>

<mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

Maven基本命令

mvn clean:清理,将根目录下面的target目录清理掉;
mvn compile:编译,将项目中的.java文件编译成.class文件;
mvn test:测试,执行项目下src/test/java下的命名为Xxx...Test.java文件的单元测试类;
mvn package:打包,将项目打包放置在项目根目录.target下;
mvn install:安装,将项目打包到自己本地环境;
mvn deploy: 部署,将项目部署到我们的私服环境;
mvn package -DskipTest:打包跳过测试;
mvn dependency:tree:查看依赖的树形结构,用于依赖冲突排除;

Scope依赖范围

compile:默认范围,用于编译      
provided:类似于编译,但支持你期待jdk或者容器提供,类似于classpath      
runtime:在执行时需要使用      
test:用于test任务时使用      
system:需要外在提供相应的元素。通过systemPath来取得      
systemPath:仅用于范围为system。提供相应的路径      
optional:当项目自身被依赖时,标注依赖是否传递。用于连续依赖时使用

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope><!-- 指定作用范围 -->
</dependency>

聚合项目

  • 父项目pom包含子项目的模块,如下:
<modules>
    <module>common</module>
    <module>core</module>
    <module>streaming</module>
    <module>kafka</module>
</modules>
  • 定义统一的版本参数,如下:
<properties>
    <junit.version>4.12</junit.version>
    <spark.version>2.2.2</spark.version>
</properties>
  • 统一的依赖版本管理,子模块只需引用即可,如下:
<dependencyManagement>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
        <version>${spark.version}</version>
    </dependency>
</dependencyManagement>
  • 子模块使用,如下:
<dependencies>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
    </dependency>
</dependencies>

其他配置

  • 将java的sdk的版本提高到1.8并且指定maven的编码,如下:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>${character}</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>
  • 打包包含第三方依赖库,如下:
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

手动安装jar包到本地仓库

主要是将maven的坐标设置好确保唯一

mvn install:install-file \
-DgroupId=com.xxx.mc \
-DartifactId=commons-web \
-Dversion=0.0.1-SNAPSHOT \
-Dpackaging=jar \
-Dfile=commons-web-0.0.1-SNAPSHOT.jar

执行命令开始安装到本地仓库 

项目依赖引用即可

<dependency>
    <groupId>com.xxx.mc</groupId>
    <artifactId>commons-web</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值