In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to install maven". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
About maven 1.1What is Maven
Maven is a project management and integrated tool. Maven provides developers to build a complete lifecycle framework. The developer team can automate the basic tooling of the project, and Maven uses a standard directory structure and default build lifecycle.
In multiple developer team environments, Maven can be set to complete the configuration in a very short time according to the standard. Because most projects are simple to set up and reusable, Maven makes it easier for developers to create reports, review, build, and test automation settings.
Students who have used GitHub should feel deja vu here. Yes, the functions of Maven and git are very similar, both for the convenience of project creation and management.
In a nutshell, Maven simplifies and standardizes the project construction process. Handle seamless connections for compilation, allocation, documentation, teamwork, and other tasks. Maven increases reusability and is responsible for establishing related tasks.
1.2 History of Maven
Maven was designed to simplify the construction of Jakarta Turbine projects. In several projects, each project contains a different Ant build file. CVS detected by JAR. Apache organizes the development of Maven to set up multiple projects, publish project information, deploy projects, and provide teamwork and assistance in several projects with JAR files.
Maven experienced the development of Maven- > Maven2-> Maven3.
1.3Why use Maven?
Before Maven, we often used Ant to build Java projects, and then Ant is just a build tool, it does not manage the project dependencies in the project and the project itself, and Ant as a build tool does not eliminate the repetition of software build, because different projects need to write corresponding Ant tasks.
As a newcomer, Maven inherits the project construction function of Ant, and provides the functions of dependency and project management, so it is a project management and comprehensive tool. Its core functions of dependency management, project information management, central warehouse, and the core functions of contract greater than configuration make Maven a standard choice for current Java project construction and management tools.
There are many reasons to learn Maven:
Mainstream IDE (Eclipse,IDEA,Netbean) has built-in Maven.
SpringFramework no longer provides downloads for jar, but directly through Maven for dependent downloads.
In github, almost all popular Java projects in the open source community are built and managed through Maven.
Maven novice introduction to Maven concept
As a build tool, Maven can not only help us automate the build, but also abstract the build process and provide the implementation of build tasks; it provides a consistent operation interface across platforms, which makes it an excellent and popular build tool.
Maven is not only a build tool, but also a dependency management tool and project management tool. It provides a central repository to help me download components automatically.
Installation of maven
One: since I am a window system, I only introduce how to install it under window. Before installing Maven, make sure that JDK.
Second: then go to the Maven official website download interface to download the desired version and decompress it to the directory you want.
Third: finally, set the environment variable to configure the Maven installation to the operating system environment, mainly to configure M2_HOME and PATH, as shown in the figure
After all is done, verify it. Open doc and enter mvn-v. How to get the following information indicates that the configuration is successful.
Maven directory
Bin directory:
This directory contains scripts that mvn runs to configure java commands, prepare classpath and related Java system properties, and then execute Java commands.
Boot directory:
The directory contains only one file, which is plexus-classworlds-2.5.2.jar. Plexus-classworlds is a classloader framework that provides richer syntax for configuration than the default java classloader, which Maven uses to load its own class libraries.
Conf directory:
This directory contains a very important file, settings.xml. Directly modify the file, you can globally customize the behavior of Maven on the machine, in general, we prefer to copy the file to the ~ / .m2 / directory (~ represents the user directory), and then modify the file to customize the behavior of Maven in the user scope.
Lib directory:
This directory contains all the Java class libraries needed by the Maven runtime, and Maven itself is developed in modules, so users can see files such as maven-core-3.0.jar, maven-model-3.0.jar, and so on, as well as some third-party dependencies used by Maven, such as commons-cli-1.2.jar, commons-lang-2.6.jar, and so on.
Maven common command description mvn clean: means to run the cleanup operation (the data in the target folder will be cleaned by default). Mvn clean compile: means to run cleanup first and then compile, which compiles the code into the target folder. Mvn clean test: run cleanup and testing. Mvn clean package: run cleanup and packaging. Mvn clean install: running cleanup and installation will install the packed package to the local repository so that other projects can call it. Mvn clean deploy: run cleanup and publish (post to private server).
Most of the above commands are written in conjunction, and you can also execute them separately, which is alive. Depending on personal preferences and usage requirements, Eclipse Run as will provide commonly used commands for maven projects.
Maven uses 4.0.0 com.tengj springBootDemo1 0.0.1-SNAPSHOT springBootDemo1
The first line of code is the XML header, which specifies the version and encoding of the xml document.
Project is the root element of all pom.xml, and it also declares some POM-related namespaces and xsd elements.
The first child element under the root element, modelVersion, specifies the version of the current POM model, which for Maven3 can only be 4.0.0
The most important thing in the code is to include groupId,artifactId and version. These three elements define the basic coordinates of a project, and in the world of Maven, any jar, pom, or jar is distinguished by these basic coordinates.
GroupId defines which group the project belongs to, and names it at will. For example, Google's myapp project is named com.google.myapp.
ArtifactId defines the ID that is unique to the current Maven project in the group, such as defining hello-world.
Version specifies the current version of the project, 0.0.1-SNAPSHOT, which means snapshot, indicating that the project is still under development and is unstable.
The name element gives birth to a more user-friendly project name, which is not required, but it is recommended to declare name for each POM to facilitate the exchange of information
Dependent configuration. The actual project module version depends on whether the type dependency scope dependency is optional. ... ...
The dependencies under the root element project can contain one or more dependency elements to declare one or more project dependencies. Each dependency can contain the following elements:
GrounpId, artifactId, and version: since the basic coordinates, for any dependency, the basic coordinates are the most important, Maven based on the coordinates to find the needed dependencies.
Type: dependent type, packaging defined for project coordinates. In most cases, this element does not need to be declared, and its default value is jar
Scope: scope of dependency
Optional: whether tag dependencies are optional
Exclusions: used to eliminate transitive dependencies
Dependent range
Dependency scope is used to control the relationship between dependency and the three types of classpath (compile classpath, test classpath, run classpath). Maven has the following dependency ranges:
Compile: compilation dependency scope. If not specified, the dependency range is used by default. Maven dependencies using this dependency scope are valid for compiling, testing, and running three kinds of classpath. A typical example is spring-code, which is required to compile, test, and run.
Test: test the scope of dependency. Maven dependencies that use secondary dependency scope are valid only for testing classpath and will not be available when compiling the main code or running the project's use. A typical example is Jnuit, which is needed only when compiling test code and running tests.
Provided: dependency scope has been provided. Maven dependencies using this dependency scope are valid for compiling and testing classpath, but not at run time. A typical example is servlet-api, which is required when compiling and testing the project, but when running the project, because of the container and the provision, there is no need for Maven to introduce it repeatedly.
Runtime: runtime dependencies on scope. Maven dependencies using this dependency scope are valid for testing and running classpath, but not when compiling the main code. A typical example is the JDBC driver implementation, where the compilation of the main code of the project only needs the JDBC interface provided by JDK, and the specific JDBC driver of the above interface only needs to be implemented when testing or running the project.
System: the system depends on scope. The relationship between this dependency and the three classpath is exactly the same as the provided dependency scope, but when using system-scoped dependencies, the path to the dependent file must be explicitly specified through the systemPath element. Since such dependencies are not resolved through the Maven repository and are often bound to native systems, they may make the build unportable, so they should be used with caution. The systemPath element can refer to environment variables, such as:
Javax.sql jdbc-stdext 2.0 system ${java.home} / lib/rt.jar
Import: import dependency ranges. This scope of dependency has no actual impact on the three classpath types.
The relationships between the above dependencies other than import and the three classpath are as follows:
Transitive dependence
Take an account-email project as an example. If account-email has a compile-scoped spring-code dependency and spring-code has a compile-scoped commons-logging dependency, then commons-logging will become the scoped dependency of account-email 's compile, and commons-logging is a transitive dependency of account-email.
With the transitive dependency mechanism, you don't have to think about what it depends on when using Spring Framework, and you don't have to worry about introducing redundant dependencies. Maven parses the POM of each direct dependency, introducing those necessary indirect dependencies into the current project in the form of transitive dependencies.
Dependent range
Suppose A depends on B and B depends on C, we say that An is the first direct dependence on B, B is the second direct dependence on C, and An is transitive dependent on C. The scope of the first direct dependency and the second direct dependency determines the scope of the transitive dependency, as shown in the following figure, the leftmost line represents the first direct dependency range, and the top line represents the second direct dependency range. the middle cross cell represents the transitive dependency range.
From the above picture, we can find this pattern:
When the scope of the second direct dependency is compile, the scope of the transitive dependency is the same as that of the first direct dependency.
When the scope of the second direct dependency is test, the dependency will not be passed
When the scope of the second direct dependency is provided, only the first direct dependency scope is also provided, and the scope of tangent passing dependency is also provided
When the scope of the second direct dependency is runtime, the scope of transitive dependency is the same as that of the first direct dependency, but outside the compile column, the scope of transitive dependency is runtime.
Comparison between Maven and Gradle
There are three major construction tools in the Java ecosystem: Ant, Maven and Gradle. Among them, Ant is maintained by the Apache Software Foundation; the word Maven comes from Yiddish (Jewish), meaning the accumulation of knowledge, was originally used in Jakata Turbine projects to simplify the build process; Gradle is a project automation build open source tool based on Apache Ant and Apache Maven concepts, it uses a Groovy-based domain-specific language (DSL) to declare project settings, abandoning various cumbersome configurations based on XML.
After several years of development, Ant has almost disappeared, while Maven has gradually been forgotten because of its inflexible configuration, and because Gradle is an optimized version based on Ant and Maven, it has become at its peak.
The main functions of Maven are mainly divided into dependency management system, multi-module construction, consistent project structure, consistent construction model and plug-in mechanism. Here are the differences between the two through these five aspects:
Dependency management system
In the Maven management system, a dependency is uniquely identified by a Coordination composed of GroupID, ArtifactID, and Version. Any project built on Maven must also define these three properties itself, and the resulting package can be a Jar package, an War package, or an Ear package.
A typical reference is as follows:
Org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-test test
Here GroupID is similar to namespace in C# or package in Java, while ArtifactID is equivalent to Class,Version in different versions. If Version ignores it, the latest version link will be selected.
At the same time, the warehouses that store these components can be divided into remote warehouses and local warehouses, which can be central repositories that use the world public, or private warehouses built by Apache Nexus, while the local warehouses are on the local computer. The path to the local warehouse and the remote warehouse address to be used can be configured through the settings.xml file under the Maven installation directory. Gradle is designed to follow the dependency management system of Maven, while introducing improvements to make dependencies more concise:
Dependencies {/ / This dependency is exported to consumers, that is to say found on their compile classpath.api 'org.apache.commons:commons-math4:3.6.1'// This dependency is used internally, and not exposed to consumers on their own compile classpath.implementation' com.google.guava:guava:23.0'// Use JUnit test frameworktestImplementation 'junit:junit:4.12'compile' org.hibernate:hibernate-core:3.6.7.Final'testCompile 'junit:junit:4.+'}
In addition, Maven and Gradle look at dependencies differently. In Maven, there are six kinds of scope for a dependency, which are compile, provided, runtime, test, system, and import. Where compile is the default. However, gradle simplified it into four types: compile, runtime, testCompile and testRuntime. Such as the above code "testCompile 'junit:junit:4.+'", dynamic version dependency is supported in Gradle, and dynamic version management can be achieved by using the + sign after the version number. In solving dependency conflicts, the implementation mechanism of Gradle is more clear, both of them use transitive dependencies, but if multiple dependencies point to different versions of the same dependency, it may cause dependency conflicts, Maven is more complicated to deal with, and Gradle inherently has a relatively clear strategy.
Multi-module construction
In a service-oriented architecture, a project is usually broken down into multiple modules. Parent POM (Project Object Model) needs to be defined as a common configuration model for a set of module in Maven, and tags can be used to define a set of sub-modules in the POM file. The build configuration and dependency configuration in parent POM are automatically inherited to the child module.
Gradle also supports multi-module construction, and allprojects and subprojects code blocks can be used in parent's build.gradle to define configurations that apply to all projects or subprojects, respectively. The definitions in the sub-modules are placed in the settings.gradle file, and each module represents the object instance of project. In the build.gradle of parent, these objects are manipulated through allproject or subprojects, which is more flexible than Maven.
Allprojects {task nice println "iTunm $task.project.name"}}
Executing the command gradle-Q nice prints out the project names of each module in turn.
Consistent project structure
Maven specifies a set of project directory structure as the standard java project structure, and Gradle also follows this standard directory structure. If the Maven project structure is used in the Gradle project, there is no need to make extra configuration in the Gradle, just include the apply plugin:'java', system in the file will automatically identify source, resource, test source, test resource and other corresponding resources.
At the same time, Gradle, as a construction tool on JVM, also supports the construction of source code such as Groovy and Scala. The same function Maven can also achieve its purpose through some plug-ins, but Gradle is more flexible in configuration.
Consistent building model
In order to solve the problem of lack of standardization of project construction in Ant, Maven sets a standard project cycle Build cycle: verification, initialization, generating raw data, processing raw data, generating resources, processing resources, compiling, processing classes, generating test raw data, processing test raw data, generating test resources, processing test resources, test compilation, processing test classes, testing, predefined packages, generating package files, pre-integrated testing, integration testing, post-integration testing, verification, installation, deployment. But this build cycle is also a disadvantage of Maven applications. Because Maven restricts the build cycle of the project too tightly to add new phases to the build cycle, you can only bind the plug-in to an existing phase. On the other hand, Gradle is very flexible in building a model, and can create a task and establish a dependency with an existing task through depends at any time.
Plug-in mechanism
Both use plug-in mechanisms, and Maven is configured based on XML, but is more flexible in Gradle.
This is the end of "how to install maven". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.