[maven] installation, use and common commands

Install

windows

  • Download the Maven binary file
    Go to the Apache Maven official website (https://maven.apache.org), find the latest stable version, and then download the corresponding binary compressed package (such as apache-maven-3.8.2-bin.zip).
  • Unzip the file
    Unzip the downloaded zip file to a directory of your choice, such as "C:\Program Files" or "C:\opt".
  • Configure environment variables
    Add Maven to system environment variables so it can run anywhere. Open "Control Panel" -> "System and Security" -> "System" -> "Advanced System Settings" -> "Environment Variables". In the "System Variables" section, find the "Path" variable and click "Edit". Add "path to Maven installation directory\bin" at the end of the variable value, for example: "C:\Program Files\apache-maven-3.8.2\bin". Then click "OK" to save the changes.
  • Verify installation
    Open a command prompt (cmd.exe) or PowerShell, and enter the following command to verify that Maven is successfully installed:
mvn -version

If Maven's version number and other details are displayed correctly, the installation was successful.
insert image description here
At this point, you have successfully installed Maven on Windows. You can use Maven to build and manage Java project dependencies, compile code, and execute other Maven commands.

Linux

ubuntu

Assuming you are using Ubuntu:

  • Update the package index
    Open the terminal and run the following command to update the package index of the system:
sudo apt update

Install Maven: Run the following command to install Maven:

sudo apt install maven

Verify the installation: After the installation is complete, you can verify that Maven was successfully installed by entering the following command:

mvn -version

If Maven's version number and other details are displayed correctly, the installation was successful.
insert image description here
At this point, you have successfully installed Maven on Ubuntu. You can use Maven to build and manage Java project dependencies, compile code, and execute other Maven commands. Please note that the specific installation method may vary depending on the Linux distribution, the above steps are applicable to the Ubuntu system. For other Linux distributions, please refer to the corresponding documentation or official guide for installation.

hundreds

The following is the general process for installing Maven on CentOS:

  • Update your system
    Open a terminal and run the following command to make sure your system is updated:
sudo yum update
  • Installing Java JDK
    Maven requires a Java environment. If Java JDK is not installed, run the following command to install OpenJDK:
sudo yum install java-1.8.0-openjdk-devel
  • Download the Maven binaries
    Go to the official Apache Maven website (https://maven.apache.org), find the latest stable version, and copy the download link.

  • Download Maven using wget
    Run the following command in terminal, replacing the download link with your actual link:

wget [Maven下载链接]
  • Extract the file
    Run the following command to extract the downloaded file (assuming the downloaded file is named apache-maven-3.8.2-bin.tar.gz):
tar xf apache-maven-3.8.2-bin.tar.gz
  • Move the files
    Move the unzipped folder to an appropriate location, for example the /opt directory:
sudo mv apache-maven-3.8.2 /opt/
  • Configure environment variables
    Edit the /etc/profile file and add the following content (use the actual Maven installation path):
export MAVEN_HOME=/opt/apache-maven-3.8.2
export PATH=$MAVEN_HOME/bin:$PATH
  • Make environment variables take effect
    Run the following command to make the modified environment variables take effect:
source /etc/profile

Verify installation: Enter the following command in the terminal to verify that Maven is successfully installed:

mvn -version

If Maven's version number and other details are displayed correctly, the installation was successful.
insert image description here
So far, you have successfully installed Maven on CentOS. You can use Maven to build and manage Java project dependencies, compile code, and execute other Maven commands. Please note that the specific installation method may vary depending on the Linux distribution, the above steps are applicable to the CentOS system. For other Linux distributions, please refer to the corresponding documentation or official guide for installation.

Common commands

The following are some common commands of Maven

mvn clean: 清理项目,删除生成的目标文件和构建结果。
mvn compile: 编译项目的源代码。
mvn test: 运行项目的单元测试。
mvn package: 打包项目,生成可部署的构建输出文件(如JAR、WAR等)。
mvn install: 将项目构建结果安装到本地Maven仓库,以供其他项目依赖使用。
mvn deploy: 将项目构建结果部署到远程Maven仓库,以供其他开发人员或项目使用。
mvn clean install: 清理项目并执行安装操作,常用于构建和部署项目。
mvn clean package: 清理项目并执行打包操作,常用于构建可部署文件。
mvn dependency:tree: 显示项目的依赖树,包括所有直接和间接依赖项。
mvn dependency:resolve: 解析项目的依赖项,确保它们都被正确下载和配置。
mvn help:system: 显示有关Maven系统信息的帮助文档,包括配置和环境设置。
这只是一些常用的Maven命令示例,Maven还有许多其他功能和插件,可以根据具体需要使用不同的命令来完成特定的任务。你可以在Maven的官方文档中找到更详细的命令和用法说明。

, the real scenario needs to filter unit tests, you can execute the following command

mvn clean install/package -DskipTests

At the same time, you can use the following command to push the specified jar to the corresponding warehouse

mvn deploy:deploy-file -DgroupId=[groupId] -DartifactId=[artifactId] -Dversion=[version] -Dfile=[xxx.jar] -DrepositoryId=[repositoryId] -Durl=[xxxx/xxx-release/]

You can also use the following command to view the current commands supported by mvn:

mvn --h 或者 mvn -help

insert image description here

Precautions

When using Maven, you need to pay attention to the following aspects:

  • Configure the correct Maven version
    Make sure to use the latest stable version of Maven that is compatible with your project.

  • Configure the correct Java version
    Make sure the proper Java JDK is installed and matches the <maven.compiler.source> and <maven.compiler.target> settings in the Maven configuration file (pom.xml).

  • Confirm the project structure and directory
    According to the Maven convention, ensure that the source code of the project is placed in the correct directory structure, for example, the Java source code is placed in the src/main/java directory, and the resource files are placed in the src/main/resources directory.

  • Clear dependency management
    Clearly declare and manage project dependencies in the pom.xml file, including dependent libraries, plugins and other project modules. Make sure dependencies are properly versioned and scoped to avoid potential conflicts or inconsistencies.

  • Use appropriate lifecycle commands
    Maven provides multiple lifecycle phases (like clean, compile, test, package, etc.). Select the appropriate lifecycle phase to perform actions as needed to ensure that the project is properly built and tested.

  • Caching and Update Mechanism
    Maven caches downloaded dependencies and plugins to speed up the subsequent build process. When necessary, mvn clean can be used to clear the cache and re-download dependencies.

  • Note Maven Plugins
    Maven has a rich ecosystem of plugins that extend its functionality. Learn about commonly used plugins and pay attention to how they are configured and used to meet the specific needs of your project.

  • Networking and Mirroring Setup
    Maven needs to communicate with the central repository to download dependencies. Make sure that the network connection is normal, or configure the mirror of the local or private warehouse as needed.

  • Update Maven and dependencies
    in time Regularly check and update the version of Maven and project dependencies to obtain the latest functions and fix bugs.

  • Read Documentation and Learning Resources
    Maven has extensive documentation and learning resources available for reference. In-depth understanding of Maven's concepts, conventions, and best practices can make better use of its functions and improve development efficiency.

Guess you like

Origin blog.csdn.net/qq_38428623/article/details/131519997