Pom.xml
lastmod 2025-02-14created 2024-11-0410 edits
That thing used in maven projects.
Properties
- modelVersion - POM model version
- groupId - The unique identifier of the project group or organization.
- artifactId - The name that uniquely identifies the project within the group.
- name - Project name
- packaging - Packaging output format
- version - The current project version. "SNAPSHOT" means a version under development.
- sourceDirectory - Specifies the source code folder
- properties - Variable settings. Accessible afterward in the
${...}format. - dependencies - List of dependency packages
- plugins - Defines the plugins to use in the Maven build process.
Properties
-
project.basedir- The directory where the current project is. Corresponds to the location of the pom.xml file. -
project.build.directory- The directory containing all files generated by the build. The default is "target".
Sample example
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.project</groupId>
<artifactId>firebell</artifactId>
<name>FirebellRest</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- More dependency -->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<!-- More Plugin -->
</plugins>
</build>
</project>
build
This section configures Maven's build process. Explained with the following example:
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
- sourceDirectory
- Specifies the src directory as the base directory for source code.
- Maven normally uses src/main/java as the source directory by default. This appears to be specified to change it to src when needed.
- plugins
- A section listing the plugins to use in the Maven build process.
- plugin
- A block that defines an individual plugin.
- Here maven-compiler-plugin is used. This plugin is used to compile source code.
- artifactId
- Specifies the name of the Maven plugin. Here it is maven-compiler-plugin.
- version
- Specifies the version of the plugin to use. In this case, version 3.7.0 is used.
- configuration
- Defines additional settings to pass to the plugin.
- source - Specifies the version of the Java source code. Java 1.8 is used here.
- target - Specifies the target JVM version for the compiled class files. Java 1.8 is used here as well.
dependencyManagement
Maven's dependencyManagement is a tag that lets you centrally manage the versions and scopes of dependencies in a project. When multiple modules or subprojects use the same dependency, it keeps versions consistent and prevents conflicts.
- Centralized dependency management - Define dependency versions and scopes in the parent POM, and child projects don't need to specify the version when referencing that dependency. Enables consistent version management.
- Not a dependency declaration - Dependencies declared in dependencyManagement are not automatically added to the project. To use them, each project must explicitly add them to dependencies.
- Used in hierarchies - Mainly used in a parent POM to manage dependencies commonly applied to child projects.
Adding a specific local JAR file as a dependency
- IntelliJ IDEA#Adding a specific local JAR file as a dependency
- Apache Ant -> build.xml#Adding a specific local JAR file as a dependency
- Gradle -> build.gradle#Adding a specific local JAR file as a dependency
Use systemPath.
<dependency>
<groupId>com.example</groupId>
<artifactId>mseo-common</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/mseo-common_1.0.0.jar</systemPath>
</dependency>