Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run mvn exec:exec without a pom.xml

Tags:

maven

I am trying to find a way just to fetch a library from maven repository and run it. I want to define everything by command line and ignore the pom. I am trying following:

 mvn exec:exec -Dexec.mainClass="org.main.Class" -Dspring.profiles.active=test

When I try to run it with pom.xml, it starts to fetch all the dependencies described in the pom. Which I really don't want. When I run it without the pom.xml, it says Goal requires a project to execute but there is no POM in this directory. Please verify you invoked Maven from the correct directory. Is there a way to run it without the pom or at least ignore it?

My goal is just to start my application from anywhere without sources or jars.

like image 647
Vojtěch Avatar asked Jan 23 '18 23:01

Vojtěch


People also ask

Can maven work without pom xml?

Nope. It's impossible. Mvn Exec is, in fact, Maven Pluging dedicated to execute something as part of maven-nature project. If you need to execute Jar outcome why not use just java ***.

What does mvn exec exec do?

Maven exec plugin allows us to execute system and Java programs from the maven command. There are two goals of the maven exec plugin: exec:exec - can be used to execute any program in a separate process. exec:java - can be used to run a Java program in the same VM.

What does mvn exec Java?

mvn exec:java is a goal from the exec plugin for maven. It lets you specify a main class to execute (see pom. xml). This lets you avoid having to figure out the proper java command to run and classpath arguments and the like.


1 Answers

One possible workaround would be to:

  • first get the jar
  • then execute it

For that, the maven plugin dependency:get is handy, and can be executed without any pom.xml/project (so from any folder you want)

mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:get -DrepoUrl=https://dtp.intramundi.com/nexus/repository/maven-central/ -Dartifact=<group>:<artifact>:<version>

Once you have copied the artifact to the folder of your choice, a plugin like dependency:copy-dependencies could, reading the pom.xml of the artifact you just got, copy said dependencies to the same folder of your choice.

Finally, a java -cp . yourArtifact.jar can execute it.

I realize this is not as straightforward as just "exec:exec", and that it fetches dependencies from the pom.xml, but here at least:

  • the first step does not require any pom.xml,
  • the second step needs the pom.xml of the very artifact you want to execute, and those dependencies might be needed at runtime: you would need those anyway if you want to 'exec' your artifact.
like image 138
VonC Avatar answered Oct 21 '22 05:10

VonC