首页 > 解决方案 > 如何在 JMH 中使用外部 JAR

问题描述

我的 Java 项目使用了一些外部 JAR。为了对项目进行基准测试,我如何将这些添加到 JMH?

-cp例如,我应该使用选项将它们添加到 java 命令行吗?(这实际上导致我的环境中找不到类错误)

标签: javajmh

解决方案


您可以像在任何其他项目中一样使用 jar。你可能调用了-cp错误的主类,它应该是org.openjdk.jmh.Main. 这是maven 的示例。请注意pom.xml.

我将在这里从 POM 复制重要部分:

..
<dependencies>
  ..
  <!-- This is the lib I want to add -->
  <dependency>
      <groupId>io.lettuce</groupId>
      <artifactId>lettuce-core</artifactId>
      <version>5.0.3.RELEASE</version>
  </dependency>
  ..
</dependencies>
...
<build>
  <plugins>
    <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.
                      http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
                  -->
                  <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>
...

推荐阅读