What is Maven

Maven is an open source Project management tool written in pure Java. Maven uses a concept called Project Object Model (POM) to manage projects. All Project configuration information is defined in a file called POM.xml.

Maven is a cross-platform project management tool. Maven is an open source project…

Maven is a Java platform based project building, dependency management and project information management…

How does POM understand??

  • We use Java for object-oriented programming, and objects are our classes
  • Maven is project-oriented, managing our project as an object

Why use Maven?

Before using Maven, was our JAR package very cluttered? Take a look at where I keep the JAR packages

Before LEARNING Maven, I thought about sorting out the jar packages one by one, in case the hard drive broke, I would have to find the JAR packages again. And because of the jar package version of the problem, has suffered a lot of losses… This will take a lot of time to sort through jar packages…

Intellij Idea does not support this function. I haven’t found it yet…. So can only rely on the native JAR command to jar package, after a while, want to jar package again, forget the command… Very distressed…

Maven does both of these things very well. Of course, there’s more to Maven than that. I’m just giving you two small examples… As we learn, you will see the power of Maven…

Maven manages the entire life cycle of a project, including cleaning, compiling, testing, reporting, packaging, deployment, and more.

Automated build projects

  • Software development: feasibility analysis, requirement analysis, software design, software development, release, operation and maintenance
  • Software construction: The software has been developed and needs to be built as a product for release construction steps:
  • Clear –> Compile –> Test –> Report –> Package (JAR \war) –> Install –> Deploy to remote Maven automatically builds software projects with a single command

Maven has the idea that convention is better than configuration, that is, you put some specifications down and people use them! If we agree in advance on the directory structure for all projects, the standard development process (compile, test), everyone follows it. Software project management becomes much easier. For example: how to cross the road (red light stop green), how to open the door, close the door, etc.

Install Maven

Go to the official website to download Maven software, decompress after the installation is complete…

Configuring environment Variables

Because Maven is written in Java, to use Maven, first configure the Java environment variables…

To check whether the Maven installation is successful, click mvn-version on the console…

Maven directory structure

  • Bill bin: a script that runs the MVN
  • Queue Boot: Contains the Plexus-Classworlds class loader framework
  • Conf: Includes the settings. XML configuration file
  • Queue lib: Contains Java libraries required by the Maven runtime
  • User library: ${user.home}/.m2/repository
  • The default repository for Maven is downloaded at: Maven-model-Builder-3.0.4.jar in the Maven-model-builder-3.0.jar directory

In Maven, we have the concept of a local repository. We can change the location of the local repository in the settings. XML configuration file, for example, if I put the local repository on the desktop


IDEA using Maven


For details, see the Maven tutorial -Idea environment

It’s worth adding that the blog post doesn’t give absolute paths when it comes to changing the web.xml path… There may be some people who don’t know how to write this. I’ll give it to you for reference

X:\Users\ozc\Desktop\zhongfucheng3\src\main\webapp\WEB-INF\web.xml
Copy the code

Maven terminology

In our previous article, we saw how Maven is used under Intellij Idea. The resulting directory structure looks like this:

The directory structure above is what Maven calls a “convention”. We use Maven to build Java projects with this directory structure…

Let’s take a look at some of the commonly used terms for Mavaen

warehouse

Local repository

  • Maven stores the jars and other resources that we build for our project in a local repository. Maven is also the first place to look for jars in the local repository when we need them

Central warehouse:

  • ** When Maven can’t find the jar we need in the local repository or private server, it will download the jar in the central repository for us **. So how does Maven know where to download it? Maven is already configured

Apache-maven-3.2.1 \lib\ maven-model-build-3.2.1 \org\apache\ maven-model has been configured in the pom.xml file under apache-maven-3.2.1\lib\maven-model

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

Copy the code

Private server

  • The server that stores some JAR packages, since the local repository may not have most of the JAR packages, a team to develop will inevitably go to the central repository to download, which is very time consuming. If private servers own these JARS, then every computer doesn’t have to go to a central repository to download them…

Maven software build lifecycle

The software life cycle is the following

-** Clear –> Compile –> Test –> Report –> Package (JAR \war) –> Install –> Deploy **

Maven life cycle command plug-in

When we develop in the IDEA environment, the plugins for the Maven lifecycle are very clear…

Maven coordinates

  1. Coordinate composition: groupId + artifactId+ Version
  • GroupId: groupId, organization name, company name: like company id, or company package name alibaba — “gode –” 5.01 edition
  • ArtifactId: Build ID, product name, or product ID
  • Version: indicates the version number
  1. Jar package: artifactid-version.jar

Pom. XML is introduced

The Project Object Model is maven’s core configuration file

Once we’ve covered the above terminology, the following POM.xml configuration file should be familiar.


<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>zhongfucheng3</groupId>
    <artifactId>zhongfucheng3</artifactId>
    <version>1.0 the SNAPSHOT</version>


</project>
Copy the code

If we want our project to have a Junit test kit, we can just specify the corresponding JAR dependencies..

The default is compile. What is the difference between test and compile?

  • Indirect dependencies: Dependent jars must be in the compile scope. If the test scope is in the compile scope, then the published JAR packages will not contain the test scope dependent JARS, and dependencies

<scope>test</scope>
Copy the code

Maven [private repository, upload jar package, reference private jar package, upload local project to private server]

As mentioned earlier, if we need to import the corresponding JAR package using Maven, Maven will first look in our local repository -> private repository -> central repository…

However, our local repository often does not have the JAR package we want, and we often go to the central repository to download it, which is a waste of time and resources… So we usually have a private warehouse…

In addition, some companies do not provide Internet access to project staff, so they cannot use Maven to access remote warehouse addresses. Therefore, it is necessary to find a machine on the LAN that has Internet access, set up the Nexus private server, and then connect the developer to this private server. This allows you to access Maven’s remote repository from the nexus server

Ready to step

We used the Nexus framework, which belongs to sonatype’s open source framework, to set up maven private servers

Nexus Environment Setup

  • Step 1: Download the nexus-Webapp-1.9.2.4. war package and copy it to the Webapps directory under Tomcat
  • Step 2: Start Tomcat The default login password is admin admin123
  • Step 3: Visit http://localhost:8080/ project name /

Tomcat automatically resolves the WAR package and then downloads the plugin…

Visit the project to the home page

Log in…

The warehouse is introduced

  • Proxy: indicates the proxy of the remote warehouse. For example, when a user requests an artifact from a Central Repository proxy in the Nexus, the proxy searches for an artifact locally, and if it doesn’t find one, it downloads it from the remote repository and returns it to the user, acting as a conduit
  • Hosted: A Hosted repository where users can deploy some of their components to the Hosted, or manually upload components to the Hosted. For example, the Oracle driver, ojdbc6.jar, is not available in Central Repository and will need to be manually uploaded to Hosted
  • Group: is a repository Group. Maven does not have this concept. It is specific to Nexus. The goal is to aggregate these repositories and expose the user to a single address, so that the user doesn’t need to configure multiple addresses in the POM, just configure the group’s address. The Repository Path on the right can be clicked to see a list of artifacts in the Repository. Be careful with the browser cache, though. I found out today that the component has been updated, but I can’t see it in the browser. I thought it was a BUG, but in fact it was cached by the browser
  • Virtual: also a central warehouse image, supporting older versions of M1

The role of each warehouse:

  • 3rd Party: Artifact repository for a third party release that is not available from a public repository
  • Apache Snapshots: Bug stores that report Snapshots taken by proxy Apache Repositories
  • Central: A repository used to represent the release artifacts in Maven’s Central repository
  • Central M1 Shadow: A component mirror repository used to provide releases in the M1 format in the Central repository
  • Codehaus Snapshots: Stores that call Snapshots of CodehausMaven repositories
  • Releases: A host-type repository used to deploy and manage internal release artifacts
  • Snapshots: Host-type repositories for deploying and managing internal snapshot version artifacts

A warehouse group is a virtual concept, which exposes a unified address externally by aggregating the physical warehouse (proxy or Hosted). Note that the one on the left is the one that will be aggregated

Warehouse group configuration order:

Upload jar package to private server

Upload pig-0.12.1.jar from your desktop directly to your private server

When I checked it again, it had been uploaded

Rely on jar packages from private servers

Configure the private server path, group ID, version, and so on in the POM.xml file



    <! If there is no private server, then there is no private server, then there is no private server, then there is no private server, then there is no private server.
    <! The jar package used by the third party project is loaded.
    <repositories>
        <repository>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <id>public</id>
            <name>public</name>
            <url>http://localhost:8080/nexus-2.6.2/content/groups/public/</url>
        </repository>
    </repositories>

    <! Maven life cycle plugin jar -->
    <pluginRepositories>
    <pluginRepository>
        <releases>
            <updatePolicy>never</updatePolicy>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <id>public</id>
        <name>public</name>
        <url>http://127.0.0.1:8080/nexus-2.6.2/content/groups/public/</url>
    </pluginRepository>
    </pluginRepositories>
Copy the code

In the local repository also private server JAR package download down

Publish local projects to private servers

The server configuration can be specified in Maven’s settings.xml


  <! -- Distribution management, publishing jar packages to private server -->
 <! -- Configure server -->
	 <server>
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
	<server>
      <id>snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    
Copy the code

Note that you need to be careful which Maven you are using, whether you download Maven yourself or use Maven integrated with Idea. If you do not configure Maven, you will get a 401 error.



  <distributionManagement>
  <! Release to snapshot repository, i.e. test repository -->
  <snapshotRepository>
  <id>snapshots</id>
  <url>http://127.0.0.1:8080/nexus-2.6.2/content/repositories/snapshots/</url>
  </snapshotRepository>
  <! Publish to the 3rd party repository as well as to the release repository
  <repository>
    <id>releases</id>
  <url>http://127.0.0.1:8080/nexus-2.6.2/content/repositories/releases/</url>
  </repository>

Copy the code

Look again at the warehouse group


conclusion

  • Maven managed our project very well, and managed our JAR packages. This is handy when building projects.
  • You need to configure the JAVA_HOME environment variable to install Maven.
  • Settings.xml configures our local repository location.
  • Too much going to the central repository to load jars can have a significant impact on performance. If we build a private repository, if the private repository has the corresponding JAR package, we can directly find in the private repository.
  • Learn how to upload jar packages to private repository pages
  • Learn how to associate a private repository jar package on a project
  • Learn how to upload local projects to a private repository.

If the article has the wrong place welcome to correct, everybody exchanges with each other. Students who are used to reading technical articles on wechat and want to get more Java resources can follow the wechat public account :Java3y