【问题标题】:Manifest in maven-shade-plugin is not workingmaven-shade-plugin 中的清单不起作用
【发布时间】:2015-11-10 13:20:31
【问题描述】:

目标:创建内部版本号并将其放入 maven-shade-plugin 生成的清单中。然后,读取该版本号。

我使用了ManifestResourceTransformer 并在那里声明了 manifestEntries。

<!-- Maven Shade Plugin -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.2</version>
    <executions>
     <!-- Run shade goal on package phase -->
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>app.MainApp</mainClass>
              <manifestEntries>
                <Implementation-Build>${buildNumber}</Implementation-Build>
              </manifestEntries>
            </transformer>
          </transformers>
          <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                </excludes>
            </filter>
          </filters>
        </configuration>
      </execution>
    </executions>
  </plugin>

mvn package的结果日志中正确生成了内部版本号。

然后,我阅读了生成的清单:

    Manifest mf = new Manifest();
    mf.read(getClass().getResourceAsStream("/META-INF/MANIFEST.MF"));
    Attributes attr = mf.getMainAttributes();

    System.out.println("Manifest-Version : " + attr.getValue("Manifest-Version"));
    System.out.println("Created by : " + attr.getValue("Created-By"));
    System.out.println("Built by : " + attr.getValue("Built-By"));
    System.out.println("Implementation-Build: " + mf.getEntries().get("Implementation-Build"));

结果

Manifest-Version : 1.0
Created by : 1.6.0_65-b14-466-11M4802 (Apple Inc.)
Built by : null
Implementation-Build: null

为了更好的衡量,我什至硬编码了内部版本号。

<manifestEntries>
  <Implementation-Build>123123</Implementation-Build>
</manifestEntries>

结果还是一样。

想法:清单看起来真的很像 DEFAULT 清单,而不是任何东西。

编辑:我的 pom 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>MyGroupId</groupId>
  <artifactId>MyArtifactId</artifactId>
  <name>MyApp</name>

  <scm>
    <connection>scm:git:https://github.com/myorg/myrepo.git</connection>

  <developerConnection>scm:git:https://github.com/myorg/myrepo.git</developerConnection>
<tag>DEVELOP</tag>
  <url>https://github.com/myorg/myrepo.git</url>
</scm>

<build>
<sourceDirectory>src</sourceDirectory>
<resources>
  <resource>
    <directory>src</directory>
    <excludes>
      <exclude>**/*.java</exclude>
    </excludes>
  </resource>
  <resource>
    <directory>resources</directory>
    <excludes>
      <exclude>**/*.java</exclude>
    </excludes>
  </resource>
</resources>
<plugins>
  <!-- Maven Shade Plugin -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.2</version>
    <executions>
     <!-- Run shade goal on package phase -->
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
          <!-- add Main-Class to manifest file -->
            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>app.MainApp</mainClass>
              <manifestEntries>
                <Implementation-Build>${buildNumber}</Implementation-Build>
              </manifestEntries>
            </transformer>
          </transformers>
          <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                </excludes>
            </filter>
          </filters>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.4</version>
    <executions>
      <execution>
        <phase>validate</phase>
        <goals>
          <goal>create</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <format>{0,date,yyyyMMddHHmmss}</format>
      <items>
        <item>timestamp</item>
      </items>
      <doCheck>true</doCheck>
      <doUpdate>true</doUpdate>
    </configuration>
  </plugin>
  <plugin>
    <!-- Deploy the web site -->
    <groupId>com.github.github</groupId>
    <artifactId>site-maven-plugin</artifactId>
    <version>0.9</version>
    <executions>
      <execution>
        <goals>
            <goal>site</goal>
        </goals>
        <phase>site-deploy</phase>
        <configuration>
            <!-- must match the server's id  -->
            <server>github</server>
            <!-- The commit message -->
            <message>Building site for my project</message>
            <!-- The location where the site is uploaded -->
            <path>${site.path}</path>
            <!-- Use merge or override the content -->
            <merge>true</merge>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
</build>
<repositories>
<repository>
    <id>4thline-repo</id>
    <url>http://4thline.org/m2</url>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>
<repository>
  <id>clojars.org</id>
  <url>http://clojars.org/repo</url>
</repository>
</repositories>
<dependencies>
  <dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.7</version>
  </dependency>
  ...
</dependencies>
</project>

【问题讨论】:

  • pom 文件不完整并且缺少重要的东西...除此之外,您为什么要更改默认文件夹位置?你有什么样的包装?
  • 缺少重要的东西,例如?我故意不包括诸如 之类的东西……除此之外,还有无数的依赖关系。不确定这些是否有用和相关。无论如何,我会编辑以包含更完整的 pom。
  • 我是 Maven 新手。不确定您为什么要更改默认文件夹位置的问题。包装,是罐子。我使用 maven-shade-plugin 创建了一个大 uber jar。

标签: java maven maven-shade-plugin


【解决方案1】:

你如何运行附加的代码?如果您将它作为测试运行,它将产生您的输出,因为测试类路径不包含 MANIFEST.MF 文件。您需要在类路径中包含生成的(+阴影)jar 才能运行该代码。

【讨论】:

    猜你喜欢
    • 2013-08-19
    • 2013-02-19
    • 1970-01-01
    • 2014-09-05
    • 2019-03-04
    • 2015-08-01
    • 2013-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多