Maven Build JAR for Test Case

Viewed 41

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?

1 Answers

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>
Related