Introduction to Maven Source Plugin
The Maven Source Plugin helps generate a JAR file that contains the source code of your Maven project. This source JAR can be deployed along with the main JAR to a repository, making it easier for others to view and understand your code.
Latest Version
As of this writing, the latest version of the Maven Source Plugin is 3.2.1
. It is recommended to use the latest version to ensure you have access to all the latest features and bug fixes.
Setting Up the Maven Source Plugin
To use the Maven Source Plugin, you need to configure it in your project's pom.xml
file. Let's go through the steps to set up a complete Maven project with the Source Plugin.
Step 1: Create a Maven Project
Run the following command to create a new Maven project:
mvn archetype:generate -DgroupId=com.example -DartifactId=source-plugin-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command will generate a simple Maven project with the following structure:
source-plugin-demo
|-- src
| |-- main
| | `-- java
| | `-- com
| | `-- example
| | `-- App.java
| `-- test
| `-- java
| `-- com
| `-- example
| `-- AppTest.java
|-- pom.xml
`-- target
Step 2: Add Maven Source Plugin Configuration
Navigate to the project directory and open the pom.xml
file. Add the Maven Source Plugin configuration inside the <build>
section:
<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</groupId>
<artifactId>source-plugin-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Explanation
<groupId>
: Specifies the group ID for the Maven Source Plugin.<artifactId>
: Specifies the Maven Source Plugin.<version>
: The version of the plugin you are using. Ensure you use the latest version.<executions>
: Specifies the executions for this plugin.<execution>
: Defines a specific execution of the plugin.<id>
: A unique identifier for this execution.<goals>
: Specifies the goals to be executed.<goal>
: The goal to generate the source JAR.
Step 3: Add Hello World to App.java
Open src/main/java/com/example/App.java
and add a simple Hello World program:
package com.example;
public class App {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Step 4: Build the Project
Run the following command to compile and package the project, including generating the source JAR:
mvn clean package
This command will compile your project, package it into a JAR file, and generate an additional JAR file containing the source code in the target
directory.
Step 5: Verify the Generated Source JAR
After the build is complete, navigate to the target
directory. You should see two JAR files:
source-plugin-demo-1.0-SNAPSHOT.jar
: The main JAR file.source-plugin-demo-1.0-SNAPSHOT-sources.jar
: The source JAR file.
Step 6: Running the Application
Run the following command to execute the main JAR and see the "Hello, World!" output:
java -jar target/source-plugin-demo-1.0-SNAPSHOT.jar
You should see the following output:
Hello, World!
Deploying the Source JAR
Deploying the source JAR along with the main JAR is a good practice, especially when sharing your project with others or using a repository manager like Nexus or Artifactory. The Maven Source Plugin ensures that your source code is easily accessible, making debugging and understanding the codebase simpler.
Step 1: Add Distribution Management to pom.xml
To deploy the JARs, add the distribution management section to your pom.xml
:
<distributionManagement>
<repository>
<id>releases</id>
<url>http://your.repository.url/repo</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://your.repository.url/snapshots</url>
</snapshotRepository>
</distributionManagement>
Step 2: Deploy the Artifacts
Run the following command to deploy your artifacts to the repository:
mvn clean deploy
This will deploy both the main JAR and the source JAR to the specified repository.
Conclusion
The Maven Source Plugin is a valuable tool for generating and deploying source JARs along with your main artifacts. This guide provided a complete example of setting up and using the Maven Source Plugin in a simple Maven project, helping you get started with generating and managing source JARs in your Maven builds.
Comments
Post a Comment
Leave Comment