11

I am using the Maven assembly plugin to package binaries of my Java project into a fat jar (with the jar-with-dependencies descriptor). This works pretty well.

Question: How can I also include the source files of my project alongside the compiled class files? I tried to look into the Maven documentation to find out how to do so but couldn't find anything.

Thanks!

My pom.xml looks like this:

<project>
...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <finalName>${pom.artifactId}-${pom.version}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>${project.basedir}/bin/</outputDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Johannes
  • 760
  • 1
  • 7
  • 20

2 Answers2

6

The simplest solution is to use the predefined descriptor src or it might be better to use the predefined descriptor project:

  <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
    <descriptorRef>src</descriptorRef>
  </descriptorRefs>

or an other option would be like this:

  <descriptorRefs>
    <descriptorRef>jar-with-dependencies</descriptorRef>
    <descriptorRef>project</descriptorRef>
  </descriptorRefs>
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Thanks! This isn't exactly what I had in mind, as I planned to package sources into the same jar, but this is probably totally sufficient or better. – Johannes May 16 '13 at 09:00
  • It depends a little bit. I would suggest to do this only in case of a release and use the maven-source-plugin for such purposes in the release configuration in your company pom. This can be enhanced to create the sources package with every SNAPSHOT deployment as well. It's your turn to choose which is the way you like to go. – khmarbaise May 16 '13 at 09:02
5

Is it a specific requirement that you choose to distribute binaries and source code as a fat jar? Normally, binaries and source files are distributed together, but as separate jar files. Many projects on Maven Central are using this approach, and repositories such as Nexus and Artifactory also support this. If you choose this option, the maven-source-plugin is your friend. From the documentation:

<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>

And then execute mvn source:jar. See the web page for configuration options.

Robert Jack Will
  • 10,333
  • 1
  • 21
  • 29
matsev
  • 32,104
  • 16
  • 121
  • 156
  • Thanks for the tip! Using the src descriptor of the assembly plugin as pointed out by hkmarbaise seems a bit simpler though, as it doesn't need an additional plugin. – Johannes May 16 '13 at 09:02