Maven 为测试用例构建 JAR

发布于 2025-01-13 04:02:12 字数 1495 浏览 2 评论 0原文

我使用 Maven 在 Eclipse 上有 Spring Boot Selenium Web 驱动程序项目。我在 scr/test/java 中运行测试,并将所有元素、页面、配置放入 scr/main/java 中。

现在我想创建可执行的jar。怎么办?是否可以?我希望它像 java -jar mytask.jar 一样从 cmd 运行,

到目前为止我尝试使用 mvn clean install、mvn clean package 来生成 .jar 文件。但是当我尝试运行它时,它不起作用。它在 scr/main/java 中运行 main。我想在 scr/test/java 中运行,

这是我的测试类:

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.bca.magenta.pages.HomePage;

    @SpringBootTest
    class ApplicationTests {
    
        @Autowired
        HomePage homePage;
    
        @Autowired
        WebDriver driver;
    
        @BeforeEach
        public void before() {
            driver.manage().window().maximize();
        }
    
        @Test
        public void test() throws InterruptedException {
            homePage.testInsert();
        }
    
        @AfterEach
        public void after() {
            driver.quit();
        }
    
    }

当我在 eclipse 中作为 junit 运行时,这是我在 src/main/java 中的主类

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);     
    }
}

。它按我的预期运行。但如何在 jar 中运行它?

i have spring boot selenium web driver project on eclipse using maven. i run the test in scr/test/java and put all element, pages, configuration in scr/main/java.

now i want to create executable jar. how to do this? is it possible? i want it to run from cmd like java -jar mytask.jar

so far i tried using mvn clean install, mvn clean package to generate .jar file. but when i tried to run it, it doesn't work. it runs the main in scr/main/java. i want to run in scr/test/java

this is my test class:

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.bca.magenta.pages.HomePage;

    @SpringBootTest
    class ApplicationTests {
    
        @Autowired
        HomePage homePage;
    
        @Autowired
        WebDriver driver;
    
        @BeforeEach
        public void before() {
            driver.manage().window().maximize();
        }
    
        @Test
        public void test() throws InterruptedException {
            homePage.testInsert();
        }
    
        @AfterEach
        public void after() {
            driver.quit();
        }
    
    }

this is my main class in src/main/java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);     
    }
}

when i run in eclipse as junit. it run as i expected. but how to run this in jar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你只要分分秒秒 2025-01-20 04:02:12

使用 shade 插件,这是一个 示例

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

Use the shade plugin, here's an example:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.4</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.sonatype.haven.HavenCli</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文