0

I'm hacking on a Maven-based project with a lot of dependencies; the project is normally meant to be developed in Eclipse but I need to work on it from the command line.

How to build+execute the project in a sane way? Something like mvn run, but of course Maven is not meant for running Java projects (for some reason).

The problem is specifying all the dependencies on java's commandline, I don't even know how to autogenerate that. I can currently deal with it using the assembly:single maven plugin (using the jar-with-dependencies descriptor) which will package the dependencies to a single .jar for me.

However, there really is a lot of dependencies and the assembly phase can take about two minutes, greatly disrupting my hack-test cycles so I'm looking for other ways to run the project with minimum build overhead - any recommendations, please?

Note: One possibility is running it in Eclipse once and capturing the java commandline. However, that's just a one-time hack, not a general solution in case I change pom.xml later or come to another project from the suite without Eclipse access anymore.

Petr Baudis
  • 1,178
  • 8
  • 13

4 Answers4

3

Have a look at the maven exec plugin

mvn exec:java -Dexec.mainClass="com.example.Main"

if you do this frequently, you can of course configure it via plugin configuration.

Max Fichtelmann
  • 3,366
  • 1
  • 22
  • 27
1

See this question: How can I create an executable JAR with dependencies using Maven?. You can use the dependency-plugin to generate all dependencies in a separate directory before the package phase and then include that in the classpath of the manifest.

Community
  • 1
  • 1
chairbender
  • 839
  • 6
  • 14
1

Regarding finding out project dependencies - you can use maven dependency plugin

http://maven.apache.org/plugins/maven-dependency-plugin/list-mojo.html

If you want to put them into file it'd be smth like

mvn dependency:list > dependencies.txt
amerykanin
  • 255
  • 2
  • 5
  • 15
0

I see three solution to this:

  • onejar-maven-plugin - faster than assemlby with jar-with-dependencies descriptor

    With onejar-maven-plugin, you'll (...) get a nice clean super jar with the dependency jars inside.

  • Spring Boot Maven Plugin - but this is dedicated to Spring projects

  • Maven Assembly Plugin with custom descriptor. This custom descriptor should grab all dependencies into lib folder, maven-jar-plugin should set Class-Path in Manifest.fm according to this new location. After this you can simply execute your program or zip your jar with lib folder as distribution.

After this is possible to run your program on your computer or any other with one command:

java -jar myjar.jar
MariuszS
  • 30,646
  • 12
  • 114
  • 155