The “maven-source” plugin is used to pack your source code and deploy along with your project. This is extremely useful, for developers who use your deployed project can attach your source code for debugging.

1. Maven Source Plugin

Add “maven-source” plugin in your “pom.xml” file.

File : pom.xml

<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.mkyong</groupId>
	<artifactId>mkyongcore</artifactId>
	<packaging>jar</packaging>
	<version>1.0</version>
	<name>mkyongcore project</name>
	<url>http://maven.apache.org</url>
	<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-source-plugin</artifactId>
			<executions>
				<execution>
					<id>attach-sources</id>
					<goals>
						<goal>jar</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
	</build>
</project>

2. Deploy It

Issue “mvn install” to package and deploy your project to local repository.

D:\mkyongweb-core>mvn install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building mkyongcore project
[INFO]    task-segment: [install]
[INFO] ------------------------------------------------------------------------
//...
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: D:\mkyongweb-core\target\mkyongcore-1.0.jar
[INFO] Preparing source:jar
[WARNING] Removing: jar from forked lifecycle, to prevent recursive invocation.
[INFO] No goals needed for project - skipping
[INFO] [source:jar {execution: attach-sources}]
[INFO] Building jar: D:\mkyongweb-core\target\mkyongcore-1.0-sources.jar
[INFO] [install:install {execution: default-install}]
[INFO] Installing D:\mkyongweb-core\target\mkyongcore-1.0.jar to D:\maven\repo\com\mky
ong\mkyongcore\1.0\mkyongcore-1.0.jar
[INFO] Installing D:\mkyongweb-core\target\mkyongcore-1.0-sources.jar to D:\maven\repo
\com\mkyong\mkyongcore\1.0\mkyongcore-1.0-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL

3. Output

Browse to your local repository, you will noticed two jars file are created

  1. mkyongcore-1.0.jar (classes)
  2. mkyongcore-1.0-sources.jar (source code)
generate source code for maven
Tags :
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Related Posts

Popular Posts