Executing a JAR file straight from a Maven repository
Asked Answered
P

3

10

Suppose that we have a Java application, rather than a library, which is available through Maven central. For example, one such project is jol, which has its corresponding CLI interface in Maven central.

As far as I can tell, the main difference from a library is that the corresponding JAR file contains a class with an appropriate main() method and, optionally, a related Main-Class: header in the JAR manifest. If such an artifact is used as a dependency on a project, Maven will happily download the JAR file to the local repository along with its dependencies, as it does for any other artifact.

Is it possible to use Maven to execute such an application directly, without setting up a Maven project?

The exec:java plugin works nicely for local projects by setting up the JVM classpath so that dependencies are available. The user does not have to worry about JAR or .class file locations and such. Unfortunately, from what I can tell, it also requires an enclosing Maven project, so it cannot be used from an arbitrary command line prompt.

Presber answered 22/7, 2014 at 16:18 Comment(4)
I'd probably just wrap up something like #1776996 with a shell script that then executes the downloaded artifact. Or write a simple program that talks Maven repo and can fetch an artifact and skip Maven altogether.Thomey
@DaveNewton: Getting the JAR file is not the only issue. exec:java, for example, will set up the JVM classpath so that dependencies are available. That is what I am after...Presber
You can get the jar's dependencies programmatically and do the same thing, though. In any case, if you have Maven installed, then creating an ad-hoc project with your top-level dependency is a pretty small POM file. Why can't you just create the project on-the-fly? What's the issue?Thomey
@DaveNewton: I suppose the main issue is that I am not fond of reinventing the wheel, if a solution already exists. Also, doing it right with a script is not quite that simple (or that portable, for that matter)...Presber
T
4

No, Maven will not do what you are asking for. It is a build tool, intended to build a Java project based on it's pom.xml file which describes the project.

So, you can't run a maven build without a pom.xml file. And if you have a pom.xml, then by definition, you have 'set up a Maven project'.

As @DaveNewton says, you should be able to set up a very small pom.xml with the dependency for the jar file in question, and the exec-maven plugin. I'm afraid that it's just not going to get any simpler than that.

Tammitammie answered 22/7, 2014 at 17:59 Comment(0)
G
2

A hacky solution working with Maven 3 would be to use the Maven Dependency Plugin in combination with the Maven Help Plugin to resolve the local repository path:

# Download JAR from Maven repo
mvn dependency:get -DremoteRepositories=http://repo1.maven.org/maven2/ \
                   -DgroupId=some.group.id \
                   -DartifactId=some-artifact-cli \
                   -Dversion=1.0.0 \
                   -Dtransitive=false

# Resolve local repository path
MVN_REPO=$(mvn help:evaluate -Dexpression=settings.localRepository -q -DforceStdout)

# Execute JAR from the local repository
java -jar $MVN_REPO/some/group/id/some-artifact-cli/1.0.0/some-artifact-cli-1.0.0.jar
Georgiannegeorgic answered 9/7, 2020 at 7:13 Comment(0)
S
0

You can now use ktx to execute a jar file from it's maven coordinates.

For example:

ktx run com.github.ricksbrown:cowsay:1.1.0 hello

Usage is explained on project's repo : https://github.com/mpetuska/ktx

Streamlined answered 7/9, 2023 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.