Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JShell, how to import classpath from a Maven project

I have a local Maven project under development. How can I launch jshell with the project class path with all the dependencies, so that I can test project or dependency classes inside JShell.

like image 929
Jianwu Chen Avatar asked Dec 07 '17 22:12

Jianwu Chen


People also ask

What is the classpath of a Maven project?

Maven creates this classpath by considering the project's dependencies. The reported classpath consists of references to JAR files cached in local Maven repository. Even the JAR artifact of the project is referenced from local Maven cache and not from the /target directory one or the other might expect.

Does Maven add to classpath?

Maven Archiver can add the classpath of your project to the manifest.

Can I add jars to maven 2 build classpath without installing them?

Maven install plugin has command line usage to install a jar into the local repository, POM is optional but you will have to specify the GroupId, ArtifactId, Version and Packaging (all the POM stuff). -1, sometimes you just want to add a jar file without the trouble of installing it.


3 Answers

You can use the jshell-maven-plugin:

mvn com.github.johnpoth:jshell-maven-plugin:1.3:run

which will fire up a JShell session with your project's runtime path. If you want to include your test dependencies just add -DtestClasspath to the command.

NOTE: the plugin expects the project to be built already. If not, invoke the appropriate Maven build phase before the plugin e.g:

mvn [install|compile|test-compile] com.github.johnpoth:jshell-maven-plugin:1.3:run

Source code: https://github.com/johnpoth/jshell-maven-plugin; contributions are welcome :) full disclaimer: I wrote the plugin.

Enjoy!

like image 161
jpoth Avatar answered Oct 17 '22 02:10

jpoth


I wrote a simple shell script put in the execution search path:

Shell script file: mshell (for *inux)

mvn dependency:build-classpath -DincludeTypes=jar -Dmdep.outputFile=.cp.txt
jshell --class-path `cat .cp.txt`:target/classes

Shell script file: mshell (for Windows cmd.exe)

mvn dependency:build-classpath -DincludeTypes=jar -Dmdep.outputFile=.cp.txt
for /F %i in (.cp.txt) do jshell --class-path "%i;target/classes"

Then in the maven project directory (for multi-module project, make sure in the module directory instead of parent directory), run:

$ cd $MAVEN_PROJECT_HOME   #make sure module folder for multi-module project
$ mshell

gist link

Thanks Jay for pointing out -DincludeTypes=jar maven option.

like image 23
Jianwu Chen Avatar answered Oct 17 '22 02:10

Jianwu Chen


See In Maven, how to output the classpath being used?.

According to:

jshell --help

run JShell with:

jshell --class-path <path>
like image 3
Gerold Broser Avatar answered Oct 17 '22 00:10

Gerold Broser