如何配置<清单>仅在桌面应用程序的 pom.xml 中出现一次

发布于 2024-08-04 11:54:32 字数 411 浏览 4 评论 0原文

我正在使用 maven 制作一个 swing 应用程序,并尝试严格控制 pom.xml (在粘贴我们在 google 中找到的任何内容后,该文件往往会变成一堆垃圾)。

我的 pom 带有 jar,我使用带有“jar-with-dependencies”描述符的 maven- assembly-plugin 。 因此,我需要定义项目的两次部分(主类和版本),一次用于普通 jar,另一次用于 jar-with-dependencies。

我认为问题来自于 jar-with-dependency 程序集描述符,它不是解压正常的 jar 并融合清单,而是从 ${project.build.outputDirectory} 创建一个新的 jar,这对我来说闻起来很奇怪。

有人有一个紧凑而优雅的想法来避免重复我的清单配置吗?

谢谢, 尼古拉斯.

I'm making a swing application with maven and I try to keep the pom.xml under tight reins (this file tends to become a pile of junk after pasting there whatever we find in google).

My pom is with jar and I use the maven-assembly-plugin with "jar-with-dependencies" descriptor.
So I need to define twice the part of my project (main class and versions), once for the normal jar and the other once for the jar-with-dependencies.

I think the problem comes from the jar-with-dependencies assembly descriptor, instead of unpacking the normal jar and fusion the manifests, it creates a new jar from ${project.build.outputDirectory} wich smells strange to me.

Does anybody have a compact and elegant idea to avoid this repetition of my manifest configuration ?

Thanks,
Nicolas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北渚 2024-08-11 11:54:32

您可以配置 程序集jar 插件来使用[特定清单文件。从程序集文档的创建可执行 Jar 部分:

正如您毫无疑问注意到的那样,Assembly Plugin 是一种非常有用的方法,可以为您的项目创建独立的二进制工件,等等。但是,一旦创建了这个独立的 jar,您可能希望能够使用 -jar JVM 开关来执行它。

为了适应这种情况,Assembly Plugin 支持与 maven-jar-plugin 支持的元素相同的元素配置(请参阅参考资料)。使用此配置,可以轻松配置 jar 清单的 Main-Class 属性:

请注意,您定义的清单将与生成的内容合并。从引用的 jar 页面:

您自己的清单文件的内容将与 Maven Archiver 生成的条目合并。如果您在自己的清单文件中指定一个条目,它将覆盖 Maven Archiver 生成的值。

以下配置将程序集插件执行绑定到 pre-integration-test 阶段(打包后的下一个阶段),并且还将包含 src/main/resources/META-INF 下定义的 MANIFEST.MF 。

因此,按照这种方法,您可以在 MANIFEST.MF 中定义通用清单内容,并将它们合并到最终清单中。

src/main/resources/META-INF/MANIFEST.MF 的内容

Created-By: Me
Foo: bar
Main-Class: com.foo.bar.MyMainClass
Bundle-Version: 4.0.0

更新:
根据有关使用 addDefaultSpecificationEntries 的评论,建议的方法将与 jar 插件生成的 jar 上的规范结合使用。然而,看起来程序集插件尚不支持合并附加声明。 jar 插件将 src/main/resources 中的清单与附加指定的值合并,以在我的测试项目中提供此内容:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Me
Built-By: Rich Seller
Build-Jdk: 1.5.0_15
Specification-Title: Unnamed - org.test:test:jar:1.0.0
Specification-Version: 1.0.0
Implementation-Title: Unnamed - org.test:test:jar:1.0.0
Implementation-Version: 1.0.0
Implementation-Vendor-Id: org.test
Foo: bar
Main-Class: com.foo.bar.MyMainClass
Bundle-Version: 4.0.0

而 assembly-plugin 的输出是:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Me
Foo: bar
Main-Class: com.foo.bar.MyMainClass
Bundle-Version: 4.0.0

config:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <id>jar-with-deps</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifestFile>${manifest.file}</manifestFile>
      <!--these properties are ignored-->
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
      </manifest>
    </archive>
  </configuration>
</plugin>
<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.2</version>
  <configuration>
    <archive>
      <manifestFile>${manifest.file}</manifestFile>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
      </manifest>
    </archive>
  </configuration>
</plugin>
...
<properties>
  <manifest.file>src/main/resources/META-INF/MANIFEST.MF</manifest.file>
</properties>

You can configure both the assembly and jar plugins to use a [particular manifest file. From the Creating an Executable Jar section of the assembly docs:

As you've no doubt noticed, the Assembly Plugin can be a very useful way to create a self-contained binary artifact for your project, among many other things. However, once you've created this self-contained jar, you will probably want the ability to execute it using the -jar JVM switch.

To accommodate this, the Assembly Plugin supports configuration of an element which is identical to that supported by the maven-jar-plugin (see Resources). Using this configuration, it's easy to configure the Main-Class attribute of the jar manifest:

Note the manifest you define is merged with the generated content. From the referenced jar page:

The content of your own manifest file will be merged with the entries generated by Maven Archiver. If you specify an entry in your own manifest file it will override the value generated by Maven Archiver.

The following configuration binds the assembly-plugin execution to the pre-integration-test phase (the next phase after package) and will also include the MANIFEST.MF defined under src/main/resources/META-INF.

So following this approach, you can define the common manifest contents in your MANIFEST.MF and have them be merged into the final manifest.

contents of src/main/resources/META-INF/MANIFEST.MF

Created-By: Me
Foo: bar
Main-Class: com.foo.bar.MyMainClass
Bundle-Version: 4.0.0

Update:
Based on the comments about using addDefaultSpecificationEntries, the suggested approach will work in conjunction with that specification on the jar produced by the jar plugin. However it looks like the assembly plugin doesn't yet support merging the additional declarations. The jar plugin merges the manifest from src/main/resources with the additionally specified values to give this content on my test project:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Me
Built-By: Rich Seller
Build-Jdk: 1.5.0_15
Specification-Title: Unnamed - org.test:test:jar:1.0.0
Specification-Version: 1.0.0
Implementation-Title: Unnamed - org.test:test:jar:1.0.0
Implementation-Version: 1.0.0
Implementation-Vendor-Id: org.test
Foo: bar
Main-Class: com.foo.bar.MyMainClass
Bundle-Version: 4.0.0

Whereas the output from the assembly-plugin is:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Me
Foo: bar
Main-Class: com.foo.bar.MyMainClass
Bundle-Version: 4.0.0

config:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <id>jar-with-deps</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifestFile>${manifest.file}</manifestFile>
      <!--these properties are ignored-->
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
      </manifest>
    </archive>
  </configuration>
</plugin>
<plugin>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.2</version>
  <configuration>
    <archive>
      <manifestFile>${manifest.file}</manifestFile>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
      </manifest>
    </archive>
  </configuration>
</plugin>
...
<properties>
  <manifest.file>src/main/resources/META-INF/MANIFEST.MF</manifest.file>
</properties>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文