3

I'm trying to execute a Java file in a subproject in a maven reactor project. This is similar to the question Maven exec:java goal on a multi-module project, but unless I do a mvn install then the exec plugin can't find the class that I'm trying to run in the subproject.

Perhaps I misunderstand the intended workflow of mvn install, but having to do mvn install every time I make changes really complicates the workflow.

When I execute the file from Eclipse, Eclipse sets up the classpath correctly (i.e. module1/target/classes, module2/target/classes) and I want to emulate this behaviour from the command line. I thought doing mvn -pl exec:java -Dexec.mainClass=... would set up the classpath in this way, but the class is not found in this case.

Community
  • 1
  • 1
bcoughlan
  • 25,987
  • 18
  • 90
  • 141
  • To the reviewers, could you please explain why the question is too localised? I think it's only mildly more specific than the question I referenced and can't see the logic in it – bcoughlan May 20 '13 at 21:13

1 Answers1

0

The classpath isn't the problem in that case. But you have to compile your classes (e.g. at least run mvn compile). If you run your application within Eclipse, Eclipse will do the compile work, on the commandline you have to explicitly call that command.

dunni
  • 43,386
  • 10
  • 104
  • 99
  • I should have mentioned that I already did a maven compile, and the target/classes folders exist. I ran `mvn -pl module1 -am exec:java -X -Dexec.mainClass=com.MyClass`. The output suggests that it tries to put my parent pom on the classpath: `[DEBUG] Adding to classpath : file:/home/.../project/target/classes` but not the subprojects. – bcoughlan May 20 '13 at 16:59
  • I think that exec:java goal requires all dependencies to be installed in local repo, so running compile before the exec:java goal is not enough. However, I am now also really curious how does it actually work in case of m2e if it is really like you say - that it works fine in Eclipse... – makasprzak May 20 '13 at 18:42
  • m2e sets up an Eclipse project but Eclipse doesn't actually use the Maven command line to run, it just runs `java -cp module1/target/classes:module2/target/classes.... com.MyClass`. I could make a script to do this but that's not repeatable or clean. – bcoughlan May 20 '13 at 21:03