0

I have created a maven project and trying to generate a SINGLE jar file should contain both /src/main/java & /src/test/java.

But it is generating two SEPARATE jar files. Please let me know, how can I achieve this in maven?

generated jar files:

test-0.0.1-SNAPSHOT.jar

test-0.0.1-SNAPSHOT-tests.jar

my pom.xml:

<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.testmaven</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>test</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>test-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
  • 1
    The location `src/test/java` is intended for unit tests which should never being made part of a jar which is intended to be consumed by others nor in general. If you like to have the classes from `src/test/java` into a jar as you already configured the correct way using `test-jar`...The question: Why do you like to have them into a single jar? Make from my point of view no sense? – khmarbaise Nov 12 '19 at 12:23
  • It worked with build-helper-maven-plugin – srinivas rao Nov 14 '19 at 10:33
  • I strongly discourage with using buildhelper cause there is an issue there but unfortunately you haven't answered my question..so I can't further help here. – khmarbaise Nov 14 '19 at 22:22

2 Answers2

0

You cannot access test classes from application code. So no maven plugin will help you to add src/main/java and src/test/java in a single executable jar.

If at all you want to access the main classes from the test project, check the answer similar to your question on stackoverflow here:

How can I include test classes into Maven jar and execute them?

0

I have tried with below approach and it worked.

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>

   <execution>
       <id>add-source</id>
       <phase>generate-sources</phase>
       <goals>
          <goal>add-source</goal>
       </goals>
       <configuration>
          <sources>
              <source>${project.basedir}/src/test/java/</source>
          </sources>
       </configuration>
   </execution>

 </executions>