首页 > 解决方案 > 如何在 pom.xml 文件中设置主类

问题描述

我有用于运行自动化测试的页面对象模型,并且在 pages 包org.jbehave.web.webdriver.pages.test下有一个 java 类。我想使用基于 maven pom 文件构建的 jar 文件只运行特定的 test.java 类。我应该在 pom 文件中的哪里添加我的主类并只运行特定的类?

我尝试在该阶段添加它,但我收到错误消息“在可用目标 map-stories-as-paths 中的插件 org.jbehave:jbehave-maven-plugin:4.0.3 中找不到目标'java'”

以下是 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>jbehave-web-webdriver</groupId>
  <artifactId>jbehave-web-webdriver</artifactId>
  <version>1.0</version>
  <name>JBehave WebDriver Stories</name>
  <description>
  </description>
  <properties>
    <jbehave.webapp.name>trader-runner</jbehave.webapp.name>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.jbehave.web</groupId>
      <artifactId>jbehave-web-selenium</artifactId>
      <version>3.6-beta-2</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>2.44.0</version>
    </dependency>
    <dependency>
      <groupId>org.jbehave</groupId>
      <artifactId>jbehave-core</artifactId>
      <version>4.0.4</version>
    </dependency>
    <dependency>
      <groupId>org.jbehave</groupId>
      <artifactId>jbehave-core</artifactId>
      <version>4.0.4</version>
      <classifier>resources</classifier>
      <type>zip</type>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.20</version>
      <optional>true</optional>
    </dependency>
  </dependencies>
  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <filtering>false</filtering>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.jbehave</groupId>
        <artifactId>jbehave-maven-plugin</artifactId>
        <version>4.0.3</version>
        <executions>
          <execution>
            <id>run-stories</id>
            <phase>integration-test</phase>
            <goals>
              <goal>java</goal>
            </goals>
            <configuration>
              <mainClass>com.java2s.ide.App</mainClass>
              <configuration>
              </configuration>
          </execution>
          <execution>
            <id>unpack-view-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>unpack-view-resources</goal>
            </goals>
            <configuration>
              <viewDirectory>target/jbehave/view</viewDirectory>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>11.0.1</version>
          </dependency>
          <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.3</version>
          </dependency>
          <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.3</version>
          </dependency>
          </dependency>
        </dependencies>
      </plugin> -->
    </plugins>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.jbehave</groupId>
                    <artifactId>jbehave-maven-plugin</artifactId>
                    <versionRange>[3.9.1,)</versionRange>
                    <goals>
                      <goal>unpack-view-resources</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

标签: javamaven

解决方案


如果你想运行你的 java 程序并且你的项目是基于 maven 的,那么你可以使用 shaded 类型来构建你的 jar 文件。它可以帮助您拥有一个胖 jar,您的所有依赖项都将添加到其中。**此外,您也可以在其底层标签中指定您的主类,例如:**

    <project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>MAIN_CLASS_ADDRESS</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>  

例如,“org.sonatype.haven.HaveCli”用于名为 HaveCli 的类。


推荐阅读