What Is an Apache Maven Artifact?
An Apache Maven artifact is a file, typically a JAR (Java Archive), created when a project is built. Each artifact is uniquely identified by three main coordinates: groupId, artifactId, and version.
Maven artifacts are package units of your software. When building a project, Maven uses these artifacts to manage dependencies, plugins, and libraries. An artifact can be a library, module, framework, or any other package that a project requires. These artifacts are stored in repositories, either local, central, or remote, and are retrieved and used as needed by Maven during the build process.
- groupId: This is the unique identifier of the organization or group that created the project.
- artifactId: This is the unique name of the project.
- version: This indicates the specific version of the artifact.
Tools and Technologies
- Spring Tool Suite
- Maven
- Java Programming
- JAR or WAR file format
Example
Setting Up a Maven Project
Step 1: Create a New Maven Project
Create a new Maven project using the below Maven command.
mvn archetype:generate -DgroupId=com.example.myapp -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Directory Structure:
Maven creates a standard directory structure:
my-app/
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── example
│ └── myapp
│ └── App.java
└── test
└── java
└── com
└── example
└── myapp
└── AppTest.java
Step 2: Add Dependencies
Dependencies are specified in the pom.xml file. For instance, to add the JUnit library for testing, you would add the following dependency.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
Step 3: Configuring Plugins
Plugins are added similarly to dependencies in the pom.xml file. For example, to use the Maven Compiler Plugin, you would add.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Example pom.xml File:
Here’s a complete example of a pom.xml file for a basic Maven project.
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.myapp</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Common Maven Commands
Clean Project:
This command removes all the files that are generated by the previous build.
mvn clean
Output:
[caption width="800"]
Compile Project:
Compiles the source code of the project.
mvn compile
Output:
[caption width="800"]
Test Project:
Runs the tests using a suitable testing framework.
mvn test
Output:
[caption width="800"]
Package Project:
It takes the compiled code and packages it into a JAR file.
mvn package
Output:
[caption width="800"]
Install Project:
Installs the package into the local repository, which can be used as a dependency in other projects locally.
mvn install
Output:
[caption width="800"]
Maven Build Success:
