分类
devops

maven tips

maven 下载指定包到特定路径

mkdir -p /tmp/test
mvn dependency:get -Dartifact=org.apache.spark:spark-connect_2.12:3.5.4 -Dmaven.repo.local=/tmp/test

maven 根据包获取下载地址

保存以下脚本为maven-jar.sh

# https://repo1.maven.org/maven2/{groupId路径}/{artifactId}/{version}/{artifactId}-{version}.jar
artifact="${1:-org.apache.spark:spark-connect_2.12:3.5.4}"

mirror="${2:-}"

group_id="$(echo $artifact | cut -d: -f1)"
artifact_id="$(echo $artifact | cut -d: -f2)"
version="$(echo $artifact | cut -d: -f3)"

maven_repo="https://repo1.maven.org/maven2/"
if [ -n "${mirror}"  ]; then

case $mirror in
  huawei)
    maven_repo="https://repo.huaweicloud.com/repository/maven/"
    ;;
  aliyun|ali)
    maven_repo="https://maven.aliyun.com/repository/public/"
    ;;
  tencent)
    maven_repo="http://mirrors.cloud.tencent.com/nexus/repository/maven-public/"
    ;;
  *)
    maven_repo=$mirror
    ;;
esac

fi

echo "${maven_repo}${group_id//./\/}/${artifact_id}/${version}/${artifact_id}-${version}.jar"


下载举例

chmod +x maven-jar.sh
./maven-jar.sh org.apache.spark:spark-connect_2.12:3.5.4 ali | xargs -I {} -r wget {}
  • 第一个参数为构建物,构建物的格式类似如下,以冒号分割的三部分,这种格式是Gradle的写法,在mvnrepository.com网站上可以复制拷贝
  • 第二个参数为镜像(可以不传)
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
implementation("org.apache.commons:commons-lang3:3.17.0")

maven 查看插件有哪些goals

mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-dependency-plugin
mvn help:describe -DgroupId=org.springframework.boot -DartifactId=spring-boot-maven-plugin
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- help:3.5.1:describe (default-cli) @ standalone-pom ---
[INFO] org.springframework.boot:spring-boot-maven-plugin:3.5.3

Name: Spring Boot Maven Plugin
Description: (no description available)
Group Id: org.springframework.boot
Artifact Id: spring-boot-maven-plugin
Version: 3.5.3
Goal Prefix: spring-boot

This plugin has 11 goals:

spring-boot:build-image
  Description: Package an application into an OCI image using a buildpack,
    forking the lifecycle to make sure that {@code package} ran. This goal is
    suitable for command-line invocation. If you need to configure a goal
    {@code execution} in your build, use {@code build-image-no-fork} instead.

spring-boot:build-image-no-fork
  Description: Package an application into an OCI image using a buildpack,
    but without forking the lifecycle. This goal should be used when
    configuring a goal {@code execution} in your build. To invoke the goal on
    the command-line, use {@code build-image} instead.

spring-boot:build-info
  Description: Generate a {@code build-info.properties} file based on the
    content of the current {@link MavenProject}.

spring-boot:help
  Description: Display help information on spring-boot-maven-plugin.
    Call mvn spring-boot:help -Ddetail=true -Dgoal=<goal-name> to display
    parameter details.

spring-boot:process-aot
  Description: Invoke the AOT engine on the application.

spring-boot:process-test-aot
  Description: Invoke the AOT engine on tests.

spring-boot:repackage
  Description: Repackage existing JAR and WAR archives so that they can be
    executed from the command line using {@literal java -jar}. With layout=NONE
    can also be used simply to package a JAR with nested dependencies (and no
    main class, so not executable).

spring-boot:run
  Description: Run an application in place.

spring-boot:start
  Description: Start a spring application. Contrary to the {@code run} goal,
    this does not block and allows other goals to operate on the application.
    This goal is typically used in integration test scenario where the
    application is started before a test suite and stopped after.

spring-boot:stop
  Description: Stop an application that has been started by the "start" goal.
    Typically invoked once a test suite has completed.

spring-boot:test-run
  Description: Run an application in place using the test runtime classpath.
    The main class that will be used to launch the application is determined as
    follows: The configured main class, if any. Then the main class found in
    the test classes directory, if any. Then the main class found in the
    classes directory, if any.

For more information, run 'mvn help:describe [...] -Ddetail'
mvn help:describe -Dcmd=install

查找所有maven插件

curl https://repo1.maven.org/maven2/org/codehaus/mojo -LSs | grep "<a" |grep "plugin" | grep -v ".xml" | grep -oP 'title="([^"]*)"' | tr -d '"' | sed 's/title=//' | tr -d '/'



animal-sniffer-maven-plugin
antlr-maven-plugin
antlr3-maven-plugin
appassembler-maven-plugin
appfuse-maven-plugin
apt-maven-plugin
aspectj-maven-plugin
axistools-maven-plugin
batik-maven-plugin
build-helper-maven-plugin
build-on-demand-maven-plugin
buildnumber-maven-plugin
buildplan-maven-plugin
cassandra-maven-plugin
castor-maven-plugin
cbuild-plugin-parent
changelog-maven-plugin
changes-maven-plugin
chronos-jmeter-maven-plugin
chronos-plugincommon
chronos-report-maven-plugin
chronos-surefire-maven-plugin
clirr-maven-plugin
cobertura-maven-plugin
codenarc-maven-plugin
commons-attributes-maven-plugin
dashboard-maven-plugin
dbunit-maven-plugin
dbupgrade-maven-plugin
dependency-maven-plugin
deploy-all-maven-plugin
dita-maven-plugin
ditaot-maven-plugin
docbook-maven-plugin
emma-maven-plugin
enchanter-maven-plugin
exec-maven-plugin
failsafe-maven-plugin
findbugs-maven-plugin
fit-maven-plugin
fitnesse-maven-plugin
flatten-maven-plugin
groovy-maven-plugin
gwt-maven-plugin
hibernate3-maven-plugin
ianal-maven-plugin
ideauidesigner-maven-plugin
idlj-maven-plugin
jalopy-maven-plugin
jasperreports-maven-plugin
javacc-maven-plugin
javancss-maven-plugin
javascript-maven-plugin
jaxb2-maven-plugin
jaxws-maven-plugin
jboss-maven-plugin
jboss-packaging-maven-plugin
jdepend-maven-plugin
jpox-maven-plugin
jruby-maven-plugin
js-import-maven-plugin
jslint-maven-plugin
jspc-maven-plugin
jxr-maven-plugin
keytool-maven-plugin
l10n-maven-plugin
latex-maven-plugin
lesscss-maven-plugin
license-maven-plugin
local-config-maven-plugin
make-maven-plugin
maven-jpox-plugin
minijar-maven-plugin
mrm-maven-plugin
multibuild-optimizer-maven-plugin
native-maven-plugin
native2ascii-maven-plugin
nb-repository-plugin
nbm-maven-plugin
netbeans-freeform-maven-plugin
nsis-maven-plugin
openjpa-maven-plugin
osxappbundle-maven-plugin
ounce-maven-plugin
patch-maven-plugin
pde-maven-plugin
platform-detector-maven-plugin
plugin-support
project-archive-maven-plugin
project-sources-maven-plugin
properties-maven-plugin
rat-maven-plugin
remote-source-maven-plugin
retrotranslator-maven-plugin
rmic-maven-plugin
rpm-cbuild-maven-plugin
rpm-maven-plugin
rspec-maven-plugin
sablecc-maven-plugin
scmchangelog-maven-plugin
selenium-maven-plugin
servicedocgen-maven-plugin
settings-maven-plugin
shade-maven-plugin
shell-maven-plugin
ship-maven-plugin
shitty-maven-plugin
siteskinner-maven-plugin
smc-maven-plugin
solaris-maven-plugin
sonar-maven-plugin
springdoclet-maven-plugin
springws-maven-plugin
sql-maven-plugin
sqlj-maven-plugin
surefire-report-maven-plugin
sysdeo-tomcat-maven-plugin
taglist-maven-plugin
templating-maven-plugin
tidy-maven-plugin
tomcat-maven-plugin
truezip-maven-plugin
unix-maven-plugin
versions-maven-plugin
vfs-maven-plugin
wagon-maven-plugin
was6-maven-plugin
webdoclet-maven-plugin
weblogic-maven-plugin
webminifier-maven-plugin
webstart-maven-plugin
webstart-maven-plugin-parent
webtest-maven-plugin
xdoclet-maven-plugin
xslt-maven-plugin



curl https://repo1.maven.org/maven2/org/apache/maven/plugins -LSs | grep "<a" | grep -v ".xml" | grep -oP 'title="([^"]*)"' | tr -d '"' | sed 's/title=//' | tr -d '/'
maven-acr-plugin
maven-ant-plugin
maven-antlr-plugin
maven-antrun-plugin
maven-archetype-plugin
maven-artifact-plugin
maven-assembly-plugin
maven-changelog-plugin
maven-changes-plugin
maven-checkstyle-plugin
maven-clean-plugin
maven-clover-plugin
maven-compiler-plugin
maven-continuum-plugin
maven-core-it-plugin
maven-dependency-plugin
maven-deploy-plugin
maven-doap-plugin
maven-docck-plugin
maven-ear-plugin
maven-eclipse-plugin
maven-ejb-plugin
maven-enforcer-plugin
maven-failsafe-plugin
maven-gpg-plugin
maven-help-plugin
maven-idea-plugin
maven-install-plugin
maven-invoker-plugin
maven-jar-plugin
maven-jarsigner-plugin
maven-javadoc-plugin
maven-jdeprscan-plugin
maven-jdeps-plugin
maven-jlink-plugin
maven-jmod-plugin
maven-jxr-plugin
maven-linkcheck-plugin
maven-one-plugin
maven-patch-plugin
maven-pdf-plugin
maven-plugin-parent
maven-plugin-plugin
maven-plugin-report-plugin
maven-plugins
maven-pmd-plugin
maven-project-info-reports-plugin
maven-projecthelp-plugin
maven-rar-plugin
maven-reactor-plugin
maven-release-plugin
maven-remote-resources-plugin
maven-repository-plugin
maven-resources-plugin
maven-scm-plugin
maven-scm-publish-plugin
maven-scripting-plugin
maven-shade-plugin
maven-site-plugin
maven-source-plugin
maven-stage-plugin
maven-surefire-plugin
maven-surefire-report-plugin
maven-toolchains-plugin
maven-verifier-plugin
maven-war-plugin
maven-wrapper-plugin



mvn 一共有多少个 phase?

Maven 的生命周期(Lifecycle)中一共有 23 个阶段(Phase),这些阶段定义了构建过程中的不同步骤。

以下是 Maven 生命周期中的 23 个阶段:

  1. validate:验证项目结构和依赖关系。
  2. initialize:初始化项目构建环境。
  3. generate-sources:生成源代码。
  4. process-sources:处理源代码。
  5. generate-resources:生成资源文件。
  6. process-resources:处理资源文件。
  7. compile:编译源代码。
  8. test-compile:编译测试代码。
  9. test:运行测试。
  10. package:打包项目。
  11. integration-test:运行集成测试。
  12. verify:验证项目构建结果。
  13. install:安装项目到本地仓库。
  14. deploy:部署项目到远程仓库。

此外,还有一些特殊的阶段:

  1. pre-clean:在 clean 阶段之前执行。
  2. post-clean:在 clean 阶段之后执行。
  3. pre-site:在 site 阶段之前执行。
  4. post-site:在 site 阶段之后执行。
  5. pre-integration-test:在 integration-test 阶段之前执行。
  6. post-integration-test:在 integration-test 阶段之后执行。
  7. pre-deploy:在 deploy 阶段之前执行。
  8. post-deploy:在 deploy 阶段之后执行。

这些阶段可以通过在 pom.xml 文件中配置插件(Plugin)来执行特定的任务。

pom.xml 文件中,可以使用 <phase> 元素来指定插件执行的阶段,例如:

<plugin>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>...</goal>
            </goals>
        </execution>
    </executions>
</plugin>

maven如何检查依赖的版本是否需要升级

# https://www.mojohaus.org/versions/versions-maven-plugin/examples/display-dependency-updates.html

mvn versions:display-dependency-updates