总览

POM文件帮助文档.md
maven由Java开发

image.png

安装和配置maven

阿里云云效
安装maven和国内镜像: maven最最重要的配置:配置国内阿里云镜像仓库,快到起飞

注意,标签顺序不能变

  1. <mirror>
  2. <!--此镜像的唯一标识符,用来区分不同的mirror元素-->
  3. <id>alimaven</id>
  4. <!--镜像名称-->
  5. <name>aliyun maven</name>
  6. <!--镜像url-->
  7. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  8. <!--对哪种仓库镜像-->
  9. <mirrorOf>central</mirrorOf>
  10. </mirror>

项目里单独配置:

  1. repositories {
  2. mavenLocal()
  3. maven {
  4. url = 'https://maven.aliyun.com/repository/public'
  5. }
  6. }

依赖

parent的依赖优先于次级依赖
Introduction to the Dependency Mechanism

依赖传递

  • 优先性

声明优先说的是2度依赖
特殊优先说的是1度依赖,如同时配置了log4j的1.2.13和1.2.14,取后者

  • 可选依赖

隐藏自己用的东西
<optional>true</option>

  • 排除依赖

不用别人用的东西
<exclusions></exlusions

依赖范围

scope: Maven中的依赖作用范围

  • compile —— gradle api
  • provided —— gradle compileOnly
  • runtime —— gradle runtimeOnly

image.png

dependencymanagement

Maven实战—- dependencies与dependencyManagement的区别

dependency tree

IntelliJ IDEA(idea) 查看 maven依赖树

版本控制

SNAPSHOT

关于 maven 中 SNAPSHOT 的 jar包的更新机制

模块版本控制

  1. <modules>
  2. <module>client</module>
  3. <module>dao</module>
  4. <module>core</module>
  5. <module>start</module>
  6. <module>common</module>
  7. <module>facade</module>
  8. <module>log-sdk</module>
  9. </modules>
  10. <dependency>
  11. <groupId>com.raycloud.sheji</groupId>
  12. <artifactId>raycloud-sheji-infra-client</artifactId>
  13. <version>${infra.client.version}</version>
  14. </dependency>
  15. # 这是外部pom中定义的,子module中的版本可能会重新指定
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <dubbo.version>2.7.7</dubbo.version>
  19. <zookeeper.version>3.4.13</zookeeper.version>
  20. <infra.client.version>1.0.0-SNAPSHOT</infra.client.version>
  21. </properties>

优先级:子module > 主module > parent

最新版本

项目中一般不用LATEST,可以用此拉远程库知道最新版本是什么

  1. <dependency>
  2. <groupId>com.paypal.sdk</groupId>
  3. <artifactId>checkout-sdk</artifactId>
  4. <version>LATEST</version>
  5. </dependency>

项目构建

各种命令都是由众多插件支持

插件

image.png
image.png

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <!-- compile插件 -->
  4. <artifactId>maven-compiler-plugin</artifactId>
  5. <configuration>
  6. <source>1.6</source>
  7. <target>1.6</target>
  8. </configuration>
  9. <executions>
  10. <execution>
  11. <id>default-testCompile</id>
  12. <!-- 哪个阶段执行,参照官网 -->
  13. <phase>test-compile</phase>
  14. <configuration>
  15. <testExcludes>
  16. <exclude>**/jsfunit/*.java</exclude>
  17. </testExcludes>
  18. </configuration>
  19. <!-- goals,参照官网 -->
  20. <goals>
  21. <goal>testCompile</goal>
  22. </goals>
  23. </execution>
  24. </executions>
  25. </plugin>

Compile

maven-compiler-plugin include/exclude

Package

compile + test compile + test

Install

compile + test compile + test + local repo

多模块

pom坐标groupId, artifactId等

packaging

maven pom.xml配置文件中的packaging标签

聚合

继承

Maven goals

maven goals

  1. Maven tool window — lifecycle
  2. Run Anything window
  3. Run/Debug Configurations

    私服 Maven Nexus

    image.png
    Nexus Guides
  • JDK配置

image.png
安装运行中遇到的问题

  1. 8081端口和nginx冲突,修改端口为8281
  2. 安装目录/usr/local的owner是root,为了不改变owner运行,要在root用户安装sdkman和java8,并且要把java和nexus的bin软连接到/usr/bin,然后在root中运行nexus run。

    Maven常用命令

    maven常用命令

    environment variables

    maven 命令的格式为 mvn [plugin-name]:[goal-name],可以接受的参数如下,
    -D 指定参数,如 -Dmaven.test.skip=true 跳过单元测试
    -P 指定 Profile 配置,可以用于区分环境;
    -e 显示maven运行出错的信息;
    -o 离线执行命令,即不去远程仓库更新包;
    -X 显示maven允许的debug信息;
    -U 强制去远程更新snapshot的插件或依赖,默认每天只更新一次。

常用maven命令

  1. 创建maven项目:mvn archetype:generate
    指定 group: -DgroupId=packageName
    指定 artifact:-DartifactId=projectName
    创建web项目:-DarchetypeArtifactId=maven-archetype-webapp

    1. mvn archetype:generate -DgroupId=com.itheima -DartifactId=java-project -
    2. DarchetypeArtifactId=maven-archetype-quickstart -Dversion=0.0.1-snapshot -
    3. DinteractiveMode=false
    1. mvn archetype:generate -DgroupId=com.itheima -DartifactId=web-project -
    2. DarchetypeArtifactId=maven-archetype-webapp -Dversion=0.0.1-snapshot -
    3. DinteractiveMode=false
  2. 验证项目是否正确:mvn validate

  3. maven 打包:mvn package
  4. 只打jar包:mvn jar:jar
  5. 生成源码jar包:mvn source:jar
  6. 产生应用需要的任何额外的源代码:mvn generate-sources
  7. 编译源代码: mvn compile
  8. 编译测试代码:mvn test-compile
  9. 运行测试:mvn test
  10. 运行检查:mvn verify
  11. 清理maven项目:mvn clean
  12. 生成eclipse项目:mvn eclipse:eclipse
  13. 清理eclipse配置:mvn eclipse:clean
  14. 生成idea项目:mvn idea:idea
  15. 安装项目到本地仓库:mvn install
  16. 发布项目到远程仓库:mvn:deploy
  17. 在集成测试可以运行的环境中处理和发布包:mvn integration-test
  18. 显示maven依赖树:mvn dependency:tree
  19. 显示maven依赖列表:mvn dependency:list
  20. 下载依赖包的源码:mvn dependency:sources
  21. 安装本地jar到本地仓库:mvn install:install-file -DgroupId=packageName -DartifactId=projectName -Dversion=version -Dpackaging=jar -Dfile=path
  22. mvn命令跳过测试:
    mvn install -Dmaven.test.skip=true 测试类不会生成.class 文件
    mvn install -DskipTests 测试类会生成.class文件

web项目相关命令

  1. 启动tomcat:mvn tomcat:run
  2. 启动jetty:mvn jetty:run
  3. 运行打包部署:mvn tomcat:deploy
  4. 撤销部署:mvn tomcat:undeploy
  5. 启动web应用:mvn tomcat:start
  6. 停止web应用:mvn tomcat:stop
  7. 重新部署:mvn tomcat:redeploy
  8. 部署展开的war文件:mvn war:exploded tomcat:exploded

Maven Surefire Plugin

  • surefire-reports: target/surefire-reports/index.html

    excludes and includes

    1. <plugin>
    2. <groupId>org.apache.maven.plugins</groupId>
    3. <artifactId>maven-surefire-plugin</artifactId>
    4. <version>3.0.0-M4</version>
    5. <configuration>
    6. <excludes>
    7. <exclude>**/pm/*.java</exclude>
    8. <exclude>**/Pm*.java</exclude>
    9. </excludes>
    10. <includes>
    11. <include>com/synnex/biz/hierarchy/service/sales/dept/*Test.java</include>
    12. </includes>
    13. </configuration>
    14. </plugin>
    1. # Test one class
    2. mvn test -Dtest=AppTest
    3. # Test one method
    4. mvn test -Dtest=AppTest#testFoo
    5. # Test two methods with the plus sign (+)
    6. mvn test -Dtest=AppTest#testFoo+testBar
    7. # Test multiple items comma separated and with a wildcard (*)
    8. mvn test -Dtest=AppTest,Web*
    9. # Test by Package with a wildcard (*)
    10. mvn test -Dtest=com.mycompany.*.*Test
    1. # Exclude one test class, by using the explanation mark (!)
    2. mvn test -Dtest=!LegacyTest
    3. # Exclude one test method
    4. mvn verify -Dtest=!LegacyTest#testFoo
    5. # Exclude two test methods
    6. mvn verify -Dtest=!LegacyTest#testFoo+testBar
    7. # Exclude a package with a wildcard (*)
    8. mvn test -Dtest=!com.mycompany.app.Legacy*

    问题

    _remote.repositories文件
    使用Maven管理项目时,明明本地仓库有对应的jar包,但还是报找不到