【问题标题】:How to get Java MANIFEST.MF to include Maven Version Number如何让 Java MANIFEST.MF 包含 Maven 版本号
【发布时间】:2012-09-14 00:58:50
【问题描述】:

我的项目的 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>

创建的 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

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

我们将不胜感激。

谢谢 乔恩

【问题讨论】:

    标签: java maven version manifest


    【解决方案1】:

    你需要添加这个插件:

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

    查看here了解详情。

    【讨论】:

    • 两个答案都指向同一篇博文。接受了第一个。谢谢大家。
    • 另一个问题,我如何获得构建主机,即执行构建的计算机的机器名称。 ${agent.name} 似乎不起作用。
    • 试试这个:${env.COMPUTERNAME},这对我有用。
    【解决方案2】:

    我们不再需要 build-helper-maven-plugin,因为该功能 (MSHARED-38) 在 2 月被添加到组件 ma​​ven-archiver : 2.5 中。 2012 (release notes)。

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

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

    • 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.4 (MEJB-56),编辑:24/Aug/14 发布

    所以现在我们将在存档的 manifest.mf 中默认包含此条目:

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

    【讨论】:

      【解决方案3】:

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

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

      这会放在我的清单中 -

      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
      

      【讨论】:

        【解决方案4】:

        至少从 2.4 版的 maven-jar 插件开始,以下条目默认添加到 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
        

        要添加项目版本和其他实现细节,只需将以下内容添加到 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>
        

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

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

        可以使用 pom.xml 的 &lt;properties&gt; 部分中的以下属性更改构建时间格式:

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

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

        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
        

        【讨论】:

          【解决方案5】:

          看看jcabi-manifests是怎么推荐的:http://manifests.jcabi.com/versioning.html

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

          【讨论】:

            【解决方案6】:

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

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-09-27
              • 2016-04-10
              • 2012-02-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多