Siva Prakash Siva Prakash - 3 months ago 38
Java Question

Maven - How to run 2 java files from different packages inside src/test/java using Annotations

In My Project,

I have created 2 different packages inside /src/test/java, as both packages classes has different functionality and falls under same project.

When i try to do "Run As-> Maven Test" only the Class inside First Package is executing. Java Class in other package is not executing.

Kindly do the needful please.

Thanks in Advance.

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.brocade</groupId>
<artifactId>Vision</artifactId>
<version>1-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>

<artifactId>maven-assembly-plugin</artifactId>
<executions>
<!--first class-->
<execution>
<id>Build-A</id>
<configuration>
<archive>
<manifest>
<mainClass>test.java.com.br.Vision.Direct_Launch_Test</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>dependencies-jar</descriptorRef>
</descriptorRefs>
<finalName>FirstClass.jar</finalName>
</configuration>
</execution>

<!--first class-->
<execution>
<id>Build-B</id>
<configuration>
<archive>
<manifest>
<mainClass>test.java.com.br.SupportSaveUploade.To_Upload_Support_Save</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>dependencies-jar</descriptorRef>
</descriptorRefs>
<finalName>SecondClass.jar</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8.0_45</jdk.version>
<spring.version>4.1.1.RELEASE</spring.version>
<jstl.version>1.2</jstl.version>
<junit.version>4.11</junit.version>

</properties>

rD. rD.
Answer

Specifically for maven, you can use multiple execution tag and configure multiple <mainClass> in your pom.xml

<build>
<plugins>
    <plugin>

        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <!--first class-->
          <execution>
            <id>Build-A</id>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>test.java.FirstClass</mainClass>
                </manifest>
              </archive>
              <descriptorRefs>
                <descriptorRef>dependencies-jar</descriptorRef>
              </descriptorRefs>
              <finalName>FirstClass.jar</finalName>
            </configuration>
          </execution>

          <!--second class-->
          <execution>
            <id>Build-B</id>
            <configuration>
              <archive>
                <manifest>
                  <mainClass>test.java.SecondClass</mainClass>
                </manifest>
              </archive>
              <descriptorRefs>
                <descriptorRef>dependencies-jar</descriptorRef>
              </descriptorRefs>
              <finalName>SecondClass.jar</finalName>
            </configuration>
          </execution>
        </executions>
    </plugin>
</plugins>
</build>