Maven - 添加对工件源的依赖

时间:2022-01-11 00:14:03

I have two maven modules, one that ends up as a jar, and one war that depends on that jar.

我有两个maven模块,一个最终作为一个jar,一个战争依赖于那个jar。

I want the jar module to package it's source code together with the compiled classes in the jar, so that the second module is able to access it. I have tried using the maven-source-plugin, but I am confused as to how to add a dependency on the output of that. It seems that the dependency by default goes to the compiled jar, and not the source-code jar (ending with "-source.jar") that maven-source-plugin creates.

我希望jar模块将它的源代码与jar中编译的类一起打包,以便第二个模块能够访问它。我尝试过使用maven-source-plugin,但我对如何在输出中添加依赖项感到困惑。似乎默认情况下依赖关系是编译的jar,而不是maven-source-plugin创建的源代码jar(以“-source.jar”结尾)。

How do I add the "-source.jar" as a dependency, while still preserving the dependency on the compiled sources?

如何将“-source.jar”添加为依赖项,同时仍然保留对已编译源的依赖?

2 个解决方案

#1


I've not tried this, but I think you need to create two profiles in your project. One which builds the main jar. The other which builds the sources jar. Unfortunately, I'm not exactly sure how you would build that profile. I couldn't find a good example of it so far.

我没试过这个,但我认为你需要在你的项目中创建两个配置文件。一个建立主罐子的人。另一个构建源jar。不幸的是,我不确定你将如何构建该配置文件。到目前为止,我找不到一个很好的例子。

(Accoding to the comments, you don't actually need a profile. You can just use the sources-plugin which will deploy the sources and make them available via the sources classifier)

(根据评论,您实际上并不需要配置文件。您可以使用sources-plugin来部署源并通过源分类器使它们可用)

In theory, you'd use the 2nd profile to attach the sources to the project. This creates a 2nd entry in your repository for the sources using that classifier. Once you install/deploy the sources to your repository, you should be able to include the sources as a dependency by using the classifier tag on the dependency to specify the sources directly.

理论上,您可以使用第二个配置文件将源附加到项目中。这将在您的存储库中为使用该分类器的源创建第二个条目。将源安装/部署到存储库后,您应该能够通过使用依赖项上的分类器标记将源作为依赖项包括在内,以直接指定源。

So you'd have something like this in your webapp POM:

所以你在webapp POM中有这样的东西:

<dependencies>
  <dependency>
    <groupId>myGroup</groupId>
    <artifactId>myJar</artifactId>
    <version>4.0</version>
    <type>jar</type>
  </dependency>
  <dependency>
    <groupId>myGroup</groupId>
    <artifactId>myJar</artifactId>
    <version>4.0</version>
    <type>jar</type>
    <classifier>sources</classifier>
  </dependency>
</dependencies>

#2


Did you try adding the src directory as a resource directory in the build section? That should copy the source into the jar on build.

您是否尝试将src目录添加为构建部分中的资源目录?这应该将源复制到构建中的jar中。

#1


I've not tried this, but I think you need to create two profiles in your project. One which builds the main jar. The other which builds the sources jar. Unfortunately, I'm not exactly sure how you would build that profile. I couldn't find a good example of it so far.

我没试过这个,但我认为你需要在你的项目中创建两个配置文件。一个建立主罐子的人。另一个构建源jar。不幸的是,我不确定你将如何构建该配置文件。到目前为止,我找不到一个很好的例子。

(Accoding to the comments, you don't actually need a profile. You can just use the sources-plugin which will deploy the sources and make them available via the sources classifier)

(根据评论,您实际上并不需要配置文件。您可以使用sources-plugin来部署源并通过源分类器使它们可用)

In theory, you'd use the 2nd profile to attach the sources to the project. This creates a 2nd entry in your repository for the sources using that classifier. Once you install/deploy the sources to your repository, you should be able to include the sources as a dependency by using the classifier tag on the dependency to specify the sources directly.

理论上,您可以使用第二个配置文件将源附加到项目中。这将在您的存储库中为使用该分类器的源创建第二个条目。将源安装/部署到存储库后,您应该能够通过使用依赖项上的分类器标记将源作为依赖项包括在内,以直接指定源。

So you'd have something like this in your webapp POM:

所以你在webapp POM中有这样的东西:

<dependencies>
  <dependency>
    <groupId>myGroup</groupId>
    <artifactId>myJar</artifactId>
    <version>4.0</version>
    <type>jar</type>
  </dependency>
  <dependency>
    <groupId>myGroup</groupId>
    <artifactId>myJar</artifactId>
    <version>4.0</version>
    <type>jar</type>
    <classifier>sources</classifier>
  </dependency>
</dependencies>

#2


Did you try adding the src directory as a resource directory in the build section? That should copy the source into the jar on build.

您是否尝试将src目录添加为构建部分中的资源目录?这应该将源复制到构建中的jar中。