maven
maven理論
- 生命週期體系 (clean compile test package install build deploy)依序執行
- pom體系
- 屬性系統
maven概要
maven Setting
- cong/Setting 檔案下進行設定
- mirror可配置鏡像倉庫
- 設置 maven倉庫所在位置 預設為 user/.m2/Repository
- 配置jdk版本 默認1.5版本 ,透過conf/setting/profile設置版本
- 設定環境參數 maven home
Maven 依赖
- 依賴maven 幫忙管理依賴與統一版本方便下載
- 除了下載要用的依賴以外也下載目標所需的依賴
- maven 工程 -> 核心程序 ->maven 命令->下載至倉庫
- install 會安裝至target
- 衝途處理
Maven 構建
- maven也負責構建的部分
- 使用原材料生產產品(src -> target)
- local測試(idea幫忙構建)→github → CI→maven打包(build jar or war)→kubernetes(Deploy)
Maven 生命週期
- clean
- site
- default (compile test package install build deploy)依序執行
maven 插件和目標
- 目標就是一個功能
- maven 具體執行功能由插件執行
maven 倉庫
local 倉庫
- local 指定repo 放置jar黨的位置
遠程倉庫
nexus倉庫
- 遠程倉庫 (Nexus有無)->(中央倉庫) 下載
中央倉庫
nexus 搭建
搭建
docker pull sonatype/nexus3
docker run -d --name nexus3 --restart=always -p 8081:8081 --mount src=nexus-data,target=/nexus-data sonatype/nexus3
docker exec nexus3 cat /nexus-data/admin.password
创建仓库 创建一个私有仓库的方法: Repository->Repositories 点击右边菜单 Create repository 选择 docker (hosted) Name: 仓库的名称 HTTP: 仓库单独的访问端口(例如:5001) Hosted -> Deployment policy: 请选择 Allow redeploy 否则无法上传 Docker 镜像。 其它的仓库创建方法请各位自己摸索,还可以创建一个 docker (proxy) 类型的仓库链接到 DockerHub 上。再创建一个 docker (group) 类型的仓库把刚才的 hosted 与 proxy 添加在一起。主机在访问的时候默认下载私有仓库中的镜像,如果没有将链接到 DockerHub 中下载并缓存到 Nexus 中
設定
maven setting路徑指定為 nexus repository public路徑
jar包發布
- 构件的发布: 在 Maven 项目的 POM 中,通过环境配置 \
来管理构件的发布
<!--
<downloadUrl>:其他 Maven 项目可以通过该 URL 下载并引用当前 Maven 项目的构件。
<status>:当前 Maven 项目的状态,常见的值包括 none、converted、partner、deployed 和 verified
-->
<distributionManagement>
<downloadUrl>http://jcat.ericsson.org/my-project</downloadUrl>
<status>deployed</status>
</distributionManagement>
- 推送仓库设置(常用):
<!--
<distributionManagement> 中的 <repository> 和 <snapshotRepository> 分别用于配置发布仓库和快照仓库。-->
<distributionManagement>
<repository>
<uniqueVersion>false</uniqueVersion>
<id>corp1</id>
<name>Corporate Repository</name>
<url>scp://repo/maven2</url>
<layout>default</layout>
</repository>
<snapshotRepository>
<uniqueVersion>true</uniqueVersion>
<id>propSnap</id>
<name>Propellors Snapshots</name>
<url>sftp://propellers.net/maven</url>
<layout>legacy</layout>
</snapshotRepository>
<!-- ...其他配置... -->
</distributionManagement>
- 部署网站和文档
<!--除了部署当前 Maven 项目的构件,<distributionManagement> 还可以用于部署当前 Maven 项目的网站和文档。-->
<distributionManagement>
<!-- ...其他配置... -->
<site>
<id>mojo.website</id>
<name>Mojo Website</name>
<url>scp://beaver.codehaus.org/home/projects/mojo/public_html/</url>
</site>
</distributionManagement>
- 重命名构件
<!--随着 Maven 项目的发展,构件可能需要重新发布到新的库。使用 <relocation> 可以将当前 Maven 项目以新的构件形式发布到另一个库-->
<distributionManagement>
<!-- ...其他配置... -->
<relocation>
<groupId>org.apache</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
<message>We have moved the Project under Apache</message>
</relocation>
</distributionManagement>
maven 多模組專案建立
父pom setting
- gav
- parent
- module
- packaging pom
- dependencyManagement
- name url properties profile
maven POM設定
四層體系
- 超POM -> 父POM ->當下POM ->Effective POM
GAV
- maven 座標 xyz 向量說明(groupId(公司),artifactId(專案),version(版本))
- groupId命名 : 倒敘法 com.example.(專案名稱)daily
- artifactId : markdown-daily
- version :1.0-snapshot ->snapshot 快照不穩定版本 released穩定版
<groupId>com.example.daily</groupId>
<artifactId>markdown-daily</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
packaging
- packaging 類型jar , war , ear , pom , rar , ejb和maven-plugin
<packaging>pom</packaging>
properties
- 自訂屬性:可以客制參數之後直接使用
<properties>
<spring.profiles.active>dev</spring.profiles.active>
<spring.version>4.0.7.RELEASE</spring.version>
<project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
<spc-name>spc-value</spc-name>
</properties>
- mvn help:evaluate 參數 ,可以測試讀取到的參數
- Java System.getProperties();{user.country}
- 環境參數使用;{env.JAVA_HOME}
- POM一級標籤;{project.artifactId}
- POM次級標籤;{project.parent.artifactId}
- POM列表標籤;{project.modules[0]}
- Setting設定;{settings.localRepository}
parent
- 繼承專案 ex Spring Boot
- 由於 Spring Boot 這套框架幫你整理了許多 Starters 套件,可以大幅簡化你在開發各種不同應用程式的時候的上手門檻。而這份 spring-boot-starter-parent POM 檔裡面,就定義了所有 Starters 套件的預設值。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
</parent>
module
- 需指定要管理的子模塊
<!- artifactId >
<modules>
<module>core</module>
<module>service</module>
<module>webapp</module>
</modules>
- 子模塊也必須加入parent
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
dependencies
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </>
build
- 座標;artifactId , version
- execution: id , phase ,goals/goal
- 可以在maven build 配置jdk 版本
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
- 設定當使用mvn執行建構時所使用的plugin
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
- Spring Boot build Spring Boot 執行時需要用到
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
profile
- 當使用mvn執行建構時可透過 "mvn -P" 執行不同的建構程序
- 所謂 Profile (設定檔) 通常有個名字(Profile Name),這個名字代表一組應用程式配置。你可以透過一個簡單的設定名稱(Profile Name),快速的切換應用程式配置,就這麼簡單!
<profiles>
<profile>
<!-- ex 當執行 "mvn -P test",會使用這個profile的建構內容 -->
<id>test</id>
<!-- 可以在profile加上特定的dependency -->
<dependencies/>
<!-- 也可以在每個profile中定義不同的建構流程 -->
<build> </>
</profile>
</profiles>
- 透過 Maven 在應用程式的「編譯時期」加入屬性值,此時就會需要一個方法將 Maven 的 pom.xml 定義的屬性傳入到 src/main/resources/application.properties 屬性檔中。
### src/main/resources/application.properties
my.profile=@my.profile@
<!-- pom >
<my.profile>dev2</my.profile>
### 執行時期才賦值(Assign Value)
mvn clean spring-boot:run -Dmy.profile=dev3
- profile可以應用在切換不同環境的設定檔
### src/main/resources/application.properties
spring.profiles.active=@spring.profiles.active@
<!-- Spring Boot ex>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
mvn clean spring-boot:run -P dev
#### application-{ProfileName}.properties 須建立
- 只要很簡單的在類別上額外套用 @Profile 標注,就可以宣告 Spring 要在特定 Profile
@Component
@Profile("!dev")
public class UserServiceProd {
@Bean
public UserService getUserService() {
return new UserService("Prod");
}
}
資源過濾
輸入指令 更新pom裡的設定
mvn clean resources:resources -P profile
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<resource>
</resources>
</build>
maven 依賴衝突處理
一.依賴仲裁
- 以路徑近的為優先
- 以先宣告的為優先
二.解決辦法
- 把衝突jar包找到
- 透過exclude把jar包排除或明確聲明依賴
三.查詢插件
- idea maven helper
- maven enforce
只有jar包如何導入module
使用mvn install
mvn install:install-file -D file=path/to/your.jar -D groupId=com.example -D artifactId=my-local-jar -D version=1.0.0 -D packaging=jar
發布
release version
mvn versions:set -DgroupId={groupId} -DartifactId={{groupId}} -DoldVersion=1.1.1 -DnewVersion=1.1.2-SNAPSHOT-->
mvn -N versions:update-child-modules
mvn versions:commit
push to mvn center
mvn clean deploy -P release
modify project version
mvn versions:set -D groupId={groupId} -D artifactId={{groupId}} -D oldVersion=1.1.1 -D newVersion=1.1.2-SNAPSHOT-->
mvn -N versions:update-child-modules
mvn versions:commit
Maven 将打包后的项目部署到远程仓库
编辑 pom.xml 文件:在你的项目的 pom.xml 文件中,配置 distributionManagement 元素。这个元素用于指定发布到远程仓库的相关信息。例如,你可以配置发布版本(稳定版本)和快照版本(开发测试版本)的仓库地址。
<distributionManagement>
<repository>
<id>releases</id>
<name>Internal Releases</name>
<url>https://your-remote-repo-url/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Internal Snapshots</name>
<url>https://your-remote-repo-url/snapshots</url>
</snapshotRepository>
</distributionManagement>
执行 Maven 命令:运行以下命令来将项目构建输出的构件部署到远程仓库
mvn clean deploy
maven 插件
maven 可以自製插件,artifactId 須按照maven規定格式命名 ex:
mvn name:sayHello
使用bat安裝目錄底下所有專案
@echo off
setlocal enableDelayedExpansion
REM 设置目标文件夹
set TARGET_FOLDER=C:\path\to\aFolder
REM 遍历目标文件夹下的所有子目录
for /d %%d in ("%TARGET_FOLDER%\*") do (
echo Processing directory: %%d
cd %%d
mvn clean install -D skipTests
cd ..
)
endLocal
pause
vscode無法執行指令
mvn -N io.takari:maven:wrapper