编程语言
首页 > 编程语言> > java-如何在桌面应用程序的pom.xml中仅配置一次

java-如何在桌面应用程序的pom.xml中仅配置一次

作者:互联网

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

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

我认为问题来自与依赖关系的jar组装描述符,而不是解压缩普通的jar和融合清单,而是从${project.build.outputDirectory}创建了一个新的jar,这对我来说很奇怪.

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

谢谢,
尼古拉斯

解决方法:

您可以将assemblyjar插件都配置为使用[特定清单文件.在程序集文档的“创建可执行Jar”部分中:

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:

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

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.

以下配置将程序集插件的执行绑定到集成前预测试阶段(程序包之后的下一个阶段),并且还将包括在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-Plug插件的输出为:

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

配置:

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

标签:maven-plugin,maven-2,java
来源: https://codeday.me/bug/20191210/2102091.html