Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a Java class inside a Maven artifact, automatically resolving dependencies?

If I know the coordinates of an artifact, and a name of the class inside that artifact, can I make Maven run the class, including all of its dependencies on the Java classpath?

For example, suppose a coworker told me about a tool I can run, which is published to our internal Nexus with the artifact coordinates example:cool-tools:1.0.0. I used this answer to download the artifact. Now, I know that the main class name is example.Main. But if I just go to the artifact's download location and run java -cp cool-tools-1.0.0.jar example.Main, I get NoClassDefFoundErrors for any dependencies of cool-tools.

I'm aware of the maven-exec-plugin, but as far as I can tell that's only for projects where you have the source. Suppose I don't have access to the source, only the Nexus containing the tool (and all its dependencies). Ideally, I'd do something like mvn exec:exec -DmainArtifact='example:cool-tools:1.0.0' -DmainClass='example.Main', but I don't think the exec plugin is actually capable of this.

ETA: To be clear, I do not have a local project / POM. I want to do this using only the command line, without writing a POM, if possible.

like image 255
Coderer Avatar asked Jul 03 '14 13:07

Coderer


People also ask

How to run a Java main class from your Maven project?

In order to run a Java main class from your Maven project you can use the Maven exec plugin. In this short article we will learn how to run it at your fingertips. exec:exec executes programs and Java programs in a separate process. exec:java executes Java programs in the same VM.

How do I add a java file to a Maven project?

How to add your java file into the maven project. Add TestNG test framework jar files in this maven project. 1. Build & Run Maven Project. Go to the project folder, in my environment it is C:\WorkSpace\MvnExampleProject\dev2qaExample.

What happens if the dependencies are not properly placed in Maven?

In your maven project, If the dependencies are placed not properly. This may result to create problems such as unused dependencies, duplicate dependencies, version conflicts…etc. These result… Open in app Home Notifications Lists Stories Write Published in Javarevisited Anish Antony Follow Jan 8, 2021 4 min read Member-only Save

How do I clean a Maven project in Linux?

Build & Run Maven Project. Go to the project folder, in my environment it is C:\WorkSpace\MvnExampleProject\dev2qaExample. Open a command prompt and run the command mvn clean package. The argument clean means remove all existing java class files and recompile all the java source files.


1 Answers

There is no out-of-the-box solution for your task. But you can create a simple script to solve it:

  • Download pom.xml of your tool from the repo.
  • Download jar of your tool.
  • Download all its dependencies.
  • Run java against resolved libraries.

Command line:

> mvn dependency:copy -Dartifact=<tool.group.id>:<tool.artifact.id>:<tool.version>:pom -DoutputDirectory=target
> mvn dependency:copy -Dartifact=<tool.group.id>:<tool.artifact.id>:<tool.version> -DoutputDirectory=target
> mvn dependency:copy-dependencies -f target/<tool.artifact.id>-<tool.version>.pom -DoutputDirectory=target
> java -cp target/* <tool.main.class>

Directory ./target will contain your tool + all dependencies.

See details on dependency:copy and dependency:copy-dependencies mojos.

Edit

As alternative, you can build classpath using libraries in the local repo by:

> mvn dependency:build-classpath -DincludeScope=runtime -f target/<tool.artifact.id>-<tool.version>.pom [-Dmdep.outputFile=/full/path/to/file]

See details on build-classpath mojo.

like image 81
ursa Avatar answered Sep 28 '22 08:09

ursa