Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build maven environment quickly

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to build a maven environment quickly". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to build a maven environment quickly.

Maven is an open source project management tool written in pure Java. Maven uses a concept called project object model (POM) to manage the project. All the project configuration information is defined in a file called POM.xml, through which Maven can manage the entire declaration cycle of the project, including compilation, build, testing, release, reporting, and so on. At present, most of the projects under Apache have been managed by Maven. Maven itself also supports a variety of plug-ins, which can facilitate and more flexible control of the project.

1: download Maven 3.0.2 (Binary zip)

2: decompress to D:

3: configure environment variables

Set the variable MAVEN:

F:\ apache-maven-3.3.3

Set the Path path:

F:\ apache-maven-3.3.3\ bin

4: verify that the installation is successful

Enter: mvn-version; enter on the command line. If you see the following message, the installation is successful:

5: create a project

On the command line, enter: mvn archetype:create-DgroupId=com.mycompany.app-DartifactId=my-app enter

If you are running the command (goal) for the first time, maven will take some time to download the latest toolkit (Maven calls it artifacts) to your local repository.

After the command is executed, you will see that maven generates a directory called my-app, which is the artifactId you specified in the command. Enter this directory and you will find the following standard project structure:

The src/main/java directory contains the source code of the project, the src/test/java directory contains the test code of the project, and pom.xml is the project object model (Project Object Model or POM) of the project.

6:POM

The pom.xml file is maven's core configuration for a project, and this file will contain most of the configuration information about how you want to build the project. POM is large and complex, but you don't have to know it all, just use some common configurations. The contents of this POM are listed below:

Quote

Xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

Com.mycompany.app

My-app

1.0-SNAPSHOT

Jar

My-app

Http://maven.apache.org

UTF-8

Junit

Junit

3.8.1

Test

7: what did we do in step 5

Quote

Mvn archetype:create-DgroupId=com.mycompany.app-DartifactId=my-app

Execute the Maven command (goal) archetype:create, and set some parameters (- DgroupId=com.mycompany.app-DartifactId=my-app)

In this command, the prefix archetype is the plugin of a maven that contains the create command. This goal command builds a simple project based on the project prototype (the project template that conforms to the maven standard).

It is now safe to say that a maven plugin is a collection of goals commands with the same purpose, such as the jboss-maven-plugin plug-in, to handle various jboss-related tasks.

8:Build project

Enter: enter cd my-app on the command line to enter under the project path

Then enter mvn package enter and the command line will print out various actions and end with the following message:

With the command executed for the first time (that is a goal)

Quote

Archetype:create

Different, this time the execution is a simple command-package. Unlike goal, this is a phase (phase), a phase is a phase of the build life cycle, which refers to an ordered series of phase. When given a phase,Maven will execute all the phase before this stage and itself, for example, if we execute the compile phase, the actual phases are:

Quote

Validate

Generate-sources

Process-sources

Generate-resources

Process-resources

Compile

You can test the newly compiled and packaged jar package using the following command

Quote

Java-cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

The most classic will be printed at this time:

Quote

Hello World!

9: run the Maven tool

Although it is difficult to list a very comprehensive table, the most common default lifecycle phases can be listed here:

Quote

Validate: verify that the project is correct and that all required resources are available.

Compile: compile the source code of the project.

Test: use the appropriate unit test framework to test the compiled source code. These tests do not need to be packaged and deployed.

Package: package compiled code into a publishable format, such as jar.

Integration-test: if necessary, process and publish the package to an environment where integration testing can be conducted.

Verify: run all checks to verify that the package is valid and meets the quality standards.

Install: installs the package in the local repository and can be used as a dependency by other projects.

Deploy: executes in an integrated or release environment and copies the final version of the package to a remote repository so that other developers or projects can share it.

Clean: clears the previously built artifacts (in maven, all packages generated by the project are called artifact).

Site: generate a documentation site for the project.

Each Phases (phase) listed above actually corresponds to the potential goals, and the special goals executed by each phase is determined by the type of project, for example: if the project type is jar,package phase, the goals of jar:jar will be executed by default, if the project type is war, then the goals executed by package phase will be war:war.

The interesting thing to note is that phases and goals need to be executed in a certain order.

Mvn clean dependency:copy-dependencies package

This command clears the project, then copies the dependencies, and finally packages the project, of course, before it is packaged. Such as compile,test and so on.

Generate site

Mvn site

This phase generates project information based on the pom.xml configuration. You can see the generated documents in the target/site directory.

At this point, I believe you have a deeper understanding of "how to build the maven environment quickly". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report