11

我正在使用 Maven 程序集插件将我的 Java 项目的二进制文件打包到一个胖 jar 中(使用 jar-with-dependencies 描述符)。这工作得很好。

问题:我怎样才能在编译的类文件旁边包含我的项目的源文件?我试图查看 Maven 文档以了解如何执行此操作,但找不到任何东西。

谢谢!

我的 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>
4

2 回答 2

6

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>
于 2013-05-15T18:25:34.953 回答
4

您选择将二进制文件和源代码作为胖 jar 分发是特定要求吗?通常,二进制文件和源文件一起分发,但作为单独的 jar 文件。Maven Central 上的许多项目都在使用这种方法,Nexus 和 Artifactory 等存储库也支持这种方法。如果你选择这个选项,maven-source-plugin就是你的朋友。从文档中:

<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>

然后执行mvn source:jar. 有关配置选项,请参见网页。

于 2013-05-15T18:31:51.833 回答