Install Maven
Maven is downloadable as a zip file at https://maven.apache.org/download.cgi.
Once you have downloaded the zip file, unzip it to your computer. Then add the bin folder to your path.
|
|
Define a simple Maven build
Maven projects are defined with an XML file named pom.xml. Create a file named pom.xml at the root of the project (i.e. put it next to the src folder) and give it the following contents:
|
|
<modelVersion>. POM model version (always 4.0.0).<groupId>. Group or organization that the project belongs to. Often expressed as an inverted domain name.<artifactId>. Name to be given to the project’s library artifact (for example, the name of its JAR or WAR file).<version>. Version of the project that is being built.<packaging>- How the project should be packaged. Defaults to “jar” for JAR file packaging. Use “war” for WAR file packaging.
Build Java code
compile the project’s code
|
|
run the package
|
|
The package goal will compile your Java code, run any tests, and finish by packaging the code up in a JAR file within the target directory. The name of the JAR file will be based on the project’s <artifactId> and <version>
Maven also maintains a repository of dependencies on your local machine (usually in a .m2/repository directory in your home directory) for quick access to project dependencies.
install your project’s JAR file to that local repository
|
|
The install goal will compile, test, and package your project’s code and then copy it into the local dependency repository, ready for another project to reference it as a dependency.
Declare Dependencies
|
|
adding the following lines to pom.xml (within the <project> element):
|
|
<groupId>- The group or organization that the dependency belongs to.<artifactId>- The library that is required.<version>- The specific version of the library that is required.
Additionally, you may specify a <scope> element to specify one of the following scopes:
provided- Dependencies that are required for compiling the project code, but that will be provided at runtime by a container running the code (e.g., the Java Servlet API).test- Dependencies that are used for compiling and running tests, but not required for building or running the project’s runtime code.
|
|
Maven uses a plugin called “surefire” to run unit tests. The default configuration of this plugin compiles and runs all classes in src/test/javawith a name matching *Test. You can run the tests on the command line like this
|
|
Spring Boot Maven plugin
- It collects all the jars on the classpath and builds a single, runnable “über-jar”, which makes it more convenient to execute and transport your service.
- It searches for the
public static void main()method to flag as a runnable class. - It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies.