Maven Tutorial - Maven Generate Source Jar








Sometime we need to package the source code into a jar file along with the compiled Java byte code. For example, Eclipse IDE may need the source code jar file to show the source code.

It is also useful for developers who use our deployed project to do source code level debugging.

The "maven-source" plugin can pack our source code and deploy along with our project.

Maven Source Plugin

Add "maven-source" plugin in your "pom.xml" file.

<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.java2s.ide</groupId>
  <artifactId>xmlFileEditor</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>xmlFileEditor</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>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Issue "mvn install" to package and deploy our project to local repository.

c:\mvn_test\xmlFileEditor>mvn install