【maven】maven入门

  • maven起源

maven这个词的来源于Yiddish语言,是“知识累加器”的意思,最初是用来简化Jakarta Turbine项目的构建过程。后来发展为由Apache软件基金会主持的独立Apache项目,用来管理和构建Java类项目

  • 为什么需要maven

以下是你实际开发过程中可能会遇到的一些场景

  1. 作为一个java小白,我如何快速创建一个java web项目?或者一个最简单的java项目来学习。
  2. 我写了几个基本的java工程了,可以在本地IDE运行了。但是我想把他打包成可执行程序jar包,到IDE之外运行,我应该怎么打包呢?
  3. 我的项目需要引用junit、fastjson、mysql-concector等众多第三方jar包,我应该如何去获取这些jar包呢?该用哪个版本?如果另一个项目也要用这些包的话,我是不是还要一个一个拷贝到另一个项目呢?
  4. 我的项目需要jar包A B,A、B jar包都依赖C,但是C的版本不一样,jar包依赖如何处理呢?
  5. 我的项目做大了,一个项目下有好多子项目,这些项目引用了许多共同的第三方jar包
  6. 我想把我的代码发布到git上,怎么保证我的项目别人下下来就是可用的呢?如何保证运行环境一致

带着上面的问题我们来学习maven,看maven是如何帮助我们的

  • maven特性

下面的内容是从https://maven.apache.org/maven-features.html获取,英语阅读能力好的同学可以直接阅读,不好的同学忽略之,直接看我精简后的翻译

The following are the key features of Maven in a nutshell:

  • Simple project setup that follows best practices - get a new project or module started in seconds
  • Consistent usage across all projects - means no ramp up time for new developers coming onto a project
  • Superior dependency management including automatic updating, dependency closures (also known as transitive dependencies)
  • Able to easily work with multiple projects at the same time
  • large and growing repository of libraries and metadata to use out of the box, and arrangements in place with the largest Open Source projects for real-time availability of their latest releases
  • Extensible, with the ability to easily write plugins in Java or scripting languages
  • Instant access to new features with little or no extra configuration
  • Ant tasks for dependency management and deployment outside of Maven
  • Model based builds: Maven is able to build any number of projects into predefined output types such as a JAR, WAR, or distribution based on metadata about the project, without the need to do any scripting in most cases.
  • Coherent site of project information: Using the same metadata as for the build process, Maven is able to generate a web site or PDF including any documentation you care to add, and adds to that standard reports about the state of development of the project. Examples of this information can be seen at the bottom of the left-hand navigation of this site under the "Project Information" and "Project Reports" submenus.
  • Release management and distribution publication: Without much additional configuration, Maven will integrate with your source control system (such as Subversion or Git) and manage the release of a project based on a certain tag. It can also publish this to a distribution location for use by other projects. Maven is able to publish individual outputs such as a JAR, an archive including other dependencies and documentation, or as a source distribution.
  • Dependency management: Maven encourages the use of a central repository of JARs and other dependencies. Maven comes with a mechanism that your project's clients can use to download any JARs required for building your project from a central JAR repository much like Perl's CPAN. This allows users of Maven to reuse JARs across projects and encourages communication between projects to ensure that backward compatibility issues are dealt with.

maven常用特性

  1. 快速搭建java常见基础项目
  2. 第三方依赖jar包管理(maven中央仓库)
  3. 项目构建
  4. 模块管理
  5. maven插件
  • maven安装

maven本身是一个软件,可以安装在window、linux,最终通过给我们的是mvn这个命令,如果安装成功并且配置系统变量的话,那么应该可以看下如下结果

mvn -v
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00)
Maven home: F:\developtools\maven\apache-maven-3.5.3
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jre1.8.0_151
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

后续我们所有的使用都是基于此mvn命令以及对应的maven配置

maven安装包:https://maven.apache.org/download.cgi

记得安装之前要安装java,安装之后要配置mvn的系统变量

  • maven使用

IDE:IDEA

maven:3.5.3

java:1.7

创建一个web应用

without maven

使用IDE创建java原生项目,web项目

withmaven

1.创建maven官方的webapp项目,填写对应groupId和artifacId

2.查看项目

3.pom文件(pom文件是maven项目的标识,是maven的核心),顺着注释看

<?xml version="1.0" encoding="UTF-8"?>

<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>test</groupId>
  <artifactId>test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging><!-- 表示最终打包的格式为war包,即把java代码打包为xxx.war的格式,部署在tomcat容器中运行-->

  <name>test Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>  <!-- dependencies字段表示第三方依赖jar管理,要引用第三方jar包,添加此字段即可 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>  <!-- plugin字段表示maven打包用到的插件,maven的所有功能都是通过对应插件实现的,像编译、打包一般都属于默认插件 -->
    <finalName>test</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

4.打包项目,执行mvn clean install(clean install都是maven的phrase,clean表示会清楚上次构建内容,install表示构建),结果就是target目录生成构建后的war包

"C:\Program Files\Java\jdk1.8.0_151\bin\java" "-Dmaven.multiModuleProjectDirectory=F:\Users\lihao\IdeaProjects\springboot\lihao (1)\test" -Dmaven.home=F:\developtools\maven\apache-maven-3.5.3 -Dclassworlds.conf=F:\developtools\maven\apache-maven-3.5.3\bin\m2.conf "-javaagent:F:\Program Files\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar=54019:F:\Program Files\JetBrains\IntelliJ IDEA 2017.3.5\bin" -Dfile.encoding=UTF-8 -classpath F:\developtools\maven\apache-maven-3.5.3\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2017.3.5 -s F:\developtools\maven\apache-maven-3.5.3\conf\settings.xml -Dmaven.repo.local=F:\Users\lihao\.m2\repository clean install
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------------< test:test >------------------------------
[INFO] Building test Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ test ---
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\Users\lihao\IdeaProjects\springboot\lihao (1)\test\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ test ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\Users\lihao\IdeaProjects\springboot\lihao (1)\test\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ test ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ test ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:3.2.2:war (default-war) @ test ---
[INFO] Packaging webapp
[INFO] Assembling webapp [test] in [F:\Users\lihao\IdeaProjects\springboot\lihao (1)\test\target\test]
[INFO] Processing war project
[INFO] Copying webapp resources [F:\Users\lihao\IdeaProjects\springboot\lihao (1)\test\src\main\webapp]
[INFO] Webapp assembled in [124 msecs]
[INFO] Building war: F:\Users\lihao\IdeaProjects\springboot\lihao (1)\test\target\test.war
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ test ---
[INFO] Installing F:\Users\lihao\IdeaProjects\springboot\lihao (1)\test\target\test.war to F:\Users\lihao\.m2\repository\test\test\1.0-SNAPSHOT\test-1.0-SNAPSHOT.war
[INFO] Installing F:\Users\lihao\IdeaProjects\springboot\lihao (1)\test\pom.xml to F:\Users\lihao\.m2\repository\test\test\1.0-SNAPSHOT\test-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.105 s
[INFO] Finished at: 2018-12-25T22:47:12+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

5.将test.war放入tomcat或者其他容器中,这个服务就可以使用了~~

未完待续...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值