java - 如何在 pom.xml 文件中设置主类

标签 java maven

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

我尝试在阶段中添加它,但收到错误“在可用目标 map 故事路径中无法在插件 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>

最佳答案

如果你想运行你的java程序并且你的项目是基于maven的,那么你可以使用阴影类型来构建你的jar文件。它可以帮助您拥有一个 fat 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 的类。

关于java - 如何在 pom.xml 文件中设置主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55734514/

相关文章:

java - 尝试将数据库连接从 MySQL 更改为 Oracle 但出现错误

java - JPA 中可以有多个主键吗

java - Joda-Time DateTimeFormatter 类线程安全吗?

java - 重力底部,文字不向上滚动

java - 执行 mvn eclipse :eclipse 后我的源文件夹被删除

java - 即使在构建过程中在 Maven 本地存储库中创建了依赖组件的 jar,也会查找自定义远程存储库

java - 如何在非抽象类中声明抽象方法

java - 如何使用带有撇号的 Java 单词边界?

java - Maven:从一个较大项目的包中创建一个 jar

java - Maven 多模块项目找不到兄弟模块