YuezhenQin / javaweb

0 stars 0 forks source link

Maven (mvnw, mvnw.cmd) #3

Open YuezhenQin opened 9 months ago

YuezhenQin commented 9 months ago

What is Maven?

Maven is one of the most commonly-used tools for building and managing Java-based projects. The word maven means "accumulator of knowledge" in Yiddish. Maven provides a unified project structure, describes how a project is built, and describes its dependencies.

There are two main build tools for Java-based projects: Apache Maven, and Gradle. A build tool is a program that automates the process of creating executable applications from source code. The build process includes compiling sources and linking and packaging the code into a usable or executable form.

An important thing about Maven is that it is used in a declarative way, meaning that you describe WHAT should be done instead of HOW to do something. This greatly simplifies project management.

Maven makes the day-to-day work of Java developers easier and generally helps with the comprehension of any Java-based project written in Java, Kotlin, Scala, or other languages. The full list of Maven features can be found here. If you want more background information on Maven you can read The Philosophy of Maven and The History of Maven. https://hyperskill.org/learn/step/4282

YuezhenQin commented 9 months ago

The key concepts

The key features of Maven are as follows:

YuezhenQin commented 9 months ago

The simplest project with Maven

Project coordinates

Every project managed by Maven is uniquely identified by three coordinates:

These three values make up the full project name according to the following rule: \<groupId>:\<artifactId>:\<version>

A version may contain an additional word like FINAL or SNAPSHOT. Do not worry about this for now; we will learn more about it later.

There is another option packaging that determines the type of the resulting artifact (file): jar, pom, maven-plugin, war, etc. When no packaging is declared, Maven assumes it is jar by default.

Generating the simplest project

1. mvn archetype:generate "-DarchetypeArtifactId=maven-archetype-quickstart" "-DarchetypeVersion=1.3" "-DgroupId=com.hyperskill" "-DartifactId=first-maven-app" "-DinteractiveMode=false"

This command will start generating a project based on the archetype maven-archetype-quickstart; groupId is specified as com.hyperskill, and the artifactId is set to first-maven-app.

2. mvn clean install

3. java -cp target/first-maven-app-1.0-SNAPSHOT.jar com.hyperskill.App

Basic project structure

The generated project has a standard project structure:

first-maven-app ├── pom.xml └── src ├── main │ └── java │ └── com │ └── hyperskill │ └── App.java └── test └── java └── com └── hyperskill └── AppTest.java

The root directory is first-maven-app; it has the same name as the artifactId. It includes the pom.xml file and the src directory.

The pom.xml is a file that describes this project (POM: Project Object Model). All Maven-based projects contain this file.

The src directory contains all of the source material needed to build the project. The directory src/main/java contains the project source code, and the src/test/java contains the source code of tests. In both cases, .java files are located inside the package com.hyperskill that is the same as the groupId.

Maven also created the sample Java source code file called App.java. It just prints HelloWorld to the standard output.

The POM file

pom.xml is the basic unit of work in Maven. It contains the Project Object Model (POM) that provides all the information required to build a project. In our project, the file contains a small piece of information. But in reality, it can be huge.

Remember that the minimal requirements for POM are project, modelVersion, and three project coordinates: groupId, artifactId, and version.

YuezhenQin commented 8 months ago

Maven build lifecycle

The Maven build lifecycle is based on the sequence of phases. A phase represent a specific stage of the build. Each phase performs a specific set of actions necessary for a successful project build. The build lifecycle includes three main sets of phases: default, clean, and site.

Image

default manages project deployments:

validate: validates if the project is correct and all necessary information is available; compile: compiles the source code; test: tests the compiled source code using a suitable unit testing framework; package: takes the compiled code and package it in JAR or WAR; verify: checks the integrity and correctness of the project; install installs the package into the local repository to use as a dependency in other projects locally; deploy in the build environment, copies the final package to the remote repository.

clean is used to clean the project:

pre-clean performs additional operations before cleaning the project; clean deletes all files generated with the previous build from the project; post-clean performs additional operations after cleaning the project.

Finally, site is used to create the project's site documentation:

pre-site executes the necessary processes prior to the actual project site generation; site generates the project's site documentation; post-site executes processes necessary to finalize the site generation; site-deploy deploys the generated site documentation to the specified web server.

YuezhenQin commented 8 months ago

Building and starting the project

To build a Maven-based project, you should be inside the root directory of your project, at the same level as the pom.xml file. There are several ways to do that:

Building a project without cleaning the project and without running tests: mvn compile Building a project without cleaning the project, but running tests: mvn package Cleaning a project and then building (with tests): mvn clean package Using install Instead of package: mvn clean install

YuezhenQin commented 6 months ago

Dependency Management

Dependency management is a core feature of Maven. There are two types of dependencies in Maven: direct and transitive.

Direct dependencies are the ones that we explicitly include in the project. These can be included using tags.

Transitive dependencies are required by direct dependencies. Maven automatically includes required transitive dependencies in our project.

We can list all dependencies in the project using mvn dependency:tree.

source: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

YuezhenQin commented 5 months ago

Maven 常用命令

安装 .jar mvn install

YuezhenQin commented 5 months ago

maven 与插件配合差 maven wrapper: maven 高/低版本切换