maven 二三事

2018-07-09  本文已影响0人  lazysong

相关参考资料

https://maven.apache.org/
https://docs.rubiconred.com/myst-studio/build/maven/
http://wiki.jikexueyuan.com/project/maven/
http://tutorials.jenkov.com/maven/maven-tutorial.html

maven简介

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 
          http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository>D:/m2repo</localRepository>
</settings>

中央仓库是由maven社区提供的,不需要对中央仓库的地址进行配置,但是访问中央仓库需要网络连接

<repositories>
      <repository>
         <id>central.repository</id>
         <url>http://repo1.maven.org/maven2/</url>
      </repository>
</repositories>

远程仓库则是用户自己配置的仓库,比如公司自己的私有仓库,远程仓库可以通过不同的文件传输协议来指定

<repositories>
      <repository>
         <id>remote.repository</id>
         <url>http://download.ogrname.com/maven2/</url>
      </repository>
</repositories>

maven的构建生命周期

maven的构建生命周期是由一系列执行的task和goal组成的。maven 2.0开始就是面向构建生命周期的。
maven具有以下三个内建的构建生命周期:

构建目标:
比如构建目标为antrun:run,对应的插件为maven-antrun-plugin
构建目标可以被绑定到一个或者多个构建阶段上,此时构建目标会在每个绑定的构建阶段都执行一次。构建目标也可以不被绑定到任何一个构建阶段,但此时只能通过mvn命令来直接运行此目标。

maven有一些默认的插件,比如maven-clean-plugin,这个插件其中的一个构建目标为clean:clean,在compile构建阶段,这个构建目标会被执行

$ mvn clean:clean
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.mycompany.app:my-app >----------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-cli) @ my-app ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.225 s
[INFO] Finished at: 2018-07-09T17:44:41+08:00
[INFO] ------------------------------------------------------------------------

clean构建生命周期的clean构建阶段???

maven 的profile

maven的profile配置可以让你的maven在不同的情况下运行不同的配置,并且仅需要一个pom.xml文件。

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jenkov.crawler</groupId>
  <artifactId>java-web-crawler</artifactId>
  <version>1.0.0</version>

  <profiles>
      <profile>
          <id>test</id>
          <activation>...</activation>
          <build>...</build>
          <modules>...</modules>
          <repositories>...</repositories>
          <pluginRepositories>...</pluginRepositories>
          <dependencies>...</dependencies>
          <reporting>...</reporting>
          <dependencyManagement>...</dependencyManagement>
          <distributionManagement>...</distributionManagement>
      </profile>
  </profiles>

</project>

其中<activation>...</activation>标签描述了profile生效的条件。
profile生效的方式有两种,一种是在setting.xml文件中指定profile,另一种方式是在mvn命令中通过-P profile-name 来指定profile。profile生效之后,会覆盖pom.xml中默认的配置。

maven插件

创建一个简单的maven项目

通过archetype插件,指定几个必须的参数,groupid,artifactid,version

如何运行maven命令

上一篇 下一篇

猜你喜欢

热点阅读