Maven is a tool that automates build and dependency management. JAR package management JAR package comes from? CSND… 51 cto… … JAR A is said to rely on B when A jar uses A class from B jar. The complexity of the dependencies common among JAR packages greatly increases the difficulty of managing jar packages when we develop projects. Manual administration is almost impossible when there are many JAR packages. Spring-core relies on commons-logging commons-fileupload relies on commons-io… each project copies the same JAR package separately at development. It is preferable that each project makes its own reference to the same JAR package. 2.2 Project Architecture In terms of the technologies we learned before, we developed projects with a single architecture. Single architecture; There is only one project for the whole project. However, in the future we will develop distributed architectures. Distributed architecture: A project consists of a number of projects, among which there are four relationships: Dependency: inherited by Maven implementation: aggregated by Maven implementation: invoked by Maven implementation: construction is a concept that proposes the concept of Dubbo+Zookeeper combination SpringBoot+SpringCloud combination with the help of other distributed architecture technologies. 2.3 Automated Construction construction is actually a feasible project process that is “produced” using the code written during development as the “raw material”. We develop the project, but what actually runs on the server is the result of the project “build”. Without actually using a build tool like Maven, we’ve been doing builds in Eclipse without even realizing it. For example: clean up, test, compile, deploy, and so on.
the main link in the construction process
Clean up the: Deletes the *. Class bytecode file from the previous compilation, preparing for the next compilation.
compile: Recompile Java source programs into *.class bytecode files.
Body program compilation
Test program compilation
test: Executes a pre-written test program to test the main program
The report: Test results
packaging: Encapsulates all the code and configuration files in the entire project into a compressed file to prepare for deployment
Java Projects: JAR packages
Web Project: WAR package
The installation: Store the packaged files in Maven’s repository in a tool like Maven.
The deployment of: Puts the packaged result in a specified directory on the server for it to run.
The role of build tools in the automated deployment process
Use of Maven tools
3.1 Working Mechanism
3.2 Decompressing the Core programs for Configuring Maven
将Apache maven – 3.5.4 – bin. ZipUnzip to a non-Chinese directory without Spaces.
Configuring environment Variables
path
Routine: bin directory
MAVEN_HOME
Routine: Indicates the upper directory of the bin directory
※ Verification: Check the version of Maven on the command line
mvn -v
※ Note: JDK must be correctly installed on your current system
configuration file path MAVEN_HOME/conf/settings.xml
* The directory used for settings. XML is: MAVEN_HOME=D:\ apache\apache-maven-3.5.4 D: In-use configuration file structure: In-use configuration file structure D: In-use configuration file structure: In-use configuration file
<? The XML version = "1.0" encoding = "utf-8"? > < Settings XMLNS = "http://maven.apache.org/SETTINGS/1.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" > <! <localRepository>/path/to/local/repo</localRepository> --> <pluginGroups></pluginGroups> <proxies></proxies> <servers></servers> <! <mirrors></mirrors> <! -- Specify the JDK version of Maven project --> <profiles></profiles> </ Settings >Copy the code
configure the local repository path must be removed from the comment or is invalid!!
<! -- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> --> <! --> <localRepository>D:\RepMaven0906</localRepository>Copy the code
configure ali Cloud mirror server address Note: Be sure to configure into the mirrors TAB
<mirrors> <! -- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <! <mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name> nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors>Copy the code
configure the Maven project JDK version must be configured inside the Profiles tag!! You can copy it directly
<profiles> <profile> < ID > JDK-1.8 </ ID > <activation> <activeByDefault>true</activeByDefault> < JDK >1.8</ JDK > </activation> Piler < properties > < maven.com. Source > 1.8 < / maven.com piler source > < maven.com piler. Target > 1.8 < / maven.com piler. Target > The < maven.compiler.com pilerVersion > 1.8 < / maven.compiler.com pilerVersion > < / properties > < / profile > < / profiles >Copy the code
Note: The JDK version that the Maven project can use is limited by the Maven core application version.
3.4 Setting the Maven plug-in to set point in Eclipse requires resetting after changing the workspace
specifies the location of the Maven core program
specifies settings. XML configuration file path
4 Single Maven Project 4.1 Create a Maven project packaged in JAR mode
The general steps of the Maven project are the same as those of the jar package project, except that the war is selected when the package method is selected.
Then you need to generate web.xml by project → right-click
4.3 Creating a Maven project with POM package
The creation process is basically the same as before, just change the packaging method to POM. Instead of writing Java code, framework profiles, pages, and so on, these projects manage other Maven projects.
coordinates: uniquely locates a JAR package in the Maven repository. GroupId: company or organization domain name in reverse order + project name artifactId: : The current project is created. In order to implement an automated build, Maven designs a convention directory structure. Based on the convention, Maven knows the location of Java source files, configuration files, test programs, and so on in a project. You can then compile the source files, execute them, load read configuration files, and perform tests.
POM Project Object Model Maven implements the construction operation and dependency management of each Maven Project based on pm. XML lifecycle characteristics: Within each lifecycle, Maven executes any link from the beginning of the entire lifecycle. So, suppose we want to execute the following operations: compile test package install Then, we can directly execute install, compile test, package are included in the install execution process. Maven is designed to further automate the build process so that you don’t have to worry about the actual build process, but just execute the final steps. ………………………………… The compile… The test – the compile… The test… Package… command line command line Execution the first step: open the command line window. The second step: D:\workstation\Station180906\Pro14_MavenSingleProject Run the Maven command MVN clean MVN compile MVN test MVN package MVN install MVN deploy
The execution effect is as follows:
Eclipse to perform
Right click on the POM.xml file →Run As→Maven Clean and so on
The execution effect is as follows:
To execute Maven commands not provided, click Maven Build…
If you click on Maven Build, you can execute the Maven command directly.
Specific command is introduced
MVN clean
Clean up the
MVN compile
Compile main program
MVN test – the compile
Compile test program
MVN test
Execute the junit test program
MVN package
Perform packaging operations. Java project jar package, Web project war package. The generated JAR or WAR package is placed in the target directory.
5Maven jar package download failure 5.1 the symptom is not limited to this one, but it is the most typical and direct symptom.
Another expression:
*.lastupdated files: Maven downloads jar packages with file names ending with lastUpdated, indicating that the file is being downloaded. However, if the download fails, Maven will not automatically remove the lastUpdated suffix. The next time Maven redownloads, files with the suffix lastUpdated will be ignored and will not be redownloaded. Without human intervention, these failed jars will remain in this state. The human intervention is to manually delete the files at the end of lastUpdated and let Maven re-download them. This operation can be made more efficient by using the clearLastUpdated. Bat script.
First step places the clearLastUpdated. Bat file in the root directory of the Maven repository. Step 2 opens the clearLastUpdated. Bat file using a text editor, or notepad if there is no text editor
the third step
Double-click to open and use as prompted
a possible problem to be encountered
cls
@ECHO OFF
SET CLEAR_PATH=D:
SET CLEAR_DIR=D:\Program Files\maven
Solution: Make Maven repository paths without Spaces!!
5.3 Basic Idea Delete the JAR package that fails to be downloaded and let Eclipse download it again. locate the storage location of the JAR package JAR package → right-click → Properties → duplicate path deletes engineering → Alt +F5→OK tries again if the JAR package is damaged
5.4File verification toolThe use of
principle: Hash encryption algorithm
Hash algorithm contains many specific algorithms, the main difference between the specific algorithm is different encryption strength. Specific encryption algorithms include MD5, CRC32, and SHA1
Feature 1: It is irreversible and cannot be reversed from ciphertext.
Feature 2: When the specific encryption algorithm is determined, the length of the output data is fixed no matter how large or small the volume of the input data is. For example, the MD5 encryption result is always 32 bits. The output result is 32 bits regardless of whether the input is 1KB or 100 TB.
Feature 3: When the specific encryption algorithm is determined, the input data is slightly changed, and the output data is changed accordingly; The input data does not change, and the result is the same whenever the encryption is performed.
verifies the file principle
hash – based encryption is used as a file verification tool
concept A uses the classes in project B, then A depends on B. Maven project configures dependency information through coordinates
<! --> <dependencies> <! -- Dependency --> <dependency> <! GroupId >junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <! <dependency> <groupId>org.springframework</groupId> <artifactId> Spring-core </artifactId> < version > 4.0.0. RELEASE < / version > < / dependency > < / dependencies >Copy the code
establishes a dependency relationship between projects
<dependencies> <! Maven </groupId> <artifactId>Dependency_Dao</artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < / dependency > < / dependencies >Copy the code
describes the way in which a JAR package is searched in the local repository based on coordinates
< the groupId > com. Maven < / groupId > < artifactId > Dependency_Dao < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version >Copy the code
The root directory of the Maven repository is /com/maven/dependency_dao /0.0.1-SNAPSHOT/ pro18_dependency_DAo-0.0.1 – snapshot.jar
Maven looks for jar packages in Maven’s local repository based on the coordinates of the dependent jar packages. If the jar packages are not found, the dependency information cannot be resolved. Maven install command installs the Maven project into the local repository. dependent transitivity A relies on B, AND B relies on C. Does A use C directly without configuring the dependency information? Yes. depends on the dependency scope: compile scope: can deliver test scope: cannot deliver Provided scope: cannot deliver dependent scope compile: the default dependency scope. For code in the main directory: valid For code in the test directory: valid deployed to the server: valid test: JAR package dedicated for testing For code in the main directory: invalid For code in the test directory: valid deployed to the server: not participating provided: Jar package that represents “provided” to code in main directory: valid to code in test directory: valid deployed to server: not participated
< the dependency > < groupId > javax.mail. Servlet < / groupId > < artifactId > servlet - API < / artifactId > < version > 2.4 < / version > <scope>provided</scope> </dependency>Copy the code
dependency transfer excludes: excludes one of the JARS that the JAR package is passed to us when a dependency is proposed
<! <dependency> <groupId>org.springframework</groupId> <artifactId> Spring-core </artifactId> < version > 4.0.0. RELEASE < / version > < scope > compile < / scope > <! Exclusions > <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency>Copy the code
concept engineering A inherits engineering B. role harmonizes management of dependent information in a parent project. requires that the Maven project packaging method as a parent project must be POM. test
Specifies a parent project in a child project
<! -- Specify the parent of the current project --> <parent> <groupId>com</groupId> <artifactId> parent </artifactId> <version>0.0.1-SNAPSHOT</version> <! The relativePath to the parent project's pom. XML file --> <relativePath>.. /Parent/pom.xml</relativePath> </parent>Copy the code
PS: In this case, the child project will prompt that the groupid and Version are the same as the parent project and can be deleted.
Of course, you can create subprojects using Maven Modules
manages dependence and parent engineering
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> < artifactId > spring - the core < / artifactId > < version > 4.0.0. RELEASE < / version > < / dependency > < the dependency > < the groupId > org. Springframework < / groupId > < artifactId > spring - the context < / artifactId > < version > 4.0.0. RELEASE < / version > </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> < version > 4.0.0. RELEASE < / version > < / dependency > < the dependency > < groupId > org. Springframework < / groupId > < artifactId > spring - expression < / artifactId > < version > 4.0.0. RELEASE < / version > < / dependency > < the dependency > < the groupId > org. Springframework < / groupId > < artifactId > spring - JDBC < / artifactId > < version > 4.0.0. RELEASE < / version > </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> < version > 4.0.0. RELEASE < / version > < / dependency > < / dependencies > < / dependencyManagement >Copy the code
sub project
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
</dependencies>
Copy the code
dependency management benefits and (3) the developer does not need to look up the dependency information of a JAR package on the Internet, but can copy it directly from the parent project. The dependency information used throughout the project has a unified source and does not become cluttered. (3) To modify the versions of a group of JAR packages in a unified manner, modify the contents in the parent project instead of each child project. One change applies everywhere.
configuration properties
<! <properties> <atguigu.spring.version>4.0.0.RELEASE</atguigu.spring.version> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> < artifactId > spring - the core < / artifactId > < version > ${atguigu. Spring. Version} < / version > < / dependency >...Copy the code
6.3 Aggregation Concept fuses the various module projects together to form the project as a whole. role is proposed to make the project more modular and clearer in structure. Fixing one key installation. test
aggregation configuration
<! -- Configure aggregation --> <modules> <module>.. /Two/pom.xml</module> <module>.. /Three/pom.xml</module> </modules>Copy the code
to automatically straighten the installation sequence of an aggregation project by executing the install command
7 maven cool site mvnrepository.com/ maven.aliyun.com/mvn/search