maven——项目构建和依赖管理工具

时间:2024-11-02 13:04:25

apache maven是一个用于项目构建和依赖管理的工具。

添加archetype

https://repo1.maven.org/maven2/archetype-catalog.xml

更改本地仓库存储位置:修改配置文件${user.home}/.m2/settings.xml或 $MAVEN_HOME/conf/setting.xml中的内容

<localRepository>...

mvn archetype:generate会非常慢,搜索了本地和远程太多archetype,而加上-DarchetypeCatalog=local将只搜索${user.home}/.m2/archetype-catalog.xml中定义的本地archetype,会大大提高速度。

mvn archetype:crawl会根据远程数据生成本地archetype目录$maven_localRepository/archetype-catalog.xml。

download jar, sources, javadoc

 mvn dependency:get -Dartifact=groupId:artifactId:version[:packaging[:classifier]]
eg. mvn dependency:get -Dartifact=mysql:mysql-connector-java:LATEST
mvn dependency:get -Dartifact=mysql:mysql-connector-java:LATEST:jar:sources

download sources/javadoc

 mvn dependency:sources -DincludeGroupIds=grouId1,groupId2... -Dclassifier=sources
mvn dependency:get -Dartifact=mysql:mysql-connector-java:LATEST:jar:sources download sources for each of dependencies in pom.xml
mvn dependency:sources
mvn dependency:resolve -Dclassifier=sources
download javadoc
mvn dependency:get -Dartifact=mysql:mysql-connector-java:LATEST:jar:javadoc
# download javadoc for each of dependencies in pom.xml
mvn dependency:sources -Dclassifier=javadoc
mvn dependency:resolve -Dclassifier=javadoc

copy dependecies

mvn dependency:copy -Dartifact=g:a:v -o -DrepoUrl=file://path/to/your/repo
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=groupId:artifactId:version[:packaging][:classifier] -DoutputDirectory=[target]

输出依赖jar包文件路径

mvn dependency:build-classpath [-Dmdep.outputFile=/path/file](输出到文件)

mvn基本的几个jar包相关命令:

mvn clean 清除target/编译文件夹

mvn compile 编译源码

mvn package 编译并打包为jar文件

mvn install 将jar文件安装到本地repo

mvn deploy 根据pom.xml中的信息,将jar包部署到远程repo

跳过测试

mvn -DskipTests

指定pom文件

mvn -f some-pom.xml

插件描述文档

mvn help:describe -Ddetail -Dplugin=<plugin-name> 或 -Dcmd=<plugin>:<phase>
# mvn help:describe -Dplugin=exec
# mvn help:describe -Dcmd=install:install-file
# mvn help:describe -Dplugin=g:a:v

关闭mvn的日志输出

mvn -q

生成源码jar包

mvn source:jar

maven 3.x super pom位置

$M2_HOME/lib/maven-model-builder-3.x.jar:/org/apache/maven/model/pom-4.0.0.xml

######## unresolved #########

list addable dependencies in local repository

list addable dependencies all available repositories

list achetypes

(不可用)get file path of artifact

mvn dependency:location -Dartifact=g