Adding a bootstrap classpath when testing with Maven - maven

My library requires me to bootstrap the JVM in order to run it. In case you do not know, if you pass a jar to the JVM with the -bootstrap option, you can override (substitute) any Java library implementation like java.lang.String, etc.
So all I need to do is tell Maven that when it runs my tests it include the -bootclasspath option with my jar/classes.
Is that possible with Maven? Maybe maven will have to bootstrap itself, if the JVM is not able to add new bootstrap classes on-the-fly as it is able to add new classes/jar to the classpath?

Have you tried the argLine and the forkMode parameters of the surefire plugin?

Related

mvn spring-boot:run vs java -jar

I know it may sound silly question but I am unable to understand the difference between mvn spring-boot:run and java -jar (.jar file generated with mvn install)
I have a spring boot application with jsp pages in /src/main/resources/META-INF/resources/WEB-INF/. If I use mvn spring-boot:run these pages are served. But If I use java -jar these pages are not found by application.
The application that I am working on is at https://github.com/ArslanAnjum/angularSpringApi
UPDATE:
It works with spring boot 1.4.2.RELEASE while I intend to use the latest version i.e., 1.5.8.RELEASE.
UPDATE:
Well I solved the problem by putting jsps in src/main/webapp/WEB-INF/views/ and changing packaging type to war and then running this war using java -jar target/myapp.war and its working fine now.
Short answer: spring-boot:run is a java -jar command on steroïd running as part of your Maven build, ensuring all required parameters are passed to your app (such as resources). spring-boot:run will also ensure that your project is compiled by executing test-compile lifecycle goals prior to running your app.
Long answer:
When you run java -jar, you launch a new JVM instance with all the parameters you passed to this JVM. For example, using the Spring doc example
java -Xdebug -Xrunjdwp:server=y, \
transport=dt_socket, address=8000, suspend=
-jar target/myproject-0.0.1-SNAPSHOT.jar
You will launch a brand new JVM with the given parameters. You need to make sure to include everything needed, such as classpath elements, application parameters, JVM options, etc. on the command line.
When you run mvn spring-boot:run, you launch a Maven build that will:
Run the test-compile lifecycle goals, by default it will be resources:resources, compiler:compile, resources:testResources, compiler:testCompile goals of the Maven Resources and Compiler plugin.
Launch your application with a bunch of parameters that will depend on the
Spring Boot Maven Plugin configuration you defined in your project (your pom.xml, parents and settings, command line, etc.). This includes among other things:
A lot of classpath elements: your target/classes folder which may contain resources and libraries required by your app, your Maven dependencies, etc.
Whether to fork your JVM or not (whether to create a brand new JVM to run your app or re-use the JVM of the Maven build), see fork and agent parameter of the plugin
As per:
I have a spring boot application with jsp pages in
/src/main/resources/META-INF/resources/WEB-INF/. If I use mvn
spring-boot:run these pages are served. But If I use java -jar these
pages are not found by application.
It's because the mvn spring:boot command will make sure your target/classes folder is present in the Classpath when your app is running. After compilation, this folder will contain target/classes/META-INF/resources/WEB-INF among other things. Your app will then be able to find META-INF/resources/WEB-INF and load them when asked. When you ran java -jar command, this folder was probably not on the classpath, your app was then not able to find your resources. (these resources were copied from the src/main/resources folder during the resources:resources goal)
To have a similar result with your java -jar command, you must include your resources on the classpath such as javar -jar myapp.jar -cp $CLASSPATH;/path/to/my/project/target/classes/
Have you tried creating a jar file using mvn package instead of mvn install when you are running jar file using java -jar? package will create a jar/war as per your POM file whereas install will install generated jar file to the local repository for other dependencies if present.

How to pass '--list-config' to Jetty when using maven jetty plugin?

We can pass arguments like '--list-config', '--list-classpath', etc. when using Jetty's start.jar as shown at http://www.eclipse.org/jetty/documentation/9.2.8.v20150217/startup-classpath.html and http://www.eclipse.org/jetty/documentation/current/startup.html
I was wondering how could I pass them on while using Jetty Maven plugin. I tried < jvmArgs > and run-forked but that didn't work.
jetty-maven-plugin is just an embedded-jetty for Maven plugins.
It is unrelated to the jetty-distribution's start.jar standalone operation.
For the jetty-maven-plugin there is no Server Classpath, just the maven plugin's own Classpath, determined by maven, gleaned from the effective pom (pom hierarchy) of your maven build. Use maven's --debug to see the plugin classpath during the execution of that plugin's phase (note: the classpath can change between different phases)
The configuration is also part of the phase that the jetty-maven-plugin executes with. Again, use the --debug from maven to see that configuration.
The configuration you see from start.jar --list-config is 100% in the scope of the start.jar determination from the configuration information starting at ${jetty.base}/start.ini, and the selected jetty modules, along with your properties to build up a Jetty instance, a "pre-start" configuration that start.jar will execute to build up a Jetty instance.
Its like you are asking how to understand a bicycle (jetty-maven-plugin) by looking at an technical specifications manual for motorcycle (start.jar). There is only inconsequential overlap between the two: 2 wheels of approximately the same size, with handlebars, and a seat.
Yes, the differences between jetty-maven-plugin and start.jar configuration and startup are that vast.

Maven plugin java.library.path

The maven-surefire-plugin supports using the java.library.path property by means of the <argLine> configuration option.
I need to pass the java.library.path property to sql-maven-plugin (which doesn't have an <argLine> configuration option) in order to use the jTDS driver with windows authentication (needs ntlmauth.dll).
Thanks in advance for your help.
The surefire plugin is a special case; it runs the tests in a separate JVM. Most maven plugins don't do this, they run in-process.
So you are out of luck with that plugin config.
One workaround you can try is to use the exec:exec goal of the exec-maven-plugin instead, specifying java with -Djava.library.path on the executable line and roll your own SQL-executing java code.
Otherwise, you can add -Djava.library.path to your MAVEN_OPTS environment variable, which will affect all plugins and all maven builds (which might be fine, depends on your setup), or add it every time to the mvn command line.

Modify Maven's classpath

I'm looking for a way to modify the class path in maven. Why?
I want to instrument maven artifacts without corrupting the local repository such that when surefire-tests run it will see the instrumented classpath, not the original class path.
In general maven manages the classpath by itself.
Having said that, there are a couple of options you can try here:
You can use 'additionalClassPath' parameter in surefire plugin. You can read about ithere:
You can generate your instrumented jars and use them in scope test, don't use un-instrumented jars in the tests at all
Hope this helps

Debuging of jar contents in Intellij Idea

I have a non-trivial maven build which produces a jar fille and I want Itellij Idea to pick up maven's output and then launch the jar but IDE doesn't allow me to specify a jar to use in 'Application' launch configuration. Please suggest a way to configure project this way.
Thanks in advance
You can create a new dummy module with this jar added to its Dependencies and attach sources to the library, then you can debug it by specifying the main class in the Run configuration. The module itself may not contain sources or other content roots.

Resources