In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use maven. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
Why use a build tool like Maven [why]
A project is a project.
If the project is very large, it is not suitable to use package to divide modules, it is best that each module corresponds to a project, which is conducive to the division of labor and cooperation.
With the help of maven, you can split a project into multiple projects
Jar package is used in the project, which needs to be copied and pasted into the lib of the project.
The same jar package appears repeatedly in different projects, and you need to keep copying and pasting.
With maven, you can keep jar packages in a "repository", no matter which project you use references.
Always prepare the jar package yourself or download it from the official website every time you need it.
With the help of maven, we can use a unified specification to download jar packages, specifications
Risk of inconsistent versions of jar packages
When using jar packages for different projects, it is possible to cause different versions of jar packages for different projects, resulting in unexecuted errors.
With the help of maven, all jar packages are placed in the "warehouse", and all projects use a jar package from the warehouse.
A jar package that relies on other jar packages needs to be manually added to the project.
FileUpload component-> IO component, commons-fileupload-1.3.jar depends on commons-io-2.0.1.jar
It not only wastes the time cost of importing the package, but also greatly increases the learning cost.
With the help of maven, it automatically imports dependent jar packages.
What is maven [what]
Maven is an automated build tool for the java platform.
Make- > Ant- > Maven- > Gradle
The name is Fa: we can be called Mei Wen or McWen, but there is no one named Ma Wen.
Construction
Build definition: the whole process of deploying the compiled results of a dynamic Web project to the server.
√ compilation: java source file [.java]-> compilation-> Classz bytecode file [.class]
√ deployment: the final deployment in the sevlet container is not the dynamic web project, but the compiled file
Each link of the construction
[1] Clean clean: delete the previously compiled class bytecode file
[2] compile compile: compile the java source program into a class bytecode file
[3] testing test: automated testing, automatically invoking junit programs
[4] report report: results of execution of the test program
[5] package package: War package for dynamic Web project and jar package for java project
[6] install install:Maven specific concepts-copy the packaged files to the specified location in the warehouse
[7] deploy deploy: copy the war package generated by the dynamic Web project to the Servlet container so that it can run
Third, install maven
Whether the current system configures the environment variables of JAVA_HOME
Download maven, decompress maven and put it in a path without spaces in non-Chinese characters.
Configure the relevant environment variables for maven
Add M2_HOME to the environment variable, and the path is the root directory of the decompressed maven.
Add the directory of maven/bin to the path in the environment variable
Verify: maven-v View maven version
Fourth, the first maven
Create the agreed directory structure (the maven project must be created according to the agreed directory structure)
[1] Root directory: project name [2] |-- src: source code [3] |-|-- main: store the main program [4] |-java:java source file [5] |-resource: store the configuration file of the framework [6] |-test : store the test program [7] |-pom.xml:maven 's core configuration file
Let's create it manually according to the folder directory structure above, without any IDE environment (manual actually helps us to understand maven most)
Common maven commands
-[1] mvn clean: clean-[2] mvn compile: compile main program-[3] mvn test-compile: compile test program-[4] mvn test: execute tests-[5] mvn package: packaging-[6] mvn install: installation
Run mvn compile
OK, after running, the dependent packages you configured in pom.xml have been imported into the repository. The question is, what is the default location of the repository? Default location of the warehouse: C:\ Usrs [user name of login to the current system] .m2\ repository the previous folder has changed after the compile has just been executed
There is a test-classes folder under the running mvn test-compile,target folder except classes.
There is another packed jar package under the mvn package,target folder.
Run mvn clean and find that the entire target folder is gone. Back to the folder we created manually before compilation
5. Warehouse and coordinates
Pom.xml:Project Object Model project object model. It is the core configuration file for maven, and all the built configurations are set here.
Coordinates: use the following three vectors to uniquely locate a maven project in the warehouse
The relationship between the coordinates of maven project and the path in the warehouse:
Mapping relationship between maven coordinates and warehouse: [groupId] [artifactId] [version] [artifactId]-[version]. Jar goes to the local warehouse to take a look at this directory: org\ springframework\ spring-core\ 4.3.4.RELEASE\ spring-core-4.3.4.RELEASE.jar is exactly the same (the default warehouse address said above, don't say you don't know where it is, it's okay. We'll talk about the warehouse next).
Warehouse
Classification of warehouses:
(1) Private server: when built in a local area network, companies usually have private servers, and private servers are generally built using nexus. Other information can be queried during the specific construction process.
(2) Central warehouse: set up on the Internet, such as the springframework is on the central warehouse
1. Local warehouse: the warehouse on the current computer has already been mentioned on the path.
2. Remote warehouse:
VI. Dependence
When maven parses dependency information, it will go to the local repository to find the dependent jar package.
Those who are not in the local warehouse will go to the central warehouse to find the maven coordinates to get the jar package. After obtaining the jar, they will download it to the local warehouse.
Compilation fails when a dependent jar package is not found for the central repository
If you rely on a maven project developed by yourself or a team, you need to first import the jar package of the dependent maven project into the local repository using the install command.
Example: now I create a second maven project HelloFriend, which uses the sayHello (String name) method of the class in the first Hello project. When we compile the HelloFriend project using the mvn compile command, we will prompt for the lack of Hello-dependent jar packages. What should I do? After executing mvn install in the first maven project, you will find that once you have the jar package of the Hello project, once the local warehouse has the jar package of the dependent maven project, you can compile successfully when you use the mvn compile command in the HelloFriend project.
Dependency range scope
Compile, the default value, applies to all phases (development, testing, deployment, running), and this jar will always exist in all phases.
Provided, used only during the development and testing phases, is designed to prevent Servlet containers from colliding with jar packages in your local repository. Such as servlet.jar.
Runtime, used only at run time, such as the JDBC driver, is suitable for the run and test phases.
Test, used only during testing, is used to compile and run test code. Will not be released with the project.
System, similar to provided, needs to explicitly provide a jar,Maven that contains dependencies and does not look for it in Repository.
VII. Life cycle
Maven has three independent life cycles. Please note that they are "three sets" and "independent". It is easy for beginners to regard the life cycle of Maven as a whole, but it is not. These three life cycles are:
Clean Lifecycle does some cleanup before actually building. The Clean life cycle consists of three phases:
Pre-clean performs some work that needs to be done before clean
Clean removes all files generated by the last build
Post-clean performs some work that needs to be done immediately after clean
The core of the Default Lifecycle build, compilation, testing, packaging, deployment, and so on.
1 、 validate
2 、 generate-sources
3 、 process-sources
4 、 generate-resources
5. Process-resources copies and processes the resource files to the target directory, ready to package
6. Compile compiles the source code of the project
7 、 process-classes
8 、 generate-test-sources
9 、 process-test-sources
10 、 generate-test-resources
11. Process-test-resources copies and processes resource files to the target test directory
12. Test-compile compiler test source code
13 、 process-test-classes
14. Test runs the tests using the appropriate unit test framework. The test code will not be packaged or deployed
15 、 prepare-package
Package accepts compiled code and packages it into a publishable format, such as JAR
17 、 pre-integration-test
18 、 integration-test
19 、 post-integration-test
20 、 verify
Install installs the package to the local warehouse so that other projects can rely on it.
Deploy copies the final package to the remote repository so that other developers can share it with the project
So we execute the mvn install command in the Hello project and see what happens in the middle through the log?
From the log, we find that mvn install is actually executed, in which compile and test are already executed. Summary: no matter which stage of the life cycle you want to execute, maven executes plug-ins from the beginning of the life cycle: there are plug-ins (plugin) at each stage, see the red above. The job of the plug-in is to execute its corresponding commands.
Site Lifecycle generates project reports, sites, and publishing sites.
1. Pre-site performs some work that needs to be done before generating site documents
2. Site generates the site documents of the project
3. Post-site performs some work that needs to be done after the site documentation is generated, and prepares for deployment
4. Site-deploy deploys the generated site documents to a specific server
VIII. Dependence on advanced features of maven project
Transitivity of dependence
After the WebMavenDemo project depends on the JavaMavenService1 JavaMavenService1 project relies on the JavaMavenService2 pom.xml file to configure the dependency, you must first mvn install before the dependent jar package can be used.
For WebMavenDemo's pom.xml file to be compiled, JavaMavenService1 must be mvn install
For JavaMavenService's pom.xml file to be compiled, JavaMavenService2 must be mvn install
The principle of relying on the version
The principle of giving priority to those with the shortest path
The log4j version of Service2 is 1.2.7. Service1 excludes the dependence of this package and adds a 1.2.9 version of Log4j. Then the WebMavenDemo project follows the principle of shortest path priority, and the version of Log4j is the same as that of Sercive1.
The same path first declares the principle of priority.
This scenario dependency has changed. The WebMavenDemo project relies on Sercive1 and Service2, both of which are the same path, so the version of whoever first declares the dependency in WebMavenDemo's pom.xml will be used.
Unified management of dependent versions
In order to uniformly manage the version number, you can use the properties tag, in which you can customize the label signature of the version. Use ${Custom tag name} where you use it
9. Build configure WebMavenDemo src/main/java * * / * .xml * * / * .txt * * / * .doc Org.apache.maven.plugins maven-compiler-plugin 2.1 1.8 1.8 org.apache.maven. Plugins maven-resources-plugin 2.1 compile UTF-8 org.apache.maven. Plugins maven-war-plugin 2.1 WebMavenDemo1
After build is configured and mvn package is executed, war packages and files are generated according to the configuration in the target directory specified by the maven project
The above is how to use maven, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.