Maven运行命令随记

来自三线的随记
Admin讨论 | 贡献2021年8月5日 (四) 19:48的版本

注意本文是基于linux 的记录


· Maven在运行命令中设置代理

mvn -B -DproxySet=true -Dhttp.proxyHost=${proxy_host} -Dhttp.proxyPort=${proxy_port} -Dhttp.nonProxyHosts="nonproxy.com|nonproxy.net:8080" package
mvn -B -DproxySet=true -Dhttp.proxyHost=${proxy_host} -Dhttps.proxyHost=${proxy_host} -Dhttp.proxyPort=${proxy_port} -Dhttps.proxyPort=${proxy_port} -Dhttp.nonProxyHosts="" install -U -Dmaven.test.skip=true

允许不安全的https

-Dmaven.wagon.http.ssl.insecure=true

Relation article

https://stackoverflow.com/questions/1251192/how-do-i-use-maven-through-a-proxy

该帖子中还会提到另一种设置http proxy以及socket proxy的参数,仅供参考,有待验证


· 使用Maven向Nexus deploy file

使用Maven 向Nexus deploy jar包

mvn deploy:deploy-file -DgroupId=${groupId} -DartifactId=${artifactId} -Dversion=${version} -Dpackaging=jar -Dfile=/jar-path.jar -Durl=http://nexus/repository/maven-uploaded-url

如果nexus有密码认证的话则需要-DrepositoryId=参数调用写在了settings.xml文件中servers段内的账号密码进行认证

mvn deploy:deploy-file -DgroupId=${groupId} -DartifactId=${artifactId} -Dversion=${version} -Dpackaging=jar -Dfile=/jar-path.jar -DrepositoryId=nexus -Durl=http://nexus/repository/maven-uploaded-url

注意mvn deploy-file不支持上传处于本地repo中的jar包,即: Cannot deploy artifact from the local repository

需要将 ~/.m2/repository 中相应的jar包提前cp出来再进行deploy


不想写groupId, artifactId,version的话就要pom文件来让maven自己识别

mvn deploy:deploy-file -DpomFile=${pomLocation} -Dfile=${jarPath} -DrepositoryId=nexus -Durl=http://nexus/repository/maven-uploaded-url


使用Maven向Nexus 批量 upload jar 包

find . -name "*.jar" | awk '{gsub("\.jar$","",$0);print "mvn deploy:deploy-file -Dfile="$0".jar -DpomFile="$0".pom -Dpackaging=jar -DrepositoryId=nexus -Durl=\"http://nexus-path \""}'

或者原脚本内容:

#!/bin/sh

# Reference: http://roboojack.blogspot.in/2014/12/bulk-upload-your-local-maven-artifacts.html

if [ "$#" -ne 3 ] || ! [ -d "$1" ]; then
    echo "Usage:"
    echo "       bash run.sh <repoRootFolder> <repositoryId> <repositoryUrl>"
    echo ""
    echo ""
    echo "       Where..."
    echo "       repoRootFolder: The folder containing the repository tree."
    echo "                       Ensure you move the repository outside of ~/.m2 folder"
    echo "                       or whatever is configured in settings.xml"
    echo "       repositoryId:   The repositoryId from the <server> configured for the repositoryUrl in settings.xml."
    echo "                       Ensure that you have configured username and password in settings.xml."
    echo "       repositoryUrl:  The URL of the repository where you want to upload the files."
    exit 1
fi

while read -r line ; do
    echo "Processing file $line"
    pomLocation=${line/./}
    pomLocation=$PWD${pomLocation/jar/pom}
    jarLocation=${line/./}
    jarLocation=$PWD${jarLocation/pom/jar}
    #echo $pomLocation
    #echo $jarLocation
    mvn deploy:deploy-file -DpomFile=$pomLocation -Dfile=$jarLocation -DrepositoryId=$2 -Durl=$3
done < <(find $1 -name "*.jar")


该脚本解析location似乎有问题,改了一下

#!/bin/sh

# Reference: http://roboojack.blogspot.in/2014/12/bulk-upload-your-local-maven-artifacts.html

if [ "$#" -ne 3 ] || ! [ -d "$1" ]; then
    echo "Usage:"
    echo "       bash run.sh <repoRootFolder> <repositoryId> <repositoryUrl>"
    echo ""
    echo ""
    echo "       Where..."
    echo "       repoRootFolder: The folder containing the repository tree."
    echo "                       Ensure you move the repository outside of ~/.m2 folder"
    echo "                       or whatever is configured in settings.xml"
    echo "       repositoryId:   The repositoryId from the <server> configured for the repositoryUrl in settings.xml."
    echo "                       Ensure that you have configured username and password in settings.xml."
    echo "       repositoryUrl:  The URL of the repository where you want to upload the files."
    exit 1
fi

while read -r line ; do
    echo "Processing file $line"
    Location=${line/.jar}
    pomLocation=${Location}.pom
    jarLocation=${Location}.jar
    echo $pomLocation
    echo $jarLocation
    mvn deploy:deploy-file -DpomFile=$pomLocation -Dfile=$jarLocation -DrepositoryId=$2 -Durl=$3
done < <(find $1 -name "*.jar")

使用命令 bash script.sh m2 nexus "http://nexus/repo" 开始冲冲冲

使用Maven向Nexus 批量 delpoy pom文件

find . -mindepth 6 -maxdepth 6 -name "*.pom"|awk 'BEGIN{path=$0;FS="\/"}{print "mvn deploy:deploy-file -Dfile="$path" -Dpackaging=pom -DrepositoryId=nexus -Durl=http://nexus-url -DgroupId="$3"."$4" -DartifactId="$5" -Dversion="$6}'

注意调整mindepth和maxdepth可能需要修改相应的其余变量偏移量重新组合成groupId和artifactId

形成效果:

mvn deploy:deploy-file -Dfile=./org/springframework/session/spring-session/1.3.1.RELEASE/spring-session-1.3.1.RELEASE.pom -Dpackaging=pom -DrepositoryId=nexus -Durl=http://nexus-url -DgroupId=springframework.session -DartifactId=spring-session -Dversion=1.3.1.RELEASE


· Maven : test skip

-Dmaven.test.skip=true


· Maven plugin: SonarQube

simple documentation

jacoco 需要在sonarqube server提前安装

# sonar-scanner documents: docs.sonarqube.org/latest/analysis/analysis-parameters
# Environment variable description:
#   SONAR_LOGIN: SonarQube Token
#   SONAR_HOST_URL: SonarQube Homepage URL
#   SONAR_PROJECT_KEY: SonarQube Project Key
#   SONAR_SOURCES: Directory to be analyzed
#   SONAR_JAVA_BINARIES: Comma-separated paths to directories containing the compiled bytecode files corresponding to your source files.
# Jacoco unit test:
#  -Dsonar.java.covergaePlugin=jacoco
#  -Dsonar.jacoco.reportPaths=target/jacoco.exec
jacoco是一个开源的覆盖率工具,针对java语言

适用于单元覆盖率验证,通常要求单元覆盖率不低于60%。

JaCoCo优势:

    (1) JaCoCo支持分支覆盖、引入了Agent模式。

    (2) EMMA官网已经不维护了,JaCoCo是其团队开发的,可以理解为一个升级版。

    (3) JaCoCo社区比较活跃,官网也在不断的维护更新。

Jacoco可以嵌入到Ant 、Maven中,并提供了EclEmma Eclipse插件,也可以使用JavaAgent技术监控Java程序。很多第三方的工具提供了对Jacoco的集成,如sonar、Jenkins等。

Jacoco包含了多种尺度的覆盖率计数器,包含指令级覆盖(Instructions,C0coverage),分支(Branches,C1coverage)、圈复杂度(CyclomaticComplexity)、行覆盖(Lines)、方法覆盖(non-abstract methods)、类覆盖(classes)
指定SonarQube plugin版本
mvn package org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar \
  -Dsonar.projectKey=$SONAR_PROJECT_KEY \
  -Dsonar.host.url=$SONAR_HOST_URL \
  -Dsonar.login=$SONAR_LOGIN \
  -Dsonar.sources=$SONAR_SOURCES \
  -Dsonar.java.binaries=$SONAR_JAVA_BINARIES \
  -Dsonar.exclusions=$SONAR_EXCLUSIONS 
sonar.verbose
-Dsonar.verbose=true


scm disable (disable git or svn scanning)

错误信息如下:
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project dep: Not inside a Git work tree: /path -> [Help 1]
解决手段1:

指定Base路径

-Dsonar.projectBaseDir
解决手段2:

直接关闭git/svn扫描

-Dsonar.scm.disabled=true


can't be indexed twice

错误信息类似如下:
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.8.0.2131:sonar (default-cli) on project project-name: File project-dir/src/test/java/com/groupid/project/service/SpotPlanScheduleServiceTest.java can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files -> [Help 1]

[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
解决手段,调整扫描目录以及忽略扫描目录

并且扫描与忽略目录不能相交:

sonar.sources=.
sonar.tests=.
sonar.test.inclusions=**/*Test*/**
sonar.exclusions=**/*Test*/**