0

Just started using Maven, I was wondering if there is any way to bound downloading dependencies for my project to the compilation phase of build lifecycle? If so, how to do that?

Like, I'd just go with:

mvn clean compile

and the code will be compiled & all the dependencies for the project will be downloaded.

  • 1
    What is your actual issue? Last time I did a `mvn clean compile` the code was compiled and the deps were downloaded –  Apr 30 '17 at 20:34
  • Well, I am compiling project with above method, and when I am trying to start the application with "java " I've got "ClassNotFoundException" for org.hibernate.service.ServiceRegistry, which is added through dependency in Maven. –  Apr 30 '17 at 20:41

1 Answers1

1

Depending on your goal (pun not originally intended), there are various options.

If you want to execute your application in place (from your development environment), you can use maven to execute your main class, with your dependencies in the classpath, by using maven exec plugin:

mvn exec:java -Dexec.mainClass="your.main.Class"

If you want to be able to package your application to something that you can distribute and run elsewhere, you have other choices:

  • maven assembly plugin, to build an archive that will bundle your app along with dependencies and a launcher script (that you will have to write)
  • maven shade plugin, to build a "fat jar" with your code and its dependencies, as a single file runnable with java yourfatjar.jar

There are more alternatives, above are just pointers for the main ones: for each you'll find a ton of other more comprehensive Stack Overflow answers.

Hugues M.
  • 19,846
  • 6
  • 37
  • 65