我使用了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>
结果仍然相同。
<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>
答案 0 :(得分:0)
如何运行附加代码?如果您将其作为测试运行,它将生成您的输出,因为测试类路径不包含MANIFEST.MF文件。您需要在类路径中包含生成的(+阴影)jar,以便运行该代码。