JMH无主清单属性

8

我刚刚使用以下命令创建了一个Maven项目:

$ mvn archetype:generate \
      -DinteractiveMode=false \
      -DarchetypeGroupId=org.openjdk.jmh \
      -DarchetypeArtifactId=jmh-java-benchmark-archetype \
      -DgroupId=org.sample \
      -DartifactId=test \
      -Dversion=1.0

然后我执行mvn clean install,接着执行java -jar test-1.0.jar

程序给了我这个消息

no main manifest attribute, in target/test-1.0.jar 

我看了一下清单文件,没有“Main-Class”属性。
这应该生成它:
<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <finalName>${uberjar.name}</finalName>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>org.openjdk.jmh.Main</mainClass>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <!--
                                    Shading signed JARs will fail without this.
                                    https://dev59.com/qHNA5IYBdhLWcg3wWsUA
                                -->
                                <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 clean package 命令进行构建,通常不要使用 install 命令,因为这会将构件放入您的 Maven 缓存中。 - Marko Topolnik
使用包具有相同的结果(JMH网站上的说明http://openjdk.java.net/projects/code-tools/jmh/是使用install)。 - Dan
1个回答

10

在JMH Maven构建的最终输出中,benchmarks.jar(即“uberjar”)是最终结果,而您正在运行的JAR只是一个中间结果。因此,请使用

java -jar target/benchmarks.jar

我在这篇博客文章中写了一些内容,希望能对你有所帮助。http://www.rationaljava.com/2015/02/jmh-how-to-setup-and-run-jmh-benchmark.html - Dan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接