Apache Maven from Getting Started to Ascending

Keywords: PHP Maven Apache xml snapshot

If you like, order a compliment!
GitHub Project JavaHouse Synchronized recording

1 Introduction

Maven should be an essential tool in everyday Java development, and of course Gradle is used.So what exactly is Maven?Isn't it just a dependent import tool?I believe many people feel the same way as me.But in general, what you feel is often unreliable.I gathered some data online, flipped through the official website, and rearranged Maven's data.

Introduction to 2 Maven

Wikipedia says Maven is a software project management and automation tool.Literally, I don't know what this means.As I understand it, Maven is actually a development tool that complements IDEA.When we compile projects with IDEA on a daily basis, we are actually using Maven, but these operations are hidden by the powerful IDEA, which gives us the illusion that we are using IDEA and have no connection with Maven.

3 Maven's Commands

Before I say the command line, let me say a few professional terms.

Name explain
groupId Package Name
artifactId entry name

groupId's artifactId is known as coordinates, and indeed, once their values are determined, we can find them based on their coordinates.

Establish

mvn -B archetype:generate 
-DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app    
-Dartifac t Id=my-app

This automatically creates a Maven project with hello world.The package name is com.mycompany.app; the project name is my-app.Seeing this, we know why Maven is a project-building tool.I personally think Maven has something similar to the npm tools on the big front end.

Compile

mvn compile

This command knows from the literal stream whether it compiles the command, or whether it compiles the source code in the test folder.Executing this command generates a target file.Inside the folder is the compiled content.

Eliminate

mvn clean

This command mainly clears the compiled target folder.

Pack

mvn package

This command will default the project to a jar package, but we can also make a war package.

install

mvn install

This command jar s the project and places it in the local warehouse.

4 Warehouses

To understand Maven, warehouse concepts are essential.Let me draw a picture.

The dependencies needed for a project are all taken from the local warehouse, which is its own computer. All the dependencies we introduce are placed in the local warehouse. If the dependencies are problematic, the dependencies of the local warehouse are not.We can delete the local dependency first, try it, maybe it will be OK.
From this, we can see that the self-built warehouse, also known as private service, can store some basic packages that we have developed. It is faster to introduce dependencies from the self-built warehouse. Of course, if there is no dependency we need in the self-built warehouse, the self-built warehouse will make a request to request a remote central warehouse. If there is no central warehouse, it will be really gone.

5 POM

Now it's time for our important mission -- the POM. Maven project will have a pom.xml file.

This is the simplest pom file.This pom file should be familiar to us, and this is where the dependencies are introduced.There's nothing to say, so say something else.

Package Type

// war
<packaging>jar</packaging>  

This tag determines whether the item is a jar package or a war package.Generally speaking, SpringBoot projects are packaged into wars and run in tomcat.Of course, jar packages can also run, since SpringBoot has Tomcat built in, a strong batch.

Add in

<build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
</build>

If we place the following files, such as mapper files with xml suffixes in resources, they will not be compiled when compiling because the <filter>tag defaults to false and we need to be true.

Remove him

<exclusions>
    <exclusion>
      <groupId>sample.ProjectE</groupId> <!-- Exclude Project-E from Project-B -->
      <artifactId>Project-E</artifactId>
    </exclusion>
</exclusions>

Deploy to remote

<distributionManagement>
    <repository>
      <id>mycompany-repository</id>
      <name>MyCompany Repository</name>
      <url>scp://repository.mycompany.com/repository/maven2</url>
    </repository>
</distributionManagement>

When we have finished building the wheels and want to deploy them to a remote central warehouse, add the label above.Also add the following in setting.xml (under the directory where Maven is installed)

<servers>
    <server>
      <id>mycompany-repository</id>
      <username>jvanzyl</username>
      <!-- Default value is ~/.ssh/id_dsa -->
      <privateKey>/path/to/identity</privateKey> (default is ~/.ssh/id_dsa)
      <passphrase>my_key_passphrase</passphrase>
    </server>
 </servers>

Multiple modules

It would also be fine if we wanted to build several maven projects at a time.For example, there are two modules (my-app, my-webapp) in the following directory

+- pom.xml
+- my-app
| +- pom.xml
| +- src
|   +- main
|     +- java
+- my-webapp
| +- pom.xml
| +- src
|   +- main
|     +- webapp

The <modules>tag needs to be added to the top-level pom.xml as follows

<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>com.mycompany.app</groupId>
  <artifactId>app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
 
  <modules>
    <module>my-app</module>
    <module>my-webapp</module>
  </modules>
</project>

Then choose a webapp to add my-app dependency to his pom file

<dependencies>
    <dependency>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    ...
</dependencies>

Then the top <parent>tag is added to the pom files of the two items (webapp, my-app)

<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">
  <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>app</artifactId>
    <version>1.0-SNAPSHOT</version>
 </parent>
 ...

Finally, executing the mvn verify command generates the war package in the target folder of the big project (webapp).

Reference resources

Watch the WeChat Public Number and Read on Mobile

Posted by dennissanche on Thu, 12 Dec 2019 18:03:12 -0800