[Maven] Build Everything via Maven
文章目录
1. Maven introduction
Apache Maven is a software project management and comprehension tool. Based on the concept of a Project Object Model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.
In short, Maven can help you to build and manage java-based project more conveniently.
Maven is a part of the Apache Software Foundation Full source code in github
Build steps:
- clean: delete the old class files, prepare for next compile
- compile: compile the java source code to class files
- test: automatic test, call junit automatically
- reporting: automatic test result
- package: java-> jar javaweb->war
- install: copy the package to specific repository path
- deploy: copy the jar/war package to sevelet container specific path to run
2. Download, installation and configuration
Download
note: please attention about about supporting jdk version by maven,Maven 3.3+ require JDK 1.7 or above to execute
environment parameter configuration
system parameter:
- M2_HOME: D:/…/apache-maven-3.6.3/bin (SpringBoot will use it)
- MAVEM_HOME: D:/…/apache-maven-3.6.3
- Path: add %MAVEN_HOME%\bin
confirm that everything is ready:
enter cmd, exec mvn --version
3. Directory structure based on Maven
Hello
|—src
|—|---main
|—|---|—|---java
|—|---|—|---resource
|—|---test
|—|---|—java
|—|---|—resource
|—pom.xml
if we want Maven to know about something that we defined, we need to add property config.
<param-value>classpath:spring-context.xml</param-value>
convention over configuration over coding
4. Maven common command
Must enter into the path of pom.xml when try to execute Maven build related command.
- mvn clean: will delete source jar package
- mvn compile: compile main program, will genenrate target folder.
- mvn test-compile: compile test program
- mvn test: execute test
- mvn package: will generate jar package
- mvn install: copy project jar package to local repository in order to be used by other projects
5. Maven settings conf
There’s a setting,xml in the maven home/conf path.1
There are two locations where a settings.xml file may live:
-
The Maven install: ${maven.home}/conf/settings.xml Global settings
-
A user’s install: ${user.home}/.m2/settings.xml User specific settings
-
If both files exists, their contents gets merged, with the user-specific settings.xml being dominant.
-
< settings >
- < localRepository >: local pc Repository, better to set, or will be C:/${user.home}/.m2/repository by default
- < interactiveMode >: attempt to interact with user or input
- < offline > : connect to remote repository or not
-
< pluginGroups >: a list of additional group identifiers.This list automatically contains org.apache.maven.plugins and org.codehaus.mojo.
-
< proxies >: netwrok proxy list
-
< servers >: Specifies the authentication information to use when connecting to a particular server
-
< mirrors >: This is a list of mirrors to be used in downloading artifacts from remote repositories.
<!-- 阿里云仓库 -->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
- < profiles >: Specifies a set of introductions to the build process.If a profile is active from settings, its values will override any equivalently ID’d profiles in a POM or profiles.xml file
6. Maven coordinate(坐标) – GAV
- groupid: project
- artifactid : module name
- version: service version
<groupid>org.springframework</groupid>
<artifactId>spring-core</artifactId>
<version>4.0.0RELEASE</version>
path in repository: org/springframework/spring-core/spring-core-4.0.0RELEASE.jar
7. Maven repository
local repository : service for all Maven project on local pc
remote repository: private repository(build in LAN, Nexus ) central repository (service for all Maven project globally) crentral repository mirrors.
Maven Central Repository Search
Maven repository
8. Dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-staticdocs</artifactId>
<version>${version.springfox}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions><
</dependency>
8.1 scope(依赖的范围)
- compile: effected for main program and test program, will be involved when package
- test: only effected for test program, won’t be involved when package
- provided: effected for main program and test program, won’t be involved when package
8.2 exclusions (依赖排除)
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
Dependency exclusion: reverse operation of transitive dependency. Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project’s classpath by way of the dependency in which the exclusion was declared.
8.3 dependency optional
<project>
...
<dependencies>
<!-- declare the dependency to be set as optional -->
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<optional>true</optional> <!-- value will be true or false only -->
</dependency>
</dependencies>
</project>
If a user wants to use functionality related to an optional dependency, they have to redeclare that optional dependency in their own project.
8.4 dependency principal(how to solve package conflict)
- principle of proximity, means shortest path first
- when path equals, who declare firstly has top priority
8.4 use properties to manage dependency
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${version.activiti}</version>
</dependency>
<properties>
<!-- artifacts versions -->
<version.activiti>5.18.0</version.activiti>
<version.spring>4.2.9.RELEASE</version.spring>
</properties>
8.5 dependency inherit
define version in parent project, then child project will inherit the version.
note: install parent project first, install child project later.
parent project
<modules>
<module>sis-service</module>
</modules>
......
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${version.aspectj}</version>
</dependency>
child project
<parent>
<artifactId>sis-parent</artifactId>
<groupId>net.homecredit.sis</groupId>
<version>30.2.0</version>
</parent>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
9. Maven Plugins
officials: https://maven.apache.org/plugins/index.html
MojoHaus: https://github.com/mojohaus