Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Maven plugin that runs a non-fat jar?

Imagine a normal java maven project with a Main class that produces the artifact project-a.jar. This project has a dependency on project-b.jar.

Is there a Maven plugin that allows to run that jar by a command like that?

mvn run-plugin:run org.mygroup:project-a:3.1 <args>

The plugin would resolve the runtime dependencies (using META-INF/maven/(...)/pom.xml), install the project and its dependencies to the local maven repository (if not already there), construct the classpath and invoke

java -cp (...)/project-a-3.1.jar;(...)/project-b-2.1.jar org.mygroup.Main <args>

I know that the usual way is to build an executable (fat) jar that contains the dependencies, but that's not what I am asking for.

Actually, it is not even necesary to read the pom from the jar, because maven can download it from the repositories given the coordinates.

Why this question is different to the Maven Run Project question:

I do not want to start from having the project's source already checked out. So the usual use of the exec plugin is not applicable. The OP of the Maven Run Project question obviously assumed the presence of a source code project folder. Her purpose was testing and she accepted an answer that clearly needs a project. The wording of both questions is correct, too. There is a difference between the words "project" and "jar" and their actual meaning in their respective contexts is quite different.

like image 737
Gustave Avatar asked Jan 18 '26 22:01

Gustave


1 Answers

You can use the appassembler-maven-plugin plugin, it creates a shell script that has the dependencies in the classpath for you. Heres an example config

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <extraJvmArguments>-Xms256m -Xmx1536m</extraJvmArguments>
                <programs>
                    <program>
                        <mainClass>com.package.MyMainClass</mainClass>
                        <name>TestFormattingUtils</name>
                    </program>
                </programs>
            </configuration>
        </plugin>

You can find the output script in .../target/appassembler/bin You can manually inspect the script and you'll see that its doing the type of command you wanted where it adds the jars to classpath via the command line. ie java -jar (...)/project-a-3.1.jar -cp (...)/project-b-2.1.jar <args>

like image 126
Carlos Bribiescas Avatar answered Jan 21 '26 11:01

Carlos Bribiescas