This is the 8th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
preface
This post is an extension of the previous post, and Hei will do this by creating a Maven project from 0 to 1. If you’re new to Maven, follow my steps.
Here are the highlights of this issue:
- Create a Maven project from 0 to 1
- Maven directory structure
- Common Maven commands
- How to build a Fat Jar
- Maven Memory Settings
Maven hello world!
Call it Maven-Hello-world.
Create a project folder
A Maven project is essentially a POM file and a folder. Let’s first find a location and create a folder called Maven-hello-world.
Create a POM file
Next we create a pom.xml file in this folder.
<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.heiz</groupId>
<artifactId>maven-hello-world</artifactId>
<version>1.0.0</version>
</project>
Copy the code
This is a basic POM.xml file. In this file we define the GAV (groupId, artifactId, version) of this project. If others need to rely on this project, we can add our GAV to the dependency.
Test poM files
Now to make sure our file is correct, we can run the command to test it. Open the command line in the current directory and execute the following command:
mvn clean
Copy the code
The MVN clean command clears all previous temporary build files from the project directory. Because our project is brand new, no files need to be deleted. If the command line outputs the following success message, our POM.xml is fine.
Create a Java source directory
Now we are ready to create a directory of Java source files. Create folders in the following hierarchy.
- src
- main
-java
Copy the code
Create a Java file
Then we create one and another folder in SRC /main/ Java as a package for our code, let’s call it Hello, and hello/ to create the first Java code file helloWorld.java.
package hello;
public class HelloWorld {
public static void main(String args[]){
System.out.println("Hello World, Maven"); }}Copy the code
Build the project
Now we can use the Maven command to package our project. Go back to the project root directory maven-hello-world. Go to the cli and run the following command:
maven package
Copy the code
This command will compile the Java source file and create a JAR file containing the compiled Java classes. A target folder is created in the project root, where the JAR files are stored, as well as many temporary files (for example, a class directory containing all the compiled classes).
The JAR file name format is artifactid-version, so our project JAR package name is maven-hello-world-1.0.0.jar.
Maven directory structure
Maven has a standard directory structure, which is useful to follow and makes it easier for others to understand the purpose of each directory when they take over your project.
Standard directory structure
Here is one of the most commonly used Maven directory structures, with some less commonly used ones I’ve omitted here.
project_dir
- pom.xml
- .mvn
- jvm.config
- src
- main
- java
- resources
- test
- java
- resources
- target
Copy the code
Project_dir is the root directory for our project.
.mvn is a directory that holds Maven configuration files, such as jv.config files, that can be used to configure the JVM Maven uses to build projects. You can set Maven memory limits in this file.
SRC is the root directory for the application and test source code.
SRC /main/ Java stores the application source code. Any resource files your application needs (such as properties files) are placed in the SRC /main/ Resources directory. Resource files can be loaded by classpath.
SRC /test/ Java stores the test source code. Any resource files needed to test the code (such as properties files) are placed in SRC /test/ Resources. It can also be loaded by classpath.
Target contains all the output of the Maven build project. Target also contains temporary and intermediate files that Maven needs when building the application.
You can also visit the Maven website for more complete catalog information. Maven official file directory description
Maven command
Maven contains a large number of commands that can be executed. The Maven command is a mix of Life Cycles, Phases, and Goals, so it’s sometimes a little hard to understand. Therefore, IN this installment, I will describe common Maven commands and explain the Life Cycles, Phases, and Goals they are executing.
Common Maven commands
Here are some of the most common Maven commands and their functional descriptions.
Maven command | Description |
---|---|
mvn –version | Print maven version |
mvn clean | Clears the build result in the target directory |
mvn package | Build the project and place the generated JAR files in the target directory. |
mvn package -Dmaven.test.skip=true | Build the project and place the generated JAR files in the target directory. — Do not run unit tests during build. |
mvn clean package | Clear the Target directory first, then build the project and place the generated JAR files in the Target directory. |
mvn clean package -Dmaven.test.skip=true | Clear the Target directory first, then build the project and place the generated JAR files in the Target directory. — Do not run unit tests during build. |
mvn verify | Run all integration test cases in the project |
mvn clean verify | Clear the Target directory, then run all integration test cases in the project |
mvn install | Build the project and save the generated JAR packages to your local Maven repository |
mvn install -Dmaven.test.skip=true | Build the project and save the generated JAR packages to a local Maven repository without running unit tests |
mvn clean install | Clean up the target directory, build the project, and save the generated JARS to your local Maven repository |
mvn clean install -Dmaven.test.skip=true | Clean the target directory, build the project, and save the generated JAR package to a local Maven repository without running unit tests |
mvn dependency:copy-dependencies | Copy dependencies from the remote Maven repository to the local Maven repository. |
mvn clean dependency:copy-dependencies | Clear the project and copy the dependencies from the remote Maven repository to the local Maven repository. |
mvn clean dependency:copy-dependencies package | Clean up the project and copy the dependencies from the remote Maven repository to the local Maven repository, then package the project. |
mvn dependency:tree | Print out a dependency tree for the project based on the dependencies configured in the POM.xml file. |
mvn dependency:tree -Dverbose | Print out a dependency tree for the project based on the dependencies configured in the POM.xml file. Including repeated transitive dependencies. |
mvn dependency:tree -Dincludes=com.fasterxml.jackson.core | Print out dependencies in the projectcom.fasterxml.jackson.core The dependency of. |
mvn dependency:tree -Dverbose -Dincludes=com.fasterxml.jackson.core | Print out dependencies in the projectcom.fasterxml.jackson.core , including repeated transitive dependencies. |
mvn dependency:build-classpath | Output the clasSPath of the dependency based on the dependency configured in the POM.xml file. |
Note that when maven’s clean command is executed, all files in target are deleted, which means that previously compiled and built classes are lost. If the project is large, it may take more time to build. However, before a project is deployed, clean is generally performed to ensure that everything has been rebuilt.
Build Life Cycles, Phases and Goals
The relationship between the three can be shown as follows:
Maven contains three main Build Life Cycles:
- clean
- default
- site
Phases are included in each Build Life Cycles, and Goals are included in each Phases.
It can be understood that Maven divides the tasks that need to be done according to granularity. Goals are grouped into Pahses, and Phases are grouped into Life cycles.
Build a Fat Jar
First, what is a Fat Jar? Let’s use our Maven-Hello-world project as an example.
Suppose I now need to add Guava to my project dependencies, let’s add Guava’s dependencies to POM.xml.
<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.heiz</groupId>
<artifactId>maven-hello-world</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
</dependencies>
</project>
Copy the code
Then we use MVN package for packaging. Maven-hello-world-1.0.0. jar will be found in the target directory. If you open the jar using the decompression tool, there will be no guava related files in the jar.
So in some scenarios, such as when we need to publish a microservice, if we don’t package the dependencies into a JAR file, then we need to upload the other dependencies to the service separately, which is cumbersome. It would be convenient to be able to package all the dependencies and project code together into a JAR file and start the service with only one file to upload.
Fat Jars are JAR files that bundle all dependencies together.
To build a Fat JAR, we need to modify our POM.xml. By including maven-assembly-plugin in the plugins section of the POM file:
<build>
<finalName>maven-hello-world</finalName>
<plugins>
<! -- other Maven plugins ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<! -- Specify the main class to run -->
<mainClass>hello.HelloWorld</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Copy the code
The element in the Maven-assembly-plugin configuration contains descriptorRef, which tells Maven how to assemble.
Jar-with-dependencies builds a JAR file with dependencies, also known as Fat jars.
Execution indicates the phase and target at which the plug-in should be executed.
Add the above configuration to our Maven-Hello-world pom.xml file and execute the MVN Clean Package again. After successful execution, a Fat Jar appears in the target directory.
Maven Memory Settings
If you are building a large Maven project, or if your computer has a small amount of memory, you may need to adjust Maven’s memory limits. From Maven version 3.3, you can set memory limits in the Jvm.config file. Jvm.config is located in the.mvn directory of the project directory.
In the Jvm. config file, you can control Maven’s memory limits with the following parameters:
-Xmx2048m -Xms1024m
Copy the code
Memory usage can be controlled by adjusting the -xmx and -xms values.
The last
That’s all for this installment. Next installment will focus on Maven dependency issues. If it’s helpful, give me a thumbs up!