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 NoClassDefFoundError
s 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.
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 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.
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
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.
There is no out-of-the-box solution for your task. But you can create a simple script to solve it:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With