Maven运行命令随记

来自三线的随记

注意本文是基于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:deploy-file -Dfile=data-framework-2.8.0-RELEASE.pom -DrepositoryId=upload-release -Durl=http://user:password@nexus-url/repository/maven-uploaded-url -DpomFile=data-framework-2.8.0-RELEASE.pom -Dpackaging=pom

注意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 deploy file

-DaltDeploymentRepository=id::layout::url

依赖于maven settings.xml , id取自settings.xml server段配置

mvn -B clean deploy -Dmaven.test.skip=true \
    -DaltSnapshotDeploymentRepository=maven-snapshots::default::http://{address}/repository/maven-snapshots \
    -DaltReleaseDeploymentRepository=maven-releases::default::http://{address}/repository/maven-releases

使用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 \""}'
  • 部分jar包文件名可能比较特殊多了个后缀(-noaop.jar)
find . -name "*.jar" | awk '{gsub("\.jar$","",$0);print "mvn deploy:deploy-file -Dfile="$0".jar -DpomFile="$0".pom -Dpackaging=jar -Dclassifier=noaop -DrepositoryId=nexus -Durl=\"http://nexus-path \""}'

使用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*/**

·Maven: 从命令行(commandline)读取pom.xml中的属性(property)

mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout
mvn help:evaluate -Dexpression=project.version -q -DforceStdout

forceStdout这个选项是在maven-help-plugin 3.2.0以上版本才有效的(在maven 3.5.0下执行时,maven-help-plugin默认使用的版本是2.2)

mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=project.version -q -DforceStdout