0

I am new to Maven and have a project with the following directory structure

project

-src
  -main
    -java
      -org
        -core
          -starter.java
  -test
    -java
      -org
        -core
          -testStarter.java

Both starter.java and testStarter.java are Main classes. However I wish to create an executable jar file (including dependencies) where testStarter.java is executed.

I added the following to my pom file:

<groupId>org</groupId>
<artifactId>proj</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>project</name>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>org.core.testStarter</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    <plugins>
</build>

However everytime I run mvn clean package, it creates a package with only the compiled classes in the main directory and not the test directory. Hence everytime I run the jar file, it does not run, since the testStarter.class does not exist in the created jar file.

Is there a way I can create either

1) One big jar file containing test and main classes while it executes only the test class

2) Only the test classes but all dependencies are included.

4 Answers4

2

Well, this seems kinda strange, but what you can do is to put testStarter under main (I suppose this won't work because it has references to other classes under the testfolder, am I right?). Btw, use CapitalCamelCase class names, that's more Java-ish.

Alternatively, you can use test-jar to create a jar for the tests (this might work if you add the original jar to the Class-Path manifest property), or you can start hacking the whole assembly of your project, but I wouldn't recommend that.

What I'd suggest is rethinking your project layout and clarifying your requirements. Probably you won't need this process.

rlegendi
  • 10,466
  • 3
  • 38
  • 50
2

According to you pom.xml you have already had a big jar with all dependencies and correct classname in manifest.

All you need now is to include your test classes into artifact.

  1. One big jar file containing test and main classes while it executes only the test class. You can specify you /src/test directory as another source directory (see Maven compile with multiple src directories).
  2. Only the test classes but all dependencies are included. You can mark your test directory as the only source directory by using sourceDirectory tag in build section. But I guess, your tests depend on code in main, so this configuration could fail on compile time.

p.s. I strongly recommend you to create separate build-profile for such case, because it seems strange for me to have main method for jar in test class.

arghtype
  • 4,376
  • 11
  • 45
  • 60
2

I'd recommend using the Maven OneJar Plugin,

look here: https://code.google.com/p/onejar-maven-plugin/

"Lets you build an executable jar with Maven2, containing all dependencies"

As for you scoping issue-- You need to move that Main class to the src folder so that it can be in the compile scope.

Cheers!

g00dnatur3
  • 1,173
  • 9
  • 16
2

We do this in our project by using a assembly.xml with the maven-assembly-plugin. In the POM we have:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <descriptors>
                <descriptor>src/main/resources/assembly.xml</descriptor>
            </descriptors>
        </configuration>
        <executions>
            <execution>
                <id>package-jar-with-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </execution>
        </executions>
    </plugin>

Then there is a assembly.xml in src/main/resources which tells how to build that assembly:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>bin</id>
    <formats>
        <format>jar</format>
    </formats>
    <!-- Adds dependencies to jar package under lib directory -->
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory></outputDirectory>
            <unpack>true</unpack>
        </dependencySet>
    </dependencySets>
</assembly>
SCI
  • 546
  • 3
  • 6