28 May 2019
Maven
by Jerry Zhang
#Tip of the Day:
Most of us are familiar with Maven. Many projects are built with maven now. I use maven from learning Spring boot. It is good to know original maven without Spring or IDE tools.
Package structure
src
-main
-java
-package
-test
-java
-package
resources
pom.xml
GroupId is the package name of the project.
ArtifactId is the module name.
mvn commands
-
mvn -v: check the version of maven.
-
mvn compile: compile this project.
-
mvn test: run the tests.
This will create a target folder in the project.
-
mvn target: Package the whole project into a jar file within target folder.
-
mvn clean: delete target folder
-
mvn install: install jar file into local repository, then we can use dependency.
<groupId>imooc</groupId>
<artifactId>flashsale</artifactId>
<version>0.0.1-SNAPSHOT</version>
After installation, this three lines can be added into another project as a dependency.
When we compile a project and import some libraries, maven will firstly search the dependencies in its pom.xml. If maven cannot find it, it will search online to download it.
Maven’s local repository
C:\Users***.m2\repository
We could change this in setting file.
<localRepository>/path/to/local/repo</localRepository>
Maven plugins
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
maven tags
<groupId>company site + project name</groupId>
<artifactId>project name + module name</artifactId>
<version>
snapshot;
alpha: internal test version;
beta: public test version
release: stable version
GA: formally published version
</version>
<packaging>default jar</packaging>
<name>project name</name>
<url>project url</url>
<scope>test: only work in test files</scope>
<exclusions>
<exclusion></exclusion>
</exclusions>
<dependencyManagement>
<dependencies>
<dependency></dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
</plugin>
</plugins>
</build>
scope
- compile: default
- provided: compile and test, e.g., servlet API
- runtime: runtime and test, e.g., JDBC driver
- test: test, e.g., jUnit
- system: depend on current machine, for example, we used JAVA_HOME in the project.
- import:
dependency passing
Suppose that project A depends on project B, and B depends on C.
When we compile project A, maven will import both B and C.
Now, if we do not want A depend on C, we could use
Aggregation and Inheritance
If we want to put multiple modules in one project, do the following.
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
If we want to inherit a dependency, use a
In the parent module,
<properties>
<junit.version></junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>