简体   繁体   English

Maven:使用jar-with-dependencies分发源代码

[英]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). 我正在使用Maven程序集插件将我的Java项目的二进制文件打包成一个胖jar(带有jar-with-dependencies描述符)。 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. 我试着查看Maven文档以了解如何执行此操作但找不到任何内容。

Thanks! 谢谢!

My pom.xml looks like this: 我的pom.xml看起来像这样:

<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 : 最简单的解决方案是使用预定义的描述符src,或者使用预定义的描述符项目可能更好:

  <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. 通常,二进制文件和源文件一起分发,但作为单独的jar文件。 Many projects on Maven Central are using this approach, and repositories such as Nexus and Artifactory also support this. Maven Central上的许多项目都在使用这种方法,而Nexus和Artifactory等存储库也支持这种方法。 If you choose this option, the maven-source-plugin is your friend. 如果您选择此选项, maven-source-plugin就是您的朋友。 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 . 然后执行mvn source:jar See the web page for configuration options . 有关配置选项,请参见网页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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