簡體   English   中英

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

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

目標:創建內部版本號,並將其放入由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>

您如何運行附帶的代碼? 如果您將其作為測試運行,則它將產生您的輸出,因為測試類路徑不包含MANIFEST.MF文件。 您需要在類路徑中包含生成的(加陰影的)jar,以便該代碼運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM