Project Coordinate
Coordinate of the maven project is used to uniquely identify a maven project, and locate it. compulsorily, it requires the followings: group id: com.xxx; artefact id: project name;version: version control.
project name: the name of the project.
Parent POM
Parent pom must be packaged as a POM.
Effective POM
Maven project is set up with default behaviors. It because Maven POM automatically extends from a super POM; the following command may show the effective pom file.mvn help:effective-pom
Maven Project Structure
Maven project has a fixed file structure to follow.The POM file is located at a project root.
src folder:
The source is located at the src/main
Java code is located at the src/main/java
Resources are located at src/main/resources
The test is located at the src/test.
Java test code is located at the src/test/java
Test resources are located at src/test/resources
target folder:
used to take all compiled results.
Build Lifecycle
- validate: is all the necessary information for this project available?
- compile: compile the source code of the project, and put them in the target folder.
- test: test the compiled source code using a suitable unit test framework.
- package: take the compiled code and package it in its distribution format, fx: jar.
- verify: run integration tests to ensure quality criteria are met.
- install: install the package into the local maven repository for other project's dependency.
- deploy: copying the final package to the remote repository, for sharing with other developers.
Plugins
mvn maven-plugin-name:goal
- Build plugins: defined under the <build>
- Report plugins: defined under the <reporting>
Archetype
The archetype is a Maven project template. A developer may create a project base on an archetype.mvn archetype: generate
maven-plugin: archetype; and goal: generate
Using maven archetype plugin to generate a web app project skeleton from a command line.
mvn archetype:generate -DgroupId=com.ynz -DartifactId=sampleWeb -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4 -DinteractiveMode=false
Package
Dependency Scope
- Compile a default dependency scope. The dependent jar is available in all the stages.
- Test: The dependent jar is available during the test. It is designed to reduce the size of the artifact.
- Import: indicates this dependency(like BOM) will be replaced by another POM.
Force Repository Update
- mvn package
- mvn install
- mvn dependency:resolve
Corrupted Dependencies
- mvn package -U
- mvn install -U
- mvn dependency:resolve -U
Analyse Maven Dependencies
- mvn dependency:analyze
- mvn dependency:tree
- Dependency convergence issues: When different versions of the same dependency are used in the project, which can lead to runtime errors.
- Unused dependencies: When a dependency is declared in the project but is not actually used by any code.
- Missing dependencies: When a dependency is required by the project but is not declared in the pom.xml file.
- Dependency conflicts: When multiple dependencies declare the same artifact with different versions.
Mediation Principle
Skipping Tests
To skip running the tests for a particular project, set the skipTests property to true.
- <project>
- [...]
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>3.0.0</version>
- <configuration>
- <skipTests>true</skipTests>
- </configuration>
- </plugin>
- </plugins>
- </build>
- [...]
- </project>
You can also skip the tests via the command line by executing the following command:
- mvn install -DskipTests
If you absolutely must, you can also use the maven.test.skip
property to skip compiling the tests. maven.test.skip
is honored by Surefire, Failsafe and the Compiler Plugin.
- mvn install -Dmaven.test.skip=true