飘飞的绿叶系列 工欲善其事必先利其器--maven

​飘飞的绿叶系列

工欲善其事必先利其器--maven

 

1. maven下载及教程

maven 官网地址  https://maven.apache.org/index.html#

mavne 3.6.3 下载地址  https://archive.apache.org/dist/maven/maven-3/3.6.3/binaries/

maven 菜鸟教程 https://www.runoob.com/maven/maven-tutorial.html

maven repository 仓库地址:https://mvnrepository.com/

阿里maven仓库:https://maven.aliyun.com/mvn/view

其他教程自行搜索

配置mvn环境变量,配置完环境变量即可在终端,如idea Terminal 中运行mvn命令。注意idea Terminal 中的命令是环境变量中的maven路径,区别于idea Setting中的maven路径。通常会将两个设置为同一个路径,注意修改idea setting中的manven配置。

 

2. idea  maven 配置

 

    菜单 file > New Projects Settings > Preferences for New Projects  可以配置新项目的默认配置。

 

maven默认读取配置文件路径 ~/.m2/settings.xml。

通常会修改长仓库的地址。打开注释代码,自行修改路径。

<localRepository>/path/to/local/repo</localRepository> 

 

配置阿里云仓库,  官网:https://maven.aliyun.com/mvn/guide

所有仓库*

<mirror>  <id>aliyunmaven</id>  <mirrorOf>*</mirrorOf>  <name>阿里云公共仓库</name>  <url>https://maven.aliyun.com/repository/public</url></mirror>

central仓库

<mirrors>    <mirror>      <id>alimaven</id>      <name>aliyun maven</name>      <url>https://maven.aliyun.com/repository/central</url>      <mirrorOf>central</mirrorOf>            </mirror>  </mirrors>

 

3. maven  私服配置

开发中公司通常会搭建私服,私服配置 添加服务账号 server其中id为服务器名称,需与miros或repository中id一致。

  • id 是要认证的服务器名称,可以配置多个。它是用来标记服务器的,要唯一。

  • username 和 password 是用户名和密码。

<servers>    <server>      <id>serverName</id>      <username>userName</username>      <password>pwd</password>    </server>  </servers>

添加miros或者repositoriry

<mirror>    <id>serverName</id>    <mirrorOf>*</mirrorOf>    <name>serverName</name>    <url>http://localhost:8080/repository/internal</url></mirror>
<repositories>    <repository>      <id>serverName</id>      <name>serverName</name>      <releases>        <enabled>true</enabled>      </releases>      <snapshots>        <enabled>false</enabled>      </snapshots>      <!-- 仓库地址 -->      <url>http://localhost:8080/repository/internal</url>    </repository></repositories>

idea项目中pom中配置使用的仓库

<distributionManagement>    <repository>    <id>serverName</id>    <url>http://localhost:8080/repository/internal</url>    </repository></distributionManagement>

 

4. profile  环境

<profiles>  <profile>    <id>dev</id>    <repository>    <!-- ... -->    </repository>  </profile></profiles>
  • settting.xml中默认开启的profile

<activeProfiles>    <activeProfile>czb</activeProfile>   </activeProfiles>
  • 通过命令指定profile

    mvn clean package -Pdev

当配置了profileidea中显示如下。可以配置多个,不同的项目可进行切换,一般一个公司一个maven私服。

 

5. spring cloud 开发常用的依赖

pom依赖 version 通常会写到properties中, 

注意 <type>pom</type> <scope>import</scope>

<!-- springboot依赖     --><dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-dependencies</artifactId>  <version>2.2.5.RELEASE</version>  <type>pom</type>  <scope>import</scope></dependency><!-- springCloud依赖     --><dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-dependencies</artifactId>  <version>Hoxton.SR3</version>  <type>pom</type>  <scope>import</scope></dependency><!-- alibaba依赖     --><dependency>  <groupId>com.alibaba.cloud</groupId>  <artifactId>spring-cloud-alibaba-dependencies</artifactId>  <version>2.2.0.RELEASE</version>  <type>pom</type>  <scope>import</scope></dependency>

spring-cloud-dependencies的版本选择,在mvn仓库中搜索并查看某一版本在Managed Dependencies 列表Version显示的基本就是spring-boot的最低版本。

例如 查看Hoxton.SR3 的spring-boot版本信息

查看spring-cloud-starter的版本信息

可以看到 Hoxton.SR3 的spring-boot最低版本为2.2.5.RELEASE 最高版本为2.4.5

spring-cloud-alibaba-dependencies的依赖 在阿里mavne仓库中搜索 地址:https://maven.aliyun.com/mvn/search,

 

6. 常用的Maven插件

maven-compiler-plugin编译插件 可以不用添加,配置properties属性即可

<properties>  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  <java.version>1.8</java.version>  <maven.compiler.source>1.8</maven.compiler.source>  <maven.compiler.target>1.8</maven.compiler.target></properties>

直接指定编译插件

<!--编译版本管理-->

<plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-compiler-plugin</artifactId>  <version>3.5.1</version>  <configuration>    <source>1.8</source>    <target>1.8</target>    <encoding>UTF-8</encoding>  </configuration></plugin>spring-boot-maven-plugin  注意 repackage:创建一个自动可执行的jar或war文件。不指定会出现找不到main方法的问题。<plugin>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId>    <version>${spring-boot-dependencies.version}</version>    <configuration>        <!-- 没有该配置,devtools 不生效 -->        <fork>true</fork>    </configuration>    <executions>        <execution>            <goals>                <!-- 重新打包 -->                <goal>repackage</goal>            </goals>        </execution>    </executions></plugin><!-- 跳过单元测试 --><plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-surefire-plugin</artifactId>    <version>2.20.1</version>    <configuration>        <skipTests>true</skipTests>    </configuration></plugin><!-- docker --><plugin>    <groupId>com.spotify</groupId>    <artifactId>docker-maven-plugin</artifactId>    <version>${docker-maven-plugin.version}</version>    <configuration>        <imageName>${project.artifactId}:${project.version}</imageName>        <dockerDirectory>${project.basedir}</dockerDirectory>        <skipDockerBuild>true</skipDockerBuild>        <resources>            <resource>                <targetPath>/</targetPath>                <directory>${project.build.directory}</directory>                <include>${project.build.finalName}.jar</include>            </resource>        </resources>    </configuration></plugin>

源码打包插件,不常用,可将源码打包一并发布到maven仓库,其他使用者可以下载源码。

<!--源码发布插件--><plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-source-plugin</artifactId>  <version>3.2.1</version>  <executions>    <execution>      <goals>        <goal>jar</goal>      </goals>      <id>attach-sources</id>    </execution>  </executions></plugin>

打包效果:

 

7. idea中pom依赖关系查看

可查看依赖结构树

 

    以上总结了maven在项目中的一些基本用法,基本能够满足项目中的使用

 

喜欢就扫码关注下吧。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值