Maven 笔记

本贴最后更新于 2481 天前,其中的信息可能已经斗转星移

Maven 笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。

封面


目录

[TOC]

工欲善其事,必先利其器
——《论语·卫灵公》

项目组准备将 Kubernetes 升级到 1.6 版本,然而项目使用的 fabric8 的 kubernetes-client 对 1.6 版本支持的还不到位,比如说当前不支持使用 batch/v1 的 api 来创建 Job,所以只能自己改源码来实现。虽然代码改的很快,但是最后在修改 pom.xm,以及上传到 Maven 私仓的过程中,却耗费了很长的时间。对于这种多级 Maven 工程的代码,到现在依然有很多疑惑。趁着这次事件,对 Maven 又更熟悉了一些,所以做一些笔记,留待以后阅读。


安装

前提:

JAVA 环境,JAVA_HOME 已配置

安装包:

apache-maven-3.3.9-bin.zip

安装步骤:

  1. 解压安装包;
  2. 将 apache-maven-3.3.9 的 bin 目录添加到 PATH 环境变量中;如果是 linux,export PATH=/opt/apache-maven-3.3.9/bin:$PATH

验证:

在命令行使用 mvn -v 进行确认,结果应如下图:

$ mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: D:\apache-maven-3.3.9
Java version: 1.8.0_74, vendor: Oracle Corporation
Java home: D:\Program Files (x86)\Java\jdk1.8.0_74\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"

常用命令

1. mvn clean

清理产生的项目

2. mvn compile

编译源代码

3. mvn test-compile

编译测试代码

4. mvn package

打包

5. mvn jar:jar

打 jar 包

6. mvn install

在本地仓库中安装 jar 包

7. mvn checkstyle:checkstyle

检查代码的样式是否符合规范,规范可以在 pom.xml 中自定义,比如是否存在没有使用的 import 等。

8. mvn test

运行测试

9. -Dmaven.test.skip=true

跳过测试

10. mvn deploy

直接 mvn deploy 会根据 pom.xml 里的 distributionManagement 与 setting.xml 里的 servers 来指定 maven 私仓以及认证信息,会将所有的 jar 包、依赖关系、test 结果都上传到 maven 仓库。可以结合 -Dmaven.test.skip=true 使用,来跳过 test 过程。

<distributionManagement>
    <repository>
      <id>user-release</id>
      <url>http://nexus.dis.sohucs.com/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
      <id>user-snapshot</id>
      <url>http://nexus.dis.sohucs.com/content/repositories/snapshots/</url>
    </snapshotRepository>
  </distributionManagement>
<server>  
  <id>user-release</id>  
  <username>deployment</username>  
  <password>deploymentsohu</password>  
</server>  
<server>  
   <id>user-plugin</id>  
  <username>deployment</username>  
  <password>deploymentsohu</password>  
</server>    
 <server>  
   <id>user-snapshot</id>  
  <username>deployment</username>  
  <password>deploymentsohu</password>
</server>

也可以上传指定单个 jar 包:

mvn deploy:deploy-file -DgroupId=org.domeos  -DartifactId=kubernetes-client -Dversion=2.3.1 -Dpackaging=jar -Durl=http://nexus.dis.sohucs.com/content/repositories/releases/ -DrepositoryId=user-release -Dfile=target/kubernetes-client-2.3.1.jar
  • -DgroupId-DartifactId 构成了 jar 包在 pom.xml 的坐标。
  • -Dfile 用来指定 jar 包在本地的绝对路径。
  • -Durl 用来指定 maven 私仓的位置,打开 nexus-->repositories 菜单,可以看到该路径。
  • -DrepositoryId 用来指定 repository id,在 nexus 的 configuration 可以看到,一般 setting.xml 里的 servers 中的各个 server 的 id 与 repository id 是一致的,所以也可以 setting.xml 中确定。
  • -Dversion 用来指定 jar 包本信息。

问题汇总

1. [ERROR] No implementation for org.eclipse.aether.RepositorySystem was bound.

错误信息:

[ERROR] 1) No implementation for org.eclipse.aether.RepositorySystem was bound.
[ERROR] while locating ru.yandex.qatools.allure.report.AllureReportMojo
[ERROR] at ClassRealm[plugin>ru.yandex.qatools.allure:allure-maven-plugin:2.0, parent: ClassRealm[plugin>org.apache.maven.plugins:maven-site-plugin:3.0, parent: sun.misc.Launcher$AppClassLoader@6d06d69c]]
[ERROR] while locating org.apache.maven.plugin.Mojo annotated with @com.google.inject.name.Named(value=ru.yandex.qatools.allure:allure-maven-plugin:2.0:report)
[ERROR] 
[ERROR] 1 error
[ERROR] role: org.apache.maven.plugin.Mojo
[ERROR] roleHint: ru.yandex.qatools.allure:allure-maven-plugin:2.0:report
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

原因:

allure-maven-plugin 需要在 3.1.1 以上的版本才能使用。

也许 Maven 版本太低,也许使用的是 IDEA 14,而 IDEA 14 集成的 Maven 版本是 3.0.5,所以版本还是太低。

解决办法:

将 Maven 升级到 3.1.1 以上的版本。

参考文档


注:封面来自于:GRATISOGRAPHY

  • Maven

    Maven 是基于项目对象模型(POM)、通过一小段描述信息来管理项目的构建、报告和文档的软件项目管理工具。

    185 引用 • 318 回帖 • 352 关注

相关帖子

回帖

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...