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>
Normally, when we package a project into a jarW file, the jar file doesn't contain its dependencies, so the dependency jar files would need to be included in the classpathW in order to execute a class in the project's jar file that uses one of the dependencies.
When Maven builds a JAR file, it places the module's POM file inside (seemingly in the directory <groupid>/<artifactid>).
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With