简体   繁体   English

Maven-shade-plugin中的清单不起作用

[英]Manifest in maven-shade-plugin is not working

Goal: Create build number and put it in the manifest generated by maven-shade-plugin. 目标:创建内部版本号,并将其放入由maven-shade-plugin生成的清单中。 Then, read that build number. 然后,读取该内部版本号。

I have used ManifestResourceTransformer and declared manifestEntries there. 我使用了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>

Build number is correctly generated from the resulting log of mvn package . 从生成的mvn package日志正确生成内部版本号。

Then, I read the generated manifest: 然后,我阅读了生成的清单:

    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"));

Result 结果

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

For good measure, I even hardcoded the build number. 从好的方面来说,我什至对内部版本号进行了硬编码。

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

The result still the same. 结果仍然相同。

Thoughts: The manifest really looks like the DEFAULT manifest instead of anything. 思考:清单实际上看起来像DEFAULT清单,而不是任何东西。

Edit: My pom file: 编辑:我的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>

how do you run the attached code? 您如何运行附带的代码? If you are running it as a test, it will produce your output because the test classpath does not include the MANIFEST.MF file. 如果您将其作为测试运行,则它将产生您的输出,因为测试类路径不包含MANIFEST.MF文件。 You will need to include the generated (+shaded) jar in your classpath for that code to run. 您需要在类路径中包含生成的(加阴影的)jar,以便该代码运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM