In Maven, how output the classpath being used?

后端 未结 7 1335
庸人自扰
庸人自扰 2020-12-12 23:31

For my current purposes I have a Maven project which creates a war file, and I want to see what actual classpath it is using when creating the war.

7条回答
  •  余生分开走
    2020-12-13 00:35

    This command outputs the classpath on Mac and Linux:

    mvn -q exec:exec -Dexec.executable=echo -Dexec.args="%classpath"
    

    Having the result printed and not saved into a file might be useful, for instance, when assigning the result to a variable in a Bash script. This solution runs on Mac and Linux only, but so do Bash shell scripts.

    In Windows (e.g. in BAT files), where there is no echo executable, you will need something like this (untested):

    mvn -q exec:exec -Dexec.executable=cmd -Dexec.args="/c echo %classpath"
    

    Alternatively, you can just execute java program with the classpath:

    mvn -q exec:exec -Dexec.executable=java -Dexec.args="-cp %classpath Main"
    

    Or even like that (it will use the correct classpath automatically):

    mvn -q exec:java -Dexec.mainClass="Main" 
    

    However, both these alternative approaches suffer from Maven adding its error messages when your program fails.

提交回复
热议问题