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
当配置了 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 文件。
不指定 repackage 打的 jar 包会出现找不到 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 依赖关系查看
可查看依赖结构树,有冲突的会标红,可以通过排除jar包方式解决冲突,或者在properties统一指定冲突jar包的版本。
复制代码
8. maven 重用目录结构
1)多 module 的常用目录结构,以坚果为例,每个 module 是一个独立的项目,项目内按包做功能划分。
2)分布式常用目录结构,以常见的商城为例,商品模块
api ---- 移动端的接口
page ---- 后台管理页面 (web 层)
sdk ---- 微服务的对外调用接口
biz ---- 业务层 (server 层)
结构无定论,适应项目就好。
喜欢就 转发+关注吧
评论