Apache Maven from entry to heaven

Like to point a praise chant!
GitHub project JavaHouse synchronous included

1 introduction

In the daily development of Java, Maven should be an essential tool, of course, some people use the Gradle. So what Maven is Gesha things? It is not dependent on the introduction of a tool? I believe many people like me the same feeling. But in general, I feel things are often unreliable. I collected some information on the Internet, looking through the official website, rearranged a bit Maven information.

2 Maven Introduction

Wikipedia says Maven is a software project management and automated build tools. Just from the literal meaning, I do not know if this is what you want to express meaning. According to my understanding, Maven is actually a development tool, complementary with IDEA. We compiled daily use IDEA project, in fact, use Maven, but these operations are strong IDEA hidden cause we use IDEA, Maven, and there is no illusion of contact.

3 Maven command

He said before the command line, let me say a few hand-professional terms.

name Explanation
groupId Package names
artifactId project name

groupId of artifactId known as the coordinates, indeed, once their value is determined, we can find them based on their coordinates.

create

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

Such maven automatically create a Maven project with the hello world. Package name is com.mycompany.app; project name is my-app. See here, we know why Maven is a project build tool. I personally feel that Maven with the tip of a large npm tool somewhat similar.

Compile

mvn compile

The command from the literal meaning is to compile the command stream know, he is not compiled test folder inside the source. Execute the command will generate a target file. Folder inside the content is compiled.

Remove

mvn clean

This command is mainly target file generated after clearing compiled clip.

Bale

mvn package

This command will default project labeled jar package, of course, we can also be labeled as war package.

installation

mvn install

This command can project labeled jar, into the local repository.

4 warehouse

To understand Maven, warehouse concept is essential. Let me (soul painting hand) draw a map.

Project dependencies are required to get from the local repository, the local repository is your own computer, we introduced various dependencies are placed inside a local warehouse, typically rely on a problem, then, it is not dependent on local repository. We can put local dependence corresponding delete in a try, perhaps on it.
From this, we can see that self storage, that is, we say, PW, you can store some of the basic package that we developed, introduced from reliance on self-built warehouse faster, of course, if there is no dependent self storage we need, since the warehouse will send a request, the request remote central warehouse, central warehouse would not really gone.

5 POM

Our important task now is played --POM. Maven project will have a pom.xml file.

This is one of the most simple pom file. This pom file we should be very familiar with, it is dependent on the introduction of lead from here. Nothing to say, so say others.

Package Type

// war
<packaging>jar</packaging>  

The decision to label the jar package or item is war package. In general, SpringBoot project will be labeled as war package, and then run into the tomcat. Of course, you can run the jar package, after all SpringBoot built tomcat, strong batch.

Add to the mix

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

If we look at the file, such as xml suffix mapper file in the resources inside, compile time will find that they have not translated into, because Label default is false, we need true.

He removed

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

Deployed to remote

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

When we made good wheels, to deploy to remote central warehouse, you add the above tag. While (at Maven installation directory) setting.xml add the following

<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

If we want to build a few maven project, there is no problem. For example there are two modules (my-app, my-webapp), the following directory

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

You need to add a top-level pom.xml Label, 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 to do a boss (webapp), join my-app relies on his pom file

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

Then the two projects (webapp, my-app) is added to the top of the pom file inside label

<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>
 ...

最后执行 mvn verify 命令,就会在老大项目(webapp)的 target 文件夹里生成 war 包。

参考

  • https://zh.wikipedia.org/wiki/Apache_Maven
  • https://maven.apache.org/
  • https://maven.apache.org/guides/getting-started/index.html
  • https://maven.apache.org/guides/mini/guide-configuring-maven.html
  • https://www.cnblogs.com/luotaoyeah/p/3791966.html

关注微信公众号,随时移动端阅读

公众号.jpg

Guess you like

Origin www.cnblogs.com/chenzhuantou/p/12032704.html