Maybe I was not talented, and when I first met Maven, I was confused by various unknown tutorials. In my later use, I found that most of the functions of Maven were not as difficult as I imagined.
This article is for beginners of Maven, hoping to let it understand Maven as quickly as possible and enjoy the series of benefits it brings.
[A simple question]
Before giving an explanation, ask a simple question.
If you are developing two Java projects under Eclipse, and call them A and B, some functions in project A depend on certain classes in project B, how to maintain this dependency?
I did this before using Maven, packaged Project B as a jar, and imported the Jar file of B under the Library of Project A.
This has obvious disadvantages:
1. If a bug in B is found during the development process, B must be repackaged and recompiled for project A.
2. When collaborating on developing project A, in order to ensure that it can run normally, either choose to package B into A, or choose to publish B like B and inform the developer of how to use it. Obviously, both methods are not very good. The former may cause waste of resources (for example, the developer may be developing other projects that rely on B, which has been stored locally), while the latter will bring risks to the normal operation of the project (if the work is handed over to manual work, it will be destined to bring a certain failure rate).
Therefore, manually controlling the dependencies between projects in this way is obviously a 'bad' approach.
[Introduction to Maven]
According to the author's opinion, Maven's core function is to reasonably describe the dependencies between projects.
All Maven projects contain a file named pom.xml, which records its own <groupId><artifactId><version> and other fields. These fields are filled in when creating a Maven project, and Maven will locate the project based on them.
Another key tag in a pom is <dependencies>, which can contain several <dependency> tags, and under <dependency> are the dependency fields such as <groupId><artifactId><version> introduced above, which determine a unique project version.
A typical pom.xml [from project luajava]:
<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>org.keplerproject</groupId> <artifactId>luajava</artifactId> <version>1.1</version> <packaging>jar</packaging> <name>luajava</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </project>
[Maven library]
Taking the A and B projects just now as an example, write <dependency> in the pom file of project A, and Maven will find the B project 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 during debugging of A.
In addition to workspace dependency association, Maven can also associate projects in a remote image. By default, if there is no corresponding project record in the workspace, Maven will connect to the central repository to look up. If 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 automatically set the association. This is a very convenient feature. When we need additional jar package support, we no longer need to manually download and introduce it. We only need to paste the dependency fields corresponding to the project to the appropriate location of the pom, and leave the rest to Maven to complete.
[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 generally be a dependency field on its page for copying, but there is also a situation where Maven is supported but no dependent fields are given. The Maven features of other projects are maintained by third parties, and it is difficult to obtain support for Maven from official channels.
Therefore, the preferred solution for querying dependency fields is to use the Maven search service to search.
Here I recommend an address: mvnrepository.com
The following is an example of using Eclipse to associate the MySqlJDBC driver. Before this, please confirm that it supports Maven in Help->AboutEclipse:
In the official new version of Eclipse, support for Maven is already built-in. If the version is older, you can install the Maven plug-in.
Let’s first create a new Maven project, New->Project->MavenProject->Createasimpleproject(skiparchetypeselection).
The following is the most important step. Fill in the dependency fields such as GroupId of this project. The fields filled in here will be used when other projects depend on them. The unfilled parts are optional:
After that, we searched for "MySql JDBC" at mvnrepository.com
Soon, we found the result and clicked to view the detailed version information:
Here are the list of multiple versions and usage of MySql JDBC. Here we choose a second new version 5.1.34:
Copy the contents in the Maven box and write them to the pom's <dependencies>:
<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.test</groupId> <artifactId>maventest</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> </dependencies> </project>
After the Progress reading bar is over, you can see that there are more Maven Dependencies in the project, and you can find the jdbc driver package we just introduced:
[Other features]
1. Mirror source
It is obvious that the Maven central warehouse has a relatively large load and is not in China, so the speed is very slow and sometimes the dependencies cannot be found. Therefore, especially in small-scale team development occasions, building a Maven private server will greatly improve efficiency. Here is a Maven image building tool: Nexus: http://www.sonatype.org/nexus/
Use it to quickly and easily build a private Maven image source, and then configure it in Maven settings.xml.
2. Lazy download of src source code
At some times, we need to refer to the source file that depends on the 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 to the local area, and eclipse will automatically load and display.
3. Compile parameter configuration
The author uses EclipseLuna version. The default Java compilation version of Maven is 1.5. We can specify its compilation version in pom to improve it to 1.7 (see the build tag in luajavapom); in addition, these settings will also be read and properly configured by automation tools such as jenkins continuous integration.
Summarize
The above is all about quickly understanding Maven. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!