3

我正在用 maven 制作一个 Swing 应用程序,并且我试图将 pom.xml 保持在严格的控制之下(这个文件在粘贴我们在谷歌中找到的任何东西之后往往会变成一堆垃圾)。

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

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

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

谢谢,尼古拉斯。

4

1 回答 1

3

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>
于 2009-09-14T13:01:24.900 回答