Get to know Maven quickly

Maybe I was not talented. When I first met maven, I was confused by various unknown tutorials. Later, I found that most functions of Maven were not as difficult as I thought.

This article is for Maven beginners, hoping to let them understand Maven as quickly as possible and enjoy a series of benefits it brings.

[a simple question]

Before the explanation, ask a simple question.

If you are developing two Java projects under eclipse, let's call them a and B, and some functions in project a depend on some classes in project B, how to maintain this dependency?

The author did this before using maven, packaged project B as a jar, and imported the jar file of project B under the library of project a.

This has obvious disadvantages:

1. If a bug in B is found during development, you must repackage B and recompile project a

2. In the collaborative development of project a, in order to ensure its normal operation, either package B into a, or publish B as well and inform the developer of the use method. Obviously, these two methods are not very good. The former may cause a waste of resources (for example, developers may be developing other projects that rely on B, and B has been stored locally), while the latter will bring risks to the normal operation of the project (once the work is handed over to labor, it is bound to bring a certain failure rate).

Therefore, such manual control of dependencies between projects is obviously a 'poor' method.

[introduction to Maven]

According to the author, Maven's core function is to reasonably describe the dependencies between projects.

All Maven projects contain a project called POM XML file, in which fields such as < groupid > < artifactid > < version > are recorded. These fields are filled in when creating Maven project, and Maven will locate the project according to them.

Another key tag in POM is < dependencies >, which can contain several < dependency > tags. Under < dependency >, there are dependent fields such as < groupid > < artifactid > < version > introduced above, which determine a unique project version.

A typical POM XML [from project luajava]:

[Maven library]

Take projects a and B just now as an example. Write < dependency > associated project B in the POM file of project A. Maven will find project B in the workspace and create dependencies. At this time, project a can directly access the class resources in project B, and the breakpoint set in project B will still be hit when debugging a.

In addition to the dependent association of the workspace, Maven can also associate an item in a remote image. By default, if there is no corresponding item record in the workspace, Maven will connect to the central warehouse to find it. If it is found, It will download the project to the local library (the local library in windows is located in the. M2 directory of the user document), and the association is automatically set. This is a very convenient feature. When we need additional jar package support, we no longer need to download and import manually. We just need to paste the dependent words corresponding to the project into the appropriate location of POM, and leave the rest to Maven.

[an example]

So, how do we know the fields such as groupid of the required project?

Generally speaking, if the official project supports maven, there will be a section of dependent fields on its page for copying, but there are cases where Maven is supported but no dependent fields are given. The Maven features of other projects are maintained by a third party. At this time, it is difficult to obtain Maven support from official channels.

Therefore, the preferred solution for query dependent fields is to use Maven search service for search.

Here I recommend an address: mvnrepository com

The following is an example of using eclipse to associate the MySQL jdbc driver. Before that, please confirm that it supports Maven in help - > about Eclipse:

In the new official version of eclipse, Maven support has been built in. If the version is older, you can install Maven plug-in.

Let's create a maven project, new - > Project - > Maven project - > create a simple project (ski arch type selection)

The following is an important step. Fill in the dependent fields such as groupid of this item. The fields filled in here will be used when other items depend on them. The unfilled parts are optional:

After that, we went to MVN repository Com search "MySQL JDBC"

Soon, we found the results. Click to view the detailed version information:

Several versions and usage of MySQL JDBC are listed here. Here we choose the second new version 5.1 34:

Copy the contents of Maven box and write them to < dependencies > in POM:

After reading the progress note, you can see more Maven dependencies in the project, where you can find the jdbc driver package we just introduced:

[other features]

1. Image source

Obviously, the Maven central warehouse has a large load and is not in China, so the speed is very slow, and sometimes we can't even find dependence. Therefore, especially in the case of small-scale team development, building a maven private server will greatly improve efficiency. Here is a maven image building tool: nexus: http://www.sonatype.org/nexus/

Using it, you can quickly and easily set up a private Maven image source, and then in Maven's settings XML.

2. Download SRC source code

In some cases, we need to refer to the source file of the dependent project. At this time, we can try to double-click the class file directly. If there is a source file in the Maven image source, it will be automatically downloaded locally, and eclipse will automatically load and display it.

3. Compilation parameter configuration

The author uses the eclipse Luna version. Maven's default Java compiled version is 1.5. We can specify the compiled version in POM and upgrade it to 1.7 (refer to the build tab in luajavapom). In addition, these settings will also be read and configured appropriately by automation tools such as Jenkins continuous integration.

summary

The above is all about getting to know Maven quickly in this article. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>