15分钟Maven

Maven

Scope

  1. provided - Dependencies that are required for compiling the project code, but that will be provided at runtime by a container running the code (e.g., the Java Servlet API).
  2. test - Dependencies that are used for compiling and running tests, but not required for building or running the project’s runtime code.

常用命令

mvn install

The install goal will compile, test, and package your project’s code and then copy it into the local dependency repository, ready for another project to reference it as a dependency.

mvn compile

编译,target/classes/*

mvn package

打包,target/artifact + version .jar

忽略测试打包

mvn install -Dmaven.test.skip=true

Maven

包冲突

exclusions:忽略掉curator-framework对log4j的依赖

 <dependency>
            <groupId>com.netflix.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${curator_version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

dependencies VS dependencyManagement

Maven 使用dependencyManagement 元素来提供了一种管理依赖版本号的方式。通常会在一个组织或者项目的最顶层的父POM 中看到dependencyManagement 元素。使用pom.xml 中的dependencyManagement 元素能让
所有在子项目中引用一个依赖而不用显式的列出版本号。Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement 元素的项目,然后它就会使用在这个dependencyManagement 元素中指定的版本号。

这样做的好处不言而喻的。 因为不需要到处复制版本号,版本升级也狠简单,只要改住pom中的dependencyManagement中的版本。

Java编译器版本设置

Intellij IDEA中Module设置里虽然有设置JDK编译器版本(language level), 但是直接在父pom里设置可以一劳永逸!

编译器插件配置

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
   <source>1.7</source>
   <target>1.7</target>
 </configuration>
</plugin>

发布二方库

在root module的pom里指定仓库地址,正式版本仓库和snapshot

<distributionManagement>
   <repository>
       <id>grade2.releases.dist</id>
       <url>http://mvn.grade2-ent.com/nexus/content/repositories/releases</url>
   </repository>
   <snapshotRepository>
       <id>grade2.snapshots.dist</id>
       <url>http://mvn.grade2-ent.com/nexus/content/repositories/snapshots</url>
   </snapshotRepository>
</distributionManagement>

如果项目有很多模块组成,部分子模块不需要发布二方库,那么在子模块的pom中添加:

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>${maven-deploy-plugin.version}</version>
      <configuration>
          <skip>true</skip>
      </configuration>
  </plugin>

多模块升级版本号

  • 升级版本号
mvn versions:set -DnewVersion=2.50.1-SNAPSHOT
  • 如果出错了,可以revert,因为mvn会备份pom文件
mvn versions:revert
  • 提交升级,mvn会删除备份的pom文件
mvn versions:commit

后记

其实是Maven杂。

要对得起标题,还需要完善一下,哈哈

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容