如何让 Java MANIFEST.MF 包含 Maven 版本号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12533318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:16:48  来源:igfitidea点击:

How to get Java MANIFEST.MF to include Maven Version Number

javamavenversionmanifest

提问by jcstock74

I have the following in my project's pom.xml which I think should display the version of Maven being used in the resulting WAR file:

我的项目的 pom.xml 中有以下内容,我认为应该显示生成的 WAR 文件中使用的 Maven 版本:

<build>
...
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>false</addClasspath>
                    </manifest>
                    <manifestEntries>
                        <Build-Time>${maven.build.timestamp}</Build-Time>
                        <Build-Host>${agent.name}</Build-Host>
                        <Build-User>${user.name}</Build-User>
                        <Build-Maven>Maven ${maven.version}</Build-Maven>
                        <Build-Java>${java.version}</Build-Java>
                        <Build-OS>${os.name}</Build-OS>
                        <Build-Label>${project.version}</Build-Label>
                        <Build-Path>${basedir}</Build-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
        ...
    </plugins>
...
</build>

The MANIFEST.MF that is created looks correct see below apart from the Build-Maven line in which the ${maven.version} is not substituted with the actual version number 3.0.4 in this case.

创建的 MANIFEST.MF 看起来是正确的,参见下面的 Build-Maven 行,其中 ${maven.version} 在这种情况下没有替换为实际版本号 3.0.4。

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: stocjon
Build-Jdk: 1.6.0_35
Build-Host: 
Build-Java: 1.6.0_35
Build-Label: 1.0.0-SNAPSHOT
Build-Maven: Maven ${maven.version}
Build-OS: Windows XP
Build-Path: C:\Development\project_name
Build-Time: 15:38:50 21-Sep-2012
Build-User: user_name

Any ideas why Maven version is not being populated in the MANIFEST.MF ?

任何想法为什么没有在 MANIFEST.MF 中填充 Maven 版本?

Help would be much appreciated.

帮助将不胜感激。

Thanks Jon

谢谢乔恩

采纳答案by Paulius Matulionis

You need to add this plugin:

你需要添加这个插件:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
  <executions>
    <execution>
      <phase>validate</phase>
      <goals>
        <goal>maven-version</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Check herefor details.

在此处查看详细信息。

回答by Guillaume Husta

We no more need the build-helper-maven-pluginsince the feature (MSHARED-38) was added to the component maven-archiver : 2.5in Feb. 2012 (release notes).

我们不再需要build-helper-maven-plugin,因为该功能 ( MSHARED-38)在 2012 年 2 月被添加到组件maven-archiver : 2.5发行说明)。

And this component is used by the Maven plugins like maven-jar-plugin, maven-war-plugin, maven-ear-plugin, etc.

这个组件被 Maven 插件使用,如maven-jar-pluginmaven-war-pluginmaven-ear-plugin等。

The versions of these plugins using this feature are :

使用此功能的这些插件的版本是:

  • maven-jar-plugin : 2.4(MJAR-148), released in Feb. 2012
  • maven-war-plugin : 2.2(MWAR-273), released in Feb. 2012
  • maven-ear-plugin : 2.8(MEAR-145), released in Sept. 2012
  • maven-assembly-plugin : 2.4(MASSEMBLY-634), released in Nov. 2012
  • maven-ejb-plugin : 2.4(MEJB-56), EDIT : released on 24/Aug/14
  • etc.
  • maven-jar-plugin : 2.4( MJAR-148),于 2012 年 2 月发布
  • maven-war-plugin : 2.2( MWAR-273),2012 年 2 月发布
  • maven-ear-plugin : 2.8( MEAR-145),2012 年 9 月发布
  • maven-assembly-plugin : 2.4( MASSEMBLY-634),2012 年 11 月发布
  • maven-ejb-plugin:2.4MEJB-56),编辑:24/Aug/14 发布
  • 等等。

So now we'll have this entry by default in the archive's manifest.mf :

所以现在我们将默认在存档的 manifest.mf 中有这个条目:

Created-By: Apache Maven ${maven.version}

创建者:Apache Maven ${maven.version}

回答by OldCurmudgeon

For completeness - this worked for me:

为了完整性 - 这对我有用:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.1</version>
    <configuration>
      <archive>
        <manifest>
          <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        </manifest>
      </archive>
    </configuration>
  </plugin>

This puts in my manifest -

这放在我的清单中 -

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Old.Curmudgeon
Build-Jdk: 1.5.0_22
Implementation-Title: JarFileName-1.0.2
Implementation-Version: 1.0.2
Implementation-Vendor-Id: our.id

回答by Deepak

At least as of version 2.4 of the maven-jar plugin, the following entries are added by default to the MANIFEST.MF file in META-INF in the jar:

至少从 maven-jar 插件的 2.4 版开始,以下条目默认添加到 jar 中 META-INF 中的 MANIFEST.MF 文件中:

 Manifest-Version: 1.0
 Archiver-Version: Plexus Archiver
 Built-By: abcUser
 Created-By: Apache Maven 3.3.3
 Build-Jdk: 1.8.0_77

To add the project version and other implementation details, simply add the following to the maven-jar-plugin (either in the pluginManagement section or in the build -> plugins section:

要添加项目版本和其他实现细节,只需将以下内容添加到 maven-jar-plugin(在 pluginManagement 部分或 build -> plugins 部分:

 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-jar-plugin</artifactId>
     <version>2.4</version>
     <configuration>
         <archive>
             <manifest>
                 <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
             </manifest>

         </archive>
     </configuration>
 </plugin>

To add something like a build time, add the following:

要添加诸如构建时间之类的内容,请添加以下内容:

 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-jar-plugin</artifactId>
     <version>2.4</version>
     <configuration>
         <archive>
             <manifest>
                 <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
             </manifest>
             <manifestEntries>
                 <Build-Time>${maven.build.timestamp}</Build-Time>                            
             </manifestEntries>
         </archive>
     </configuration>
 </plugin>

The Build-Time format can be changed by using the following property in the <properties>section of your pom.xml:

可以使用<properties>pom.xml 部分中的以下属性更改构建时格式:

<maven.build.timestamp.format>yyyy-MM-dd HH:mm z</maven.build.timestamp.format>

The output of all the above is something like:

以上所有的输出是这样的:

Manifest-Version: 1.0
Implementation-Title: UI
Implementation-Version: 2.0.5-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: abcUser
Implementation-Vendor-Id: com.xyz.abc.dbe
Build-Time: 2016-12-23 12:04 UTC
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_77
Implementation-Vendor: XYZ Corporation

回答by jcstock74

In order to get the build machine I added the following plugin:

为了获得构建机器,我添加了以下插件:

<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source>project.properties["hostname"] = InetAddress.getLocalHost().getHostName()</source>
            </configuration>
        </execution>
    </executions>
</plugin>

I could then retrieve the host machine name through ${hostname}.

然后我可以通过 ${hostname} 检索主机名。

回答by yegor256

Take a look how it's suggested by jcabi-manifests: http://manifests.jcabi.com/versioning.html

看看它是如何建议的jcabi-manifestshttp: //manifests.jcabi.com/versioning.html

Also, see this blog post for more details: http://www.yegor256.com/2014/07/03/how-to-read-manifest-mf.html

此外,有关更多详细信息,请参阅此博客文章:http: //www.yegor256.com/2014/07/03/how-to-read-manifest-mf.html