Author: Java Academic Party
Repositories: Github, Gitee
Blogs: CSDN, Nuggets, InfoQ, Cloud + Community
Public account: Java Academic Party
Special statement: the original is not easy, shall not be reproduced without authorization or plagiarism, if need to be reproduced can contact xiaobian authorization
Copyright notice: some of the text or pictures in this article are from the Internet and Baidu Encyclopedia. If there is any infringement, please contact xiaobian as soon as possible. Wechat search public account Java Academic party contact xiaobian.
Every limitation begins with one’s own mind.
Everybody is good! I am your old friend Java Academic Party, and I will continue to share with you today the core knowledge of Maven build tools that I have compiled for you. You like the words can be collection, forwarding, a three even yo!! Maven Project Object Model (POM), a project management tool that manages project builds, reports, and documentation with a short description. Because Maven’s default build rules are highly reusable, simple projects can often be built with two or three lines of Maven build scripts. Because of Maven’s project-oriented approach, many Apache Jakarta projects use Maven when they publish, and the percentage of corporate projects using Maven continues to grow.
1. The Maven is introduced
- Maven is a project build tool (it’s a build aid, you can do it without it)
- Software is an engineering (software engineering) : the concept of software engineering has been developed in order to enable pipelined production of software, to have a standardized and engineered approach to designing patterns and building software.
2. What needs to be done to complete a Java project
-
1. Analyze what the project is going to do and know what the components are.
-
2. Design the project, what steps to go through and what technologies to use. How many people, how long.
-
3. Build a team, hire people, and purchase equipment, servers, software, and laptops.
-
4. Developers write code. Developers need to test their own code. Repeated work.
-
5. Tester, test whether the project function meets the requirements. The test developer submits the code – if there are problems with the test – needs to be modified by the developer – submits the code to the test – the tester tests the code – if there are still problems – gives it to the developer – the developer submits – tests until – the test code passes.
3. Problems with traditional development projects, there are no Maven managed projects
1) There are many modules, and there are relationships between modules. Manual management of relationships is tedious.
2) You need a lot of third party functionality, you need a lot of JAR files, you need to manually fetch each JAR from the network
3) To manage the version of jar, you need mysql.5.1.5.jar to take which you cannot give to a mysql.4.0.jar
4) Manage dependencies between JAR files. To use A.jar in your project, you need to use the classes in B.jar. B. jar must be obtained before A. jar can be used.
This relationship is called a dependency, or the mysql driver that your project is using, or the mysql driver that your project is relying on. A.c lass uses b.class,a depends on b classCopy the code
4.Maven role (improved project development and management)
1) Maven can manage JAR files
2) Automatically download jar and its documentation, source code
3) Manage jar dependencies directly. A. jar requires B. jar, and Maven will automatically download B. jar
4) Manage the JAR versions you need
5) Compile your program, compile Java to class
6) Help you test if your code is correct.
7) Package files for you to form JAR files, or WAR files
8) Help you deploy your project
5. Project construction
-
In this case, testing, compiling, packaging, and deploying are all part of the build process.
-
Builds are process-oriented, the steps that complete the compilation, testing, running, packaging, deployment, and so on of a project’s code.
5.1 Maven supports the following builds
-
Clean up: delete the things compiled by the previous project, and prepare my new compiled code.
-
Compile: Compile the program source code into execution code, java-class files. This compile is batch. Maven can compile hundreds of files into classes simultaneously. (You can compile all Java files in your project into classes at once)
This compilation is different from javac, which compiles one file at a time.
-
Testing: Maven can execute the test program code to verify that your functionality is correct. This test is also batch, with Maven executing multiple test codes simultaneously, testing many functions (methods) simultaneously. Without Maven, you can only test one function (method) at a time.
-
Report: Generate a file of test results, test passes or not.
-
Package: (called after successful testing) put all the class files, configuration files, and other resources in your project into a compressed file. The zipped file is the result file of the project, normally Java programs, zipped file extension is.jar. For Web applications, the zip file extension is.war
-
Install: install the jar and WAR files generated in the package to the local repository
-
Deploy: To install the program and execute it.
The first six steps are implemented using Maven, and the final deployment step is done by the developers themselves, which can be complex using Maven.
6.Maven core concepts
-
Maven’s ability to automate builds is tied to its internal principles. Here we start with nine core Concepts of Maven.
Take a look at how Maven implements automated builds.
6.1 Maven’s nine core concepts
- POM:A fileThe name is POM.xml, which poM translates toProject object model.Maven uses a project as a model. Control the maven build process and manage JAR dependencies.
- Convention directory structure: The directory and file locations of a Maven project are specified.
- Coordinates: is oneUnique stringUsed to represent resources.
- Dependency management: Manage your projects using JAR files.
- Warehouse management (understanding) : Where your resources are stored.
- Life Cycle (Understanding) : The process by which maven tools build projects is the life cycle.
- Plug-ins and goals (understand) : The tools used to perform Maven builds are plug-ins.
- Inheritance: Inheritance in Maven refers to inheriting the dependencies in POM.xml, and then subprojects can directly use the referenced jars and projects in the parent project. This function is the same as passing dependencies, but is written differently. Similar to inheritance in Java.
- Aggregation: String together the build process of a subproject. A project is usually composed of multiple modules. When building, it is very tedious and error-prone to run build commands for each module **, ** therefore, Maven’s aggregation function can complete the build of all modules by running one build command for us. Put several projects together.
How to use Maven, first the difficult and then the easy. Use maven command to complete the use of maven, use maven directly in idea, instead of command.
7. Install and configure Maven
-
Download the Maven installation package apache-Maven-3.3.9-bin.zip from the Maven official website
-
Decompress the installation package to a non-Chinese directory.
Decompression file introduction:
- The bin directory is home to some of Maven’s tools (mvn.cmd, which is a maven command that can execute a Maven build project).
- The conf directory contains a settings. XML file, which is the configuration file for the Maven tool.
- The Jar package is stored in the lib directory, and the Maven tool is written in the Java language.
-
Configure environment variables: In the system environment variable, specify the name M2_HOME and specify its value to be the directory before the maven tool installation directory bin
M2_HOME=D:\work\maven_work\apache-maven-3.39.Add M2_HOME to path and add %M2_HOME%\bin before all paths.Copy the code
-
To verify, on the new command line, run MVN -v(this command stands for testing the Maven version number).
8.Maven core —- project convention directory structure
- So-called agreement is already good, can not do according to this agreement, but still come according to this agreement normally.
- An agreement is a rule that everyone follows.
- Each Maven project is a folder on disk. (This folder is the project)
Hello/ --/ SRC ------/main # put your main program Java code and configuration file ----------/ Java # Your program package and the Java files in the package -- -- -- -- -- -- -- -- -- -- # / resources to use in your Java program configuration file -- -- -- -- -- - / test # put test program code and documentation (can't) -- -- -- -- -- -- -- -- -- - / Java # test package and package in the Java file ----------/resources # Test the configuration file to be used in the Java program -/ pom.xml # Maven core file (required for the Maven project)Copy the code
-
Mavne will automatically go to the main directory to find the main program and the test directory to find the test program. All of Maven’s work is found in the pom.xml file.
-
When launching a project, Maven goes firstpom.xmlGo to the main and Test folders to find where the work is
-
PowerShell is a superset of CMD, PowerShell can do all the things that PowerShell can do, but PowerShell does not have to do all the things that PowerShell can do, PowerShell can interact with CMD (in the CMD black box you can type PowerSehll and use PowerShell, You can also enter CMD in the CMD black box and use CMD.)
-
It works with either PowerShell or CMDTree Directory name (file name)To get the directory structure in that directory
-
This gets the rest of the directory structure (the folder structure), not the files in it (for example, pom.xml).
8.1 Obtaining Maven jar packages (in Mave, this jar package is called plug-ins)
-
How to obtain, according to the above directory to create a good document, in the root directory using MVN compile SRC /main directory of all Java files. At this point, you’ll have all the plugins (that is, jars) you need for Maven.
-
Note: To compile the MVN command, you must run Java files in the same directory as pom.xml.
-
Where to download these JARS: in the Apache central repository.
Maven at apache Central Repository address Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-parameter-documenter-2.0.9.pomCopy the code
-
Why you want to download it: The Maven tool performs operations that require many plug-ins (Java class-JAR files). Without this
With some JAR packages supported, Maven cannot do this.
-
What you download: Jar packages (called plug-ins in Maven) are the files that Are necessary to perform some of Maven’s functions.
-
Where is the downloaded JAR: default repository (local repository) C:\Users\YunboCheng(username).m2\repository
-
The result of the download: when the MVN compile command is executed successfully, a target directory (the result directory) is generated under the project root path (which is the same as SRC and POM.xml).
-
In this target directory, we hold the.class files for all the Java classes we write.
8.2 Setting the Local Directory for Storing Resource Files (Setting the Local Repository)
-
Change the maven configuration file to the maven installation directory /conf/settings.xml
First backup setting. The XML
-
Modify localRepository to specify the directory you want to move **(not Chinese)**
-
Find this line of code in the setting.xml (at this point, the following code is in the comment of the XML file), which is the default location where the JAR packages will be stored after downloading. You need to take the following line of code out of the comment and change its path.
<localRepository>/path/to/local/repo</localRepository> <! -- Take this line out of the comment and change the path to your own --> <localRepository>/path/to/local/repo</localRepository> <! -- for example, the path you want to change is: C:\development\Maven\ maven-jar --> <! -- Note that \ is changed to/consistent --> <localRepository>C:/development/Maven/Maven-jar</localRepository> Copy the code
8.3 How to Use Maven
-
Use Maven alone: Use Maven’s various commands to compile, test, package, and so on.
For example, the Maven command MVN compile is used independently to compile the code.
-
Use with development tools: You can generally use Maven in IDEA: simple, quick, and no need to remember commands.
9. Maven core —- repository concept
-
The repository is where you store things, the JARS that Are used by Maven and the jars that are used by our project
- Plugins used by Maven (various JARS)
- Jar used by my project (third-party tools such as myQL driver, Jackson)
9.1 Classification of warehouses
-
Local repository: A folder on your PC that holds various JARS
-
Remote warehouse: a warehouse located on the Internet and accessible only by using the network (remote warehouses include the following three types)
(1) Central repository: the most authoritative, all developers share a centralized repository repo.maven.apache.org: central repository address.
(2) The mirror image of central warehouse: it is the backup of central warehouse. In all continents, important cities are mirrored.
③ Private server: in the company, used in the LAN, not external use.
9.2 Use of warehouse
-
Maven repositories are used without human involvement
-
The order in which developers use the repository:
Developers need to use the mysql driver -- -- -- -- -- -- -- -- > maven first checks the local warehouse > private servers -- -- -- -- -- -- -- -- - > > image central warehouseCopy the code
-
The order in which the developer uses the remote repository
The repository downloaded from the remote repository is always saved to the local repository
10.Maven core —- POm file
-
Pom is the Project Object Model (POM). Maven abstractions the structure and content of a project into a model, which is declared in an XML file for construction and description. Pom.xml is the soul of Maven. So, once the Maven environment is set up, all the learning and manipulation is about POM.xml.
-
Pom.xml initial: The following parameters are tag attributes in the POM.xml file
All of these core Maven parameters are uninstalledpom.xmlFile.
11.Maven core —- coordinates
-
Every Maven projectMust beThere are coordinates, the unique identification of the project.
-
GroupId + artifactId + Version represents a coordinate
-
This coordinate is unique in value and uniquely identifies an item in the Internet.
11.1 Central Warehouse
-
MVN Repository.com/ (MVN Central warehouse address)
-
In this central repository you can download jar packages for everything you need and search the web for what you need.
For example: searching mysql will find information about mysql database, connection driver, etc…..
-
So this is what happens when you search mysql, This includes the name of the company to which the MySQL Connector/J » 8.0.26 belongs, the original download location of the version, the mechanism to download poM version files and jar packages, and most importantly, the Maven box below that provides the unique identifier (location) of the database for this version. This coordinate is provided by MySQL’s company.
-
The main thing about using the central warehouse is to go to the warehouse and look for thisThe Maven dependency code in the pom.xml file, copy the dependency code into our pom.xml file, and Mavne will automatically download the required JAR package and store a copy locally (or in the default native repository)
12.Maven core —- dependency management
-
Packaging: indicates the extension of the compressed file after packaging. The default value is JAR. For web applications, the value is war.
-
Packaging can be left out because the default is JAR
How pom.xml adds dependencies:
-
If the 5.1.9 mysql-conncetor jar package exists in the local repository, Maven will bind the 5.1.9 mysql-conncetor database to the project. Mysql, mysql-conncetor, and 5.1.9 are all folders that exist in the local repository
-
If the above dependency files are not available in the local repository, Maven will automatically look for them in the remote repository.
Developers need to use the mysql driver -- -- -- -- -- -- -- -- > maven first checks the local warehouse > private servers -- -- -- -- -- -- -- -- - > > image central warehouseCopy the code
Maven core —- configuration properties
14. Build Maven core —-
- When building a project, Maven configures information, such as specifying the version of the JDK used to compile Java code
15. Maven core —- Lifecycle, common commands, plug-ins
-
Maven lifecycle: This is the process by which Maven builds a project, cleaning, compiling, testing, reporting, packaging, installing, and deploying
-
Maven commands: These commands are used independently by maven to complete the execution of the maven lifecycle. Maven can use commands to clean up, compile, test, and more.
-
Maven plugins: When maven commands are executed, the actual function is done by plugins, which are jar files and classes
The following is a standard engineering convention directory structure
The following are Maven’s common commands
15.1 MVN clean command
-
This is a plug-in that is used in the cleanup phase of the life cycle, using the Maven commandmvn cleanClean command, MVN Clean will then use the above plug-in (JAR package) to complete the clean up.
-
Note: The cleanup is done by the plug-in, not by the command. The command simply calls the plug-ins (JAR packages), and the plug-ins do the cleanup, compile, test, report, package, install, and deploy.
-
MVN clean: only the Java files under the main program are compiled (that is, all Java files under the mian directory, not the Test folder)
Test procedure)
15.2 MVN compile command
-
The following is the use ofThe MVN compile command compiles the main program, the plug-in used by
-
With the MVN compile command, two plug-ins are used for two operations
Compile all Java class files in main/ Java/and store them in the Target /classes folder.
- compileThe main/Java/directoryAll Java files, and store these class files toIn target/classes.
- This plugin willFiles in the SRC /main/resources folderCopy toIn the target/classes folder
15.3 MVN test-compile command
- mvn test-compile: This command compilesAll test programs in the test folder, these compiled tests are stored inIn target/test-classes folder.
15.4 MVN test command
-
Important: the package path of the test program must be the same as the package path of the main program, so as not to report errors, that is, to ensure that the two files are in the same package path.
-
At this point, you can see that the test program in test has the same path as the main program in Main.
-
This one is executedTest program in the test/ Java folder, all test results will be stored inTarget/Surefire -resports folder.
- Note: In executionmvn testCommand, the previous steps (cleaning, compiling the main program, compiling the test program) are redone because of the Maven lifecycle. Therefore, more plug-ins are needed at this time. The first compile is the main program. The second compile is the test program.
- The above plugin is used to test the program. The following output format represents a successful test.
- From TESETS the following are the test results:
- Runing represents the path of the test program to be executed and the name of the test program.
- Maven junit testAddRepresents the output of the test result.
- Test Run: Indicates the information when the Test program is running. (Number of tests, failure, error, skip, test time)
- Results: represents the output of this test program. (Number of tests, failures, errors, skips)
- The following code tests fail. An exception is thrown, showing two tests and one failed.
The following code is the test result: Test two, fail one, will tell you the error message.
Failed tests:testAdd2(com.yunbocheng.TestHelloMaven): expected:<5> but was:<3>
Copy the code
Important: The above error message will generate a report on thetarget/surefire-reportsFolder. The test ofAll the resultsIt’s all in this folder.
When the error of the above code is modified, the following result will be displayed. Both methods were tested and both methods were successful.
15.5 MVN package Command (Package the main Program)
- mvn packageCommand to package the main program as jar or WAR according to the pom.xml configuration file.
- The following is the packaged plug-in. Before executing the plug-in, the project will also be executed using the compiled, tested plug-inmvn packageThis command packages the main program.
-
The JAR and WAR packages are equivalent to a compressed file.
-
A ch01-maven-1.0-snapshot. jar package is generated in the target directory.
-
The jar contains the main program’s class files and configuration files (POM.xml).
-
Hello: The path to the project’s main program (HelloMaven) and the bytecode file.
-
There is a meta-INF folder that contains the pom.xml configuration file for the project.
-
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
This configuration file was created and written by the developers themselves in the Mian directory.
-
-
The name of the jar package
-
Ch01-maven: this is the artifactId(project module name parameter) in the project pom. XML configuration file.
-
1.0-snapshot: the version parameter in the pom. XML configuration file of the project.
-
Important: This JAR contains all of the main program class files from the original Maven project’s main directory and the developer’s own configuration files.
15.6 Running the MVN install Command (Installing the Main Program)
- The project will be packaged and saved to the local warehouse according to the coordinates of the project. Even if the jar package generated by this project is stored in your own native repository or default repository (.m2), other projects can use the main program methods in your project
- Here is the implementationmvn installBefore executing the plug-in, the project will also be executed using the compiled, tested, and packaged plug-insmvn packageThis command saves the jar packages generated by the Maven project to the native repository
- The address of the local repository is either the address you configured in Maven or the default address (.m2).
- The address saved to the local repository is in this Maven projectXML configuration file coordinates.
<groupId>com.yunbocheng</groupId>
<artifactId>ch01-maven</artifactId>
<version>1.0 the SNAPSHOT</version>
Copy the code
This is in the POM.xml configuration file of the Maven projectCoordinate information. The following is saved to the local repositoryPath information
C: \ development \ Maven \ Maven - jar \ com \ yunbocheng \ ch01-1.0 the SNAPSHOT Maven \ \ ch01 - Maven - 1.0 - the SNAPSHOT. The jarCopy the code
You can see that the coordinate information is the path information saved to the local repository
Note: In the groupId parameter information, each decimal point represents the level 1 folder
Com. yunbocheng stands for com/ Yunbocheng.
16. Unit test junit
-
Unit testing (test method) : Using junit, which is a framework (tool) for testing.
-
What junit tests: Tests the methods in the class, each individually tested.
A method is the basic unit (unit) of a test.
-
Maven uses unit testing to batch test a large number of methods in your class to see if they work as expected.
Test program format
17. Explanation of the POM. XML configuration file in the Maven project
<project xsi:schemaLocation="http://maven.apche.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yunbocheng</groupId>
<artifactId>ch01-maven</artifactId>
<version>1.0 the SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Copy the code
- ModelVersion: is the version number of Maven, currently 4.0.0 is used uniformly
<! This code indicates the path where the jar package generated by the project is saved in the native repository -->
<groupId>com.yunbocheng</groupId>
<artifactId>ch01-maven</artifactId>
<version>1.0 the SNAPSHOT</version>
Copy the code
<! Maven will automatically find and use the jar package if it exists on the local server. If it does not exist, Maven will automatically retrieve the jar package from the remote repository and save a copy of the obtained jar package in the local repository for future use. -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<! <dependencies> <dependencies> <dependencies>
<! <dependency> <dependencies> <dependencies> <dependencies> <dependencies> <dependencies> <dependencies>
Copy the code
18. Compile the plug-in configuration
- Plug-ins can be set up in your own project, and the most commonly used are maven compiled plug-ins. Setting the JDK version to be used by the project is specified by compiling the plug-in. Use the < build > tag in the POM.xml configuration file.
- Build is used to build configuration projects.
- Control the configuration of Maven build project parameters, such as: set the JDK version.
- Description of the parameters:
- Build: Indicates the parameters of the build project.
- Plugins: Configure plugins. This is like a curly bracket for all plugins. All plugins are written inside this.
- Plugin: Configures the specific plugin, which represents one plugin in the Maven project, as well as other plug-ins.
- GroupId: The organization name of the plug-in.
- ArtifactId: Indicates the name of the plug-in.
- Version: indicates the plug-in version.
- Confiruration: configures plug-in information.
- Source: tell Maven that the code we wrote was compiled on jdk1.8.
- Target: Our program should run on a 1.8 JDK.
18.1 Two Methods for Configuring the JDK Version
- The first: Use the Properties TAB to configure
- Second: Use the Configuration TAB
19. Set Maven in idea
-
Set maven in IDEA and let idea and Maven use together.
-
Maven is built into idea, and is usually not used because it is inconvenient to modify Maven Settings.
-
To use your own maven installation, you need to override the default Settings in IDEA. Let IDEA specify information such as where Maven is installed
-
In the idea file, Setting is used to set the current project, and Other Settings is used for future new projects
-
Both locations need to be configured when configuring Maven.
19.1 Configuring Maven in the Setting
- Step 1: Configure Maven
Three places to configure
-
Maven Home Directory: specifies the Maven installation directory.
-
User Settings File: Maven installation directory conf/setting. XML
-
Local Repository: directory location of the Local Repository.
-
Step 2: Configure the running program
- Configure the JDK version
- Set a parameter, -darchetypecatalog =internal. This parameter is intended to make building a Maven project faster.
- When creating a project, Maven will download a template file through the network by default. The size of this template file is 7M. In order to avoid not downloading this template file, add this parameter information to indicate that we are using internal content and do not need to download it.
- -darchetypecatalog =internal, the template file will be downloaded from the maven network when the project is created. (After 2019, you don’t need to add this for new versions)
19.2 Configuring Other Settings
- Step 1: Find new Project Management in the file drop – down menu
- Step 2: Find the new project Settings
- Step 3: Find Maven and configure it the same way
- Step 4: Add the JDK and parameter information
20. Idea to create JavaSE project
Create the project using the template
- Maven-archetype-quickstart: Normal Java project
- Maven-archetype-webapp: Web project
Here is the structure directory after the maven-archetype- QuickStart module has been successfully built
Note: The resources file is manually created by ourselves. The maven-Archetype – QuickStart module does not provide these two folders. The APP class is automatically generated by the module. If you want to use another class file, you can delete this one directly.
In IDEA, different folders are displayed in different ways
- Source root: The root folder that represents the main Java program.
- Test Source Root: The root folder that represents the Java test program.
- Resource root: represents the configuration file in the main folder.
- Test resource root: represents the configuration file in the test folder.
The following directory structure contains the Target folder generated after the project is executed.
The following is the structure of a Java project built by Maven after executing the program. The Java project generates jar packages
21. Idea to create Web projects
Create the project using the template
- Maven-archetype-quickstart: Normal Java project
- Maven-archetype-webapp: Web project
Here is the Web project directory set up using the maven-archetype-webapp module
In this case, manually create a Java directory (source root) under the main folder, and a folder to store the main program configuration (resource root).
You also need to create a test folder (test) under SRC that is the same as the main folder, a test folder under Test (test source root), and a configuration folder under test (test resource root).
Here is a complete Web Maven project structure
There is no need to import external servlets (JAR packages) and JSPS (JAR packages) in Maven. You only need to add servlets and JSPS dependencies in POM.xml
Note: The following dependencies do not need to be written manually; go to the central repository and download the pom.xml source code.
Central Warehouse address:The central warehouse
<dependencies>
<! -- These dependencies are written inside the dependencies tag -->
<! Add servlet dependencies (servlet JAR packages)-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<! -- add JSP dependencies -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
Copy the code
Here is the structure of a Maven-built Web project after the program is executed, and a Java project generates the WAR package
This WAR package is the final web file delivered to customer service. After decompression, this WAR package can be used directly
- At this point, put the war package in Tomcat/webapps, and then start Tomcat/bin/startup.bat. After startup, you can directly run the war package file on the web page.
22. Import Maven project (module) in Idae
22.1 Solving unrecognized dependency problems:
- First: Hover over the pom.xml file, right-click Maven, and click Reload project.
- Note: Only the dependent files of this project will be refreshed.
-
Second: Click the Refresh button in the Maven bar
-
Note: This refreshes the entire project that exists in the Maven bar
22.2 IDEA Import Maven module
- Step 1: Find the project structure, find the module, and click the plus sign to import the module.
- Step 2: Find the Maven module you want to import in your Explorer.
- Step 3: Select the import module from the external model, select Maven, and click OK.
- Step 4: Check the missing dependencies. Idea will be our choice.
23. Dependency management
- Dependent scopes, represented by the scope tag in the POM.xml configuration file.
- Scope: Represents the scope in which dependencies are used, that is, the stages in which Maven builds a project.
- Maven build project compile, test, package, install, deploy process (phase)
- Scope has the following values: compile, test, provided. The default is compile
- Compile: The dependent JAR package is used during compilation, testing, packaging, installation, and deployment.
- Test: Use the dependency JAR only during the test program phase,
- Provided: This dependency JAR package is used between writing the main program and running the program.
-
Important: When the project is packaged, there are dependencies in the generated WAR or JAR package that is declared by compile.
Using Provided, the generated JAR or WAR does not contain dependencies declared by Peovided when the project is packaged.
-
When you add a dependency JAR package to a POM.xm file, Maven automatically downloads it to the central repository without the developer having to solve the problem, and the code (coordinates) to add the dependency can be copied and pasted from the central repository.
Maven common operations
24.1 Maven properties
- This property setting is provided by Maven.
<! The Maven property Settings are declared in the Properties TAB -->
<! --maven.compiler.source this is the property name, 1.8 this is the property value -->
<properties>
<! Maven builds projects using code, avoid Chinese garble -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<! -- Compile the code using the JDK version -->
<maven.compiler.source>1.8</maven.compiler.source>
<! -- JDK version used to run the program -->
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
Copy the code
24.2 Maven global variables
-
This property is developer custom.
-
Custom properties:
- The variable is declared in the Properties tag with a custom tag (the tag name is the variable name).
- Elsewhere in the pom.xml file, use ${tag name} to use the value of the variable.
-
If you want to use more than one version number in your project, use the global variable definition first, and then ${variable name}.
<! Declare a custom global variable in the Properties tag -->
<properties>
<! Maven builds projects using code, avoid Chinese garble -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<! -- Compile the code using the JDK version -->
<maven.compiler.source>1.8</maven.compiler.source>
<! -- JDK version used to run the program -->
<maven.compiler.target>1.8</maven.compiler.target>
<! -- custom variable (the name of the variable is the tag name), indicating the version number -->
<! This tag name can be defined by itself, since the signature in the XML file is arbitrary and not required. -->
<spring.version>5.2.0</spring.version>
</properties>
Copy the code
- Later, when using the version number in other places, you don’t need to write the version number in sequence, but just use this variable.
- If you change the version number in properties once, the version number will change in other dependencies.
24.3 Resource Plug-ins
- Add the resources tag to the build tag in pom.xml
<build>
<! Just put the resources tag in the buil tag, anywhere -->
<resources>
<resource>
<directory>src/main/java</directory><! -- directory -->
<includes><! * * * * * * * * * * * * * * *
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<! *. Property is already used to filter -->
<filtering>false</filtering>
</resource>
</resources>
</build>
<! Select * from SRC /main/ Java directory where all.properties and.xml files are stored -->
Copy the code
For example, mybatis uses this function.
-
By default, maven will copy files from the SRC /main/resource directory to target/classes directory when compiling code. Non-java files in SRC /main/ Java are not copied to target/classes.
-
When executing a Java program, we need to use files in SRC /main/ Java. When compiling a program in SRC /main/ Java, use the MVN compile command to copy all non-Java files from the Java folder to target/classes
Source code for the above project,Click on the planet to get it for freeplanet(making address)If you don’t have your Github buddies. You can follow my wechat official account:Java academic lie prone, send Maven, free to send to everyone project source code, code is tested by xiaobian personally, absolutely reliable. Take it for free.
——– to see the big guys can pay attention to xiaobian, will always update tips, free to share with you!! ———
Click on the planet to quickly enter the Github planet!! There are more interesting technologies inside, waiting for you to explore!!