如何配置<清单>在桌面应用程序的pom.xml中仅一次 [英] how to configure <manifest> only once in pom.xml in a desktop application

查看:87
本文介绍了如何配置<清单>在桌面应用程序的pom.xml中仅一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Maven制作一个swing应用程序,并尝试将pom.xml保持严格控制(在粘贴了我们在Google中找到的所有内容之后,该文件往往会变成一堆垃圾).

我的pom与jar一起使用,并且我将maven-assembly-plugin与"jar-with-dependencies"描述符一起使用. 因此,我需要为项目的一部分(主类和版本)定义两次,一次用于普通的jar,另一次用于jar-with-dependencies.

我认为问题出在jar-with-dependencies程序集描述符,而不是解压缩普通的jar和融合清单,而是从$ {project.build.outputDirectory}创建一个新的jar,这对我来说很奇怪. /p>

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

谢谢, 尼古拉斯.

解决方案

您可以同时配置装配 jar 插件以使用[特定清单文件.在程序集文档的创建可执行Jar 部分中:

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

为了适应这一点,Assembly插件支持与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>

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.

解决方案

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>

这篇关于如何配置&lt;清单&gt;在桌面应用程序的pom.xml中仅一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆