In the previously developed java applications, we used the lib package to import the required dependencies. However, as the program becomes more and more complex and the project becomes larger and larger, we need more and more dependent packages. All of them are copied to lib package, which obviously can not meet our needs. Is there a better way to manage our dependencies? Of course, there are. Maven and Gradle are popular in the market. This article will introduce the installation and use of Maven and Gradle.
What is Maven
Maven is a tool for building and managing any Java based project. It can manage the dependencies of the project and package the project into the jar package type we need.
Maven installation and configuration
Click to jump to Maven's official website for download
After downloading, unzip it.
Next, we configure environment variables for Maven. Similar to Java environment configuration.
Add MAVEN_HOMEļ¼Path
After the environment variables are configured, let's check whether it is successful.
Now configure Maven
We found the directory where Maven was installed (the extracted directory)
Address of Alibaba cloud Maven warehouse: https://developer.aliyun.com/mvn/guide
After the configuration is completed, save and close.
Next, we configure Maven information in IDEA
In this way, IDEA configures Maven.
Use IDEA to create Maven project project. For example, Java operates excel files.
Previously, we managed our third-party dependencies by creating a new lib directory and importing dependency packages. After using Maven, we only need a label, which can add many dependencies.
Person class, the code is as follows
public class Person { private String name;//name private String city;//city private String cellPhone;//phone number public Person(){ } public Person(String name, String city, String cellPhone) { this.name = name; this.city = city; this.cellPhone = cellPhone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCellPhone() { return cellPhone; } public void setCellPhone(String cellPhone) { this.cellPhone = cellPhone; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", city='" + city + '\'' + ", cellPhone='" + cellPhone + '\'' + '}'; } }
The code of the ExcelWriter class is as follows:
public class Person { private String name;//name private String city;//city private String cellPhone;//phone number public Person(){ } public Person(String name, String city, String cellPhone) { this.name = name; this.city = city; this.cellPhone = cellPhone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCellPhone() { return cellPhone; } public void setCellPhone(String cellPhone) { this.cellPhone = cellPhone; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", city='" + city + '\'' + ", cellPhone='" + cellPhone + '\'' + '}'; } }
Run the main method in the ExcelWriter class. The effect is as follows:
Now we can use maven to package the project into executable jar packages
Maven assembly plugin link
Copy the following information to POM XML file
Next, package it into a jar package
If we don't want the jar package to have a suffix, we can add it
false