maven构建java项目_使用Maven进行Java构建自动化

maven构建java项目什么是Apache Maven?(What is Apache Maven?)Apache Maven, is a software project management, and comprehension tool, based on the concept of a project object model, or POM. Maven can manage a ...
摘要由CSDN通过智能技术生成

maven构建java项目

什么是Apache Maven?(What is Apache Maven?)

Apache Maven, is a software project management, and comprehension tool, based on the concept of a project object model, or POM. Maven can manage a project’s build, reporting, and documentation from a central piece of information.

Apache Maven是基于项目对象模型(POM)的概念的软件项目管理和理解工具。 Maven可以从中央信息管理项目的构建,报告和文档。

A more comprehensive definition of Apache Maven, is that Maven is a project management tool, which encompasses a project object model. It follows a set of standards, it includes a project life cycle, a dependency management system, and logic for executing plugin goals at defined phases in a life cycle. Maven is designed to provide a simple project setup, that uses best practices as a guide.

Apache Maven的更全面定义是Maven是一个项目管理工具,其中包含项目对象模型。 它遵循一组标准,包括项目生命周期,依赖管理系统以及用于在生命周期中定义的阶段执行插件目标的逻辑。 Maven旨在提供一个简单的项目设置,以最佳实践为指导。

With Maven, your projects follow a consistent structure. Projects become IDE agnostic, by enforcing a consistent structure, it makes modifications easier in the future, when new developers are introduced to the project. It also ensures that programmers always get the most recent version of compilers, etc.

使用Maven,您的项目遵循一致的结构。 通过强制使用一致的结构,项目变得与IDE无关,从而在将来向新开发人员介绍该项目时使修改变得更容易。 它还可以确保程序员始终获得最新版本的编译器等。

Most Java projects rely on other projects, and open source frameworks, to function properly. It can be cumbersome to download these dependents manually, and keep track of their versions, as you use them in your project. Maven provides a convenient way to declare these project dependencies, in a separate, external, POM.XML file. It then automatically downloads these dependencies and allows you to use them in your project. This simplifies project dependency management greatly. It is important to note, that in the POM.XML file, you specify the what, and not the how. The POM.XML file, can also serve as documentation tool, conveying your project dependencies and their versions. Software developers refer to Maven, as a build tool. Since it is used to build deployable artifacts from source code. On the other hand, if you asked a project manager they might call it a project management tool, since it follows a development life cycle. In reality, it is both.

大多数Java项目都依赖于其他项目和开源框架来正常运行。 在项目中使用它们时,手动下载这些依赖项并跟踪其版本可能很麻烦。 Maven在一个单独的外部POM.XML文件中提供了一种方便的方法来声明这些项目依赖项。 然后,它会自动下载这些依赖项,并允许您在项目中使用它们。 这大大简化了项目依赖管理。 重要的是要注意,在POM.XML文件中,您指定内容而不是方式。 POM.XML文件还可以用作文档工具,传达您的项目依赖项及其版本。 软件开发人员将Maven称为构建工具。 因为它用于从源代码构建可部署的工件。 另一方面,如果您问项目经理,他们可能会称其为项目管理工具,因为它遵循开发生命周期。 实际上,两者都是。

下载Maven (Download Maven)

To get started using Apache Maven, start by downloading Maven from the maven.apache.org website. From this page we can just use the link in the middle where it says Use Download. Always make sure you download the latest version.

要开始使用Apache Maven,请先maven.apache.org网站下载Maven。 在此页面上,我们仅需使用中间链接“使用下载”即可。 始终确保下载最新版本。

在Windows上安装Maven (Install Maven on Windows)

Now that we have the Maven file downloaded and extracted into our program files directory, we can go ahead and install Maven on our Windows machine. As I stated earlier, the Maven download is not very large. That’s because Maven’s power is included in its plugins which are located and retrieved from a central repository on an as-needed basis and allowing for greater code reuse. Before the installation we must verify our Java version from the command line using java -version. Remember it must be 1.7 or higher.

现在我们已经下载了Maven文件并将其解压缩到我们的程序文件目录中,我们可以继续在Windows机器上安装Maven了。 如前所述,Maven下载不是很大。 这是因为Maven的功能包含在其插件中,这些插件可根据需要从中央存储库中定位和检索,并允许更大的代码重用。 在安装之前,我们必须使用java -version从命令行验证Java版本。 请记住,它必须是1.7或更高。

  1. Install JDK and Add ‘JAVA_HOME’ Environment Variable.To install java, download JDK installer and add/update the JAVA_HOME variable to JDK install folder.

    安装JDK并添加'JAVA_HOME'环境变量。要安装Java,请下载JDK安装程序,并将JAVA_HOME变量添加/更新到JDK安装文件夹。

  2. Download Maven and add ‘MAVEN_HOME’ and ‘M2_HOME’ Environment Variables. Maven can be downloaded from this location. Set the M2_HOME and MAVEN_HOME variable to maven installation folder.

    下载Maven并添加“ MAVEN_HOME”和“ M2_HOME”环境变量。 可以从此位置下载Maven。 将M2_HOME和MAVEN_HOME变量设置为maven安装文件夹。

3. Include ‘maven/bin’ directory in ‘PATH’ variable. To run maven from command prompt, this is necessary. Update the PATH variable with ‘Maven-installation/bin’ directory.

3.在“ PATH”变量中包括“ maven / bin”目录。 要从命令提示符运行maven,这是必需的。 使用“ Maven-installation / bin”目录更新PATH变量。

4. Verify maven in console:

4.在控制台中验证Maven:

Maven installation is complete. Now lets test it from windows command prompt.

Maven安装完成。 现在让我们从Windows命令提示符下对其进行测试。

  1. Go to start menu and type cmd in application location search box.

    转到开始菜单,然后在应用程序位置搜索框中键入cmd。
  2. Press ENTER. A new command prompt will be opened.

    按ENTER。 将打开一个新的命令提示符。
  3. Type mvn -version in command prompt and hit ENTER.

    在命令提示符下键入mvn -version,然后按Enter。

$ mvn -version

$ mvn-版本

Maven与代理(Maven with proxy)

To set Maven Proxy :

设置Maven代理

Edit the proxies session in your Home Folder — C:/Users/{UserName}/.m2/settings.xml file. If you cant find the file, create one.

主文件夹中编辑代理会话-C:/ Users / {UserName} /。m2 / settings.xml文件。 如果找不到该文件,请创建一个。

or

要么

Edit the proxies session in your {M2_HOME}/conf/settings.xml

{M2_HOME} /conf/settings.xml中编辑代理会话

You can go through the Official documentation

您可以阅读官方文档

If the settings file changes don’t work, try this in the command prompt having the POM file.

如果设置文件更改不起作用,请在具有POM文件的命令提示符下尝试此操作。

mvn install -Dhttp.proxyHost=abcproxy -Dhttp.proxyPort=8080 -Dhttps.proxyHost=abcproxy -Dhttps.proxyPort=8080

IDE整合 (IDE integration)

If you are accustomed to using Eclipse, then you wanna use the Eclipse version M2Eclipse, which is specifically designed for Maven integration. This version of Eclipse includes the ability to launch Maven builds, handle dependency management based on Maven’s pom.xml file, automatic download of required dependencies, and wizards for creating new project and search capabilities for Maven remote repositories. https://www.eclipse.org/m2e/ is the homepage for this version of the M2Eclipse website. As you can see, this version of Eclipse is designed specifically for integration of Apache Maven.

如果您习惯于使用Eclipse,那么您想使用专门为Maven集成设计的Eclipse版本M2Eclipse。 此版本的Eclipse具有启动Maven构建,基于Maven的pom.xml文件处理依赖项管理,自动下载所需依赖项以及创建用于Maven远程存储库的新项目和搜索功能的向导的功能。 https://www.eclipse.org/m2e/是此版本的M2Eclipse网站的主页。 如您所见,此版本的Eclipse专为集成Apache Maven而设计。

项目对象模型(POM) (Project Object Model (POM))

Maven use of the concept of a Project Object Model, or POM. This model has a a set of standards, a project life-cycle, a dependency management system, and logic for executing plugin goals at certain phases in the life-cycle process. One of the things that makes Maven so powerful is that it relies on the concept that projects are set up with default behaviors. For example, the pom.xml file is always located in the base directory. The source code must be in a certain directory. Resources necessary for the project are in a another folder or directory. Test cases are in a specifically named folder. And a target folder is always created that’s used for the final JAR file. This folder structure is an important example of how Maven has adopted convention over configuration. By always using a standard folder structure, it allows developers to concentrate on coding. Once the code and resources are placed in the correct directories, and the POM file is updated. Maven handles the rest.

Maven使用项目对象模型或POM的概念。 该模型具有一组标准,项目生命周期,依赖性管理系统以及用于在生命周期过程中某些阶段执行插件目标的逻辑。 使Maven如此强大的原因之一是,它依赖于使用默认行为设置项目的概念。 例如,pom.xml文件始终位于基本目录中。 源代码必须位于某个目录中。 项目所需的资源位于另一个文件夹或目录中。 测试用例位于专门命名的文件夹中。 并且始终创建用于最终JAR文件的目标文件夹。 此文件夹结构是Maven如何采用约定而非配置的重要示例。 通过始终使用标准的文件夹结构,它使开发人员可以专注于编码。 一旦将代码和资源放置在正确的目录中,并且POM文件将更新。 Maven处理其余的事情。

A project model includes:

项目模型包括:

  • A project description

    项目说明
  • A unique set of coordinates

    一组独特的坐标
  • Project attributes

    项目属性
  • The project’s license information.

    项目的许可证信息。
  • The project version,

    项目版本,
  • Any authors or contributors to the project

    该项目的任何作者或贡献者
  • A list of project dependencies.

    项目依赖项列表。

Before we go further, let’s take a look at a sample POM file.

在继续之前,让我们看一个示例POM文件。

<?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/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.ucguy4u</groupId>
   <artifactId>datastructures</artifactId>
   <packaging>jar</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>datastructures</name>
   <url>http://maven.apache.org</url>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
               <execution>
                  <id>attach-javadocs</id>
                  <goals>
                     <goal>jar</goal>
                  </goals>
               </execution>
            </executions>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值