博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
maven开始spring_如何开始使用Maven
阅读量:2521 次
发布时间:2019-05-11

本文共 12975 字,大约阅读时间需要 43 分钟。

maven开始spring

Maven is used very often in the industry and I felt it would be good to cover the basics in this article so that it can be used efficiently.

Maven在行业中经常使用,我觉得最好涵盖本文的基础知识,以便可以有效地使用它。

This article will cover things like maven basics, maven plugins, maven dependencies, and maven build lifecycle.

本文将介绍Maven基础知识,Maven插件,Maven依赖项和Maven构建生命周期。

什么是Maven (What is Maven)

Maven was created to provide a standard way in which Projects can be built. One of its powerful features is dependency management.

创建Maven是为了提供构建项目的标准方法。 它的强大功能之一是依赖管理。

Maven is commonly used for dependency management, but it is not the only thing it is capable of doing.

Maven通常用于依赖项管理,但它并不是唯一能够做到的事情。

If you do not know what dependency management means, don’t worry?. I will cover that in this article as well.

如果您不知道依赖管理的含义,不用担心吗? 我还将在本文中介绍这一点。

安装Maven (Installing Maven)

You can Install Maven from

您可以从安装Maven。

Also ensure Maven is set in the PATH so that mvn comands work.

还要确保在PATH中设置了Maven,以便mvn可用。

You can verify if it is installed and can be accessed using the command

您可以使用以下命令验证它是否已安装并且可以访问

mvn -v

Also ensure is set.

还要确保已设置 。

By default, Maven will use the jdk you provided in JAVA_HOME. This can be overridden, but for this article we will use the jdk provided in JAVA_HOME.

默认情况下,Maven将使用您在JAVA_HOME中提供的jdk。 这可以被覆盖,但是对于本文,我们将使用JAVA_HOME中提供的jdk。

创建您的Maven项目 (Create your Maven Project)

Normally an IDE like eclipse can be used to easily create maven projects. But in this artice I will be running the commands from the command line so that the steps are clearly understood.

通常,像eclipse这样的IDE都可以用来轻松创建maven项目。 但是在本文中,我将从命令行运行命令,以便清楚地理解这些步骤。

Run the following command to Create the project.

运行以下命令以创建项目。

mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.first.app -DartifactId=first-maven-app

Archetype in the above command is nothing but a sample project template. groupdId tells what group your project comes under and artifactId is the project name.

上面命令中的原型不过是一个示例项目模板。 groupdId告诉您的项目属于哪个组, artifactId是项目名称。

Once you run the above command, it may take maven a minute or so to download the necessary plugins and create the project.

一旦运行了上述命令,可能需要一分钟左右的时间来下载必要的插件并创建项目。

A folder called first-maven-app is now created. Open the folder and you will see a file called pom.xml

现在创建了一个名为first-maven-app的文件夹。 打开文件夹,您将看到一个名为pom.xml的文件。

pom.xml (pom.xml)

POM stands for Project Object Model. pom.xml has all the details about your project, and this is where you will tell maven what it should do.

POM代表项目对象模型。 pom.xml包含有关项目的所有详细信息,在这里您将告诉maven它应该做什么。

The contents of this file are shown below:

该文件的内容如下所示:

4.0.0
com.first.app
first-maven-app
jar
1.0-SNAPSHOT
first-maven-app
http://maven.apache.org
junit
junit
3.8.1
test

groupdId and artifactId are the same values we gave in the command line.

groupdIdartifactId是我们在命令行中提供的相同值。

packaging is the package format of the artifact. Default value is jar. It can have other values as well like ear, war, tar and so on.

包装是工件的包装格式。 默认值为jar 。 它可以具有其他值,例如ear,war,tar等。

version indicates the version number of the artifact. If SNAPSHOT is present, then it indicates the version is still in dev and may not be stable. If the version number does not have SNAPSHOT, then it’s the actual release version.

version指示工件的版本号。 如果存在SNAPSHOT ,则表明版本仍在开发中,可能不稳定。 如果版本号没有SNAPSHOT,则为实际发行版本。

name is the project name.

名称是项目名称。

I will explain about dependencies and plugins in Maven below.

我将在下面解释有关Maven中的依赖项和插件的信息。

超级POM (Super POM)

pom.xml as you can see is pretty small. The reason for this is that a lot of the configuration is present in something called Super POM which is maintained internally by Maven.

如您所见,pom.xml很小。 这是因为Maven内部维护了称为Super POM的许多配置。

pom.xml extends Super Pom to get all the config present in super Pom.

pom.xml扩展了Super Pom,以获取super Pom中存在的所有配置。

One of the config present in Super Pom indicates the following:

Super Pom中存在的一种配置指示以下内容:

  • All java source code is present inside src/main/java

    所有Java源代码都位于src / main / java内部

  • All java test code is present inside src/test/java

    所有Java测试代码都位于src / test / java内部

I mention only this config here, since we will be dealing with both source code as well as test code in this article.

我在这里仅提及此配置,因为在本文中我们将同时处理源代码和测试代码。

(Code)

The entire code discussed here is available in this repo:

此仓库中提供了此处讨论的整个代码: :

Let’s add some simple Java code. Create the following folder structure:

让我们添加一些简单的Java代码。 创建以下文件夹结构:

src/main/java/com/test/app/App.java

src / main / java / com / test / app / App.java

App.java is the Java code we will be adding.

App.java是我们将添加的Java代码。

Copy the following code into App.java:

将以下代码复制到App.java中:

package com.first.app;import java.util.List;import java.util.ArrayList;public class App {    public static void main( String[] args )    {        List
items = new ArrayList
(); items.add(1); items.add(2); items.add(3); printVals(items); System.out.println("Sum: "+getSum(items)); } public static void printVals(List
items){ items.forEach( item ->{ System.out.println(item); }); } public static int getSum(List
items){ int sum = 0; for(int item:items){ sum += item; } return sum; }}

This is simple code which has 2 functions.

这是简单的代码,具有2个功能。

But one thing to observe is, the code is using lambda expressions inside the forEach loop in printVals function.

但是要观察的一件事是,代码在printVals函数的forEach循环内使用了lambda表达式。

Lambda expressions need at minimum Java 8 to run. But by default Maven 3.8.0 runs using Java version 1.6.

Lambda表达式至少需要Java 8才能运行。 但是默认情况下,Maven 3.8.0使用Java 1.6版运行。

So we need to tell maven to use Java 1.8 instead. In order to do this we will use Maven Plugins.

因此,我们需要告诉Maven使用Java 1.8。 为此,我们将使用Maven插件。

Maven插件 (Maven Plugins)

We will use the Maven Compiler Plugin to indicate which Java version to use. Add the following lines to pom.xml:

我们将使用Maven编译器插件来指示要使用的Java版本。 将以下行添加到pom.xml中:

...
org.apache.maven.plugins
maven-compiler-plugin
3.8.0
1.8
1.8
...

You can see that the Java source and target versions are set to 1.8.

您可以看到Java 版本和目标版本设置为1.8

Plugins basically get some action done in maven. The compiler plugin compiles the source files.

插件基本上可以在Maven中完成一些操作。 编译器插件编译源文件。

The full pom.xml is available .

完整的pom.xml 可用。

There are a lot of maven plugins available. By knowing how to use plugins well, Maven can be used to do amazing things. ?

有很多可用的Maven插件。 通过了解如何很好地使用插件,Maven可以用来做令人惊奇的事情。

Maven依赖 (Maven Dependencies)

Normally while writing code, we will be using a lot of existing libraries. These existing libraries are nothing but dependencies. Maven can be used to manage dependencies easily.

通常,在编写代码时,我们将使用许多现有的库。 这些现有的库不过是依赖项。 Maven可用于轻松管理依赖关系。

In the pom.xml of our project you can see the following dependency:

在我们项目的pom.xml中,您可以看到以下依赖关系:

junit
junit
3.8.1
test

This dependency is telling that we will be needing junit. Junit is used to write Unit Tests for Java code. Similarly a lot of other dependencies can be added.

这种依赖性表明我们将需要junit 。 Junit用于编写Java代码的单元测试。 同样,可以添加许多其他依赖项。

Let’s say you want to handle JSON in the code. Then you can add the gson dependency as shown below:

假设您要在代码中处理JSON。 然后,您可以添加gson依赖项,如下所示:

com.google.code.gson
gson
2.8.5

You can search for Maven artifacts in

您可以在搜索Maven工件。

传递依存关系 (Transitive Dependencies)

Let’s say you add a dependency A to the Project. Now A depends on a dependency called B. B depends on a dependency called C.

假设您向项目添加了依赖项A。 现在A依赖于称为B的依赖项。 B依赖于称为C的依赖项。

Since you are using A in the project, you will also need B and C.

由于您在项目中使用A ,因此您还将需要BC。

But fortunately, it is enough if you add only A in pom.xml. Because Maven can figure out that A depends on B and that B depends on C. So internally Maven will automatically download B and C.

但是幸运的是,仅在pom.xml中添加A就足够了。 因为Maven可以确定A依赖于B,而B依赖于C。所以Maven在内部将自动下载BC。

Here B and C are transitive dependencies.

这里BC是传递依存关系。

自定义Maven存储库 (Custom Maven Repository)

All these dependencies are available in a Public Maven Central Repository

所有这些依赖项都可以在Public Maven中央存储库中找到,网址为

It is possible that there are some artifacts which are private to your company. In this case, you can maintain a private maven repository within your organization. I won’t be covering this portion in this tutorial.

可能存在一些对您的公司私有的工件。 在这种情况下,您可以在组织内维护私有Maven存储库。 我不会在本教程中介绍这一部分。

添加测试类 (Adding the test class)

Since the junit dependency is present in the project, we can add test Classes.

由于junit依赖项存在于项目中,因此我们可以添加测试类。

Create the following folder structure:

创建以下文件夹结构:

src/test/java/com/test/app/AppTest.java

src / test / java / com / test / app / AppTest.java

AppTest.java is the Test Class.

AppTest.java是测试类。

Copy the following code into AppTest.java:

将以下代码复制到AppTest.java中:

package com.first.app;import junit.framework.TestCase;import java.util.List;import java.util.ArrayList;public class AppTest extends TestCase{    public AppTest( String testName )    {        super( testName );    }    public void testGetSum()    {        List
items = new ArrayList
(); items.add(1); items.add(2); items.add(3); assertEquals( 6, App.getSum(items) ); }}

This class tests the getSum() function present in the App Class.

此类测试App类中存在的getSum()函数。

Maven构建生命周期和阶段 (Maven Build Lifecycle and Phases)

Maven follows a build lifecycle to build and distribute artifacts. There are three main lifecycles:

Maven遵循构建生命周期来构建和分发工件。 有三个主要生命周期:

  1. Default lifecycle: This deals with building and deploying the artifact.

    默认生命周期 :这涉及构建和部署工件。

  2. Clean lifecycle: This deals with project cleaning

    清洁生命周期 :涉及项目清洁

  3. Site lifecycle: This deals with Site documentation. Will cover this in a different article.

    网站生命周期 :这涉及网站文档。 将在另一篇文章中介绍。

A Lifecycle is made up of phases. Here are some of the important phases in the default lifecycle:

生命周期由阶段组成。 以下是默认生命周期中的一些重要阶段

  • validate: Checks if all necessary information is available for the project

    validate :检查项目是否需要所有必要信息

  • compile: Used to compile the source files. Run the following command to compile:

    compile :用于编译源文件。 运行以下命令进行编译:

mvn compile
  • After running this command, a folder called target is created with all the compiled files.

    运行此命令后,将使用所有编译文件创建一个名为target的文件夹。
  • test: Used to run all the unit tests present in the project. This is why the Junit dependency was needed. Using Junit, unit tests can be written. Test classes can be run using the command

    测试 :用于运行项目中存在的所有单元测试。 这就是为什么需要Junit依赖项的原因。 使用Junit,可以编写单元测试。 可以使用以下命令运行测试类

mvn test
  • package: This will run all the above phases and then package the artifact. Here it will package it into a jar file since pom indicates a jar is needed. Run the following command for this:

    package :这将运行上述所有阶段,然后打包工件。 由于pom指示需要一个jar,因此它将打包到jar文件中。 为此,运行以下命令:

mvn package
  • The jar file is created inside the target folder

    jar文件在目标文件夹中创建

  • verify: This will ensure that quality criteria is met in the project

    验证 :这将确保项目符合质量标准

  • install: This will install the package in a local repository. The local repository location is usually ${user.home}/.m2/repository. Use the following command for this:

    install :这会将软件包安装在本地存储库中。 本地存储库位置通常为$ {user.home} /。m2 / repository 。 为此使用以下命令:

mvn install
  • deploy: This is used to deploy the package to a remote repository

    deploy :用于将程序包部署到远程存储库

One more command which is commonly used is the clean command which is given below:

另一个常用的命令是clean命令,如下所示:

mvn clean

This command cleans up everything inside the target folder

此命令清除目标文件夹中的所有内容

参考文献 (References)

Maven’s Offical Guide:

Maven的官方指南: :

More about POM :

有关POM的更多信息: :

More about Build Lifecycle :

有关构建生命周期的更多信息: :

恭喜😄 (Congrats 😄)

You know how to use Maven now. This article covered just the basics of pom, plugins, dependencies and build lifecycle. To know more about Maven check the links I have given above.

您知道现在如何使用Maven。 本文仅介绍pom,插件,依赖项和构建生命周期的基础知识。 要了解有关Maven的更多信息,请查看我上面给出的链接。

Happy Coding 😄

快乐编码😄

关于作者 (About the author)

I love technology and follow the advancements in the field. I also like helping others with my technology knowledge.

我热爱技术,并关注该领域的进步。 我也喜欢用我的技术知识来帮助他人。

Feel free to connect with me on my LinkedIn account

随时使用我的LinkedIn帐户与我联系

You can also follow me on twitter

您也可以在Twitter上关注我

My Website:

我的网站: :

Originally published at .

最初发布在 。

翻译自:

maven开始spring

转载地址:http://qywzd.baihongyu.com/

你可能感兴趣的文章
Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决
查看>>
第2天线性表链式存储
查看>>
python自动化测试-D11-学习笔记之一(yaml文件,ddt)
查看>>
mysql存储过程使用游标循环插入数据
查看>>
Ubuntu 12.04 添加新用户并启用root登录
查看>>
20145309信息安全系统设计基础第9周学习总结上
查看>>
c# 字段、属性get set
查看>>
td内容超出隐藏
查看>>
Spring CommonsMultipartResolver 上传文件
查看>>
Settings app简单学习记录
查看>>
SQLAlchemy
查看>>
多线程
查看>>
使用缓存的9大误区(下)转载
查看>>
appium键值对的应用
查看>>
MyEclipse 8.X 通用算法
查看>>
selenium.Phantomjs设置浏览器请求头
查看>>
分布式数据库如何选择,几种分布式数据库优缺点一览
查看>>
BZOJ 4443: 小凸玩矩阵【二分图】
查看>>
苹果 OS X制作u盘启动盘
查看>>
Jquery便利对象
查看>>