Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Distribute source code with with jar-with-dependencies

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>
like image 379
Johannes Avatar asked May 15 '13 17:05

Johannes


People also ask

Does JAR file include dependencies?

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.

Does JAR file contains POM xml?

When Maven builds a JAR file, it places the module's POM file inside (seemingly in the directory <groupid>/<artifactid>).


2 Answers

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.

like image 118
matsev Avatar answered Oct 01 '22 02:10

matsev


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>
like image 22
khmarbaise Avatar answered Oct 01 '22 01:10

khmarbaise