问题
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>
回答1:
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>
回答2:
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.
来源:https://stackoverflow.com/questions/16571889/maven-distribute-source-code-with-with-jar-with-dependencies