简体   繁体   中英

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>

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>

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM