Modify Maven's classpath - maven

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

Related

maven provided dependency will cause NoClassDefFoundError in intellij?

IntelliJ doesn't seem to put the provided dependency on the classpath when I run it, however I can do this successfully in Eclipse.
As it would be a lot more convenient for me, how can I do this in IntelliJ?
I'm having the same problem. Intellij does not include provided dependencies in classpath. See this source. The best solution I found is to run it as maven app, using the exec:java goal. For example:
exec:java -Dexec.classpathScope=compile -Dexec.mainClass=com.splout.db.integration.NShardEnsemble -Dexec.args=4
Better solutions are welcome.
Does it work in Maven via command line? The behaviour seems correct. Eclipse used to handle classpath badly, so I guess it still does.
There is a difference if you run something in Test source root or Source root, since the scope provided is only available on the compilation and test classpath.
If you run a test, or a main method in Test source root, then it can use provided dependencies, but if you try to execute something (via IntelliJ, or exec-maven-plugin) in Source root, then it will fail on ClassNotFoundException.
IntelliJ now has an option to Include dependencies with provided scope in the Run Configuration:
Any library marked as scope - provided means that the library (as the name suggests) is supposed to be provided by the JDK or the container (e.g. tomcat) at runtime.
this answer is based on #Meo's answer.
ALT + Enter to create a unit test:
then run it :

How to manage compile time dependencies in Maven

Trying to avoid the use of jargon, so that I don't get misinterpreted.
Here is the scenario, My project requires a jar in order to get compiled(let say x.jar). My project get once compiled gets converted into a WAR file, which gets deployed somewhere.
Now I want x.jar just to be there for my project to compile and it should not be packed(or part of) inside WAR file.
How can I do this in Maven ? should I used dependency scope as "provided"
You are right, as stated in the Maven FAQs, the scope to use is provided,
How do I prevent including JARs in WEB-INF/lib? I need a "compile only" scope!
The scope you should use for this is provided. This indicates to Maven that the dependency will be provided at run time by its container or the JDK, for example.
Dependencies with this scope will not be passed on transitively, nor will they be bundled in an package such as a WAR, or included in the runtime classpath.
To quickly try it out, you can use
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp
to generate a "toy webapp" project, add a dependency to your project and set it to <scope>provided</scope>.

How to reference packaged resources from the jasmine maven plugin?

I want to use the jasmine-maven-plugin to test my maven my-webapp project. This project depends on another my-lib project that contains some required JavaScript libraries. When the my-webapp project is built, it adds the my-lib JAR to the WEB-INF/lib/ path of the generated WAR. Inside the my-lib JAR, the needed JS resources are in folders META-INF/resources and META-INF/test-resources.
How can I reference these packaged resources from the jasmine-maven-plugin goals jasmine:bdd and jasmine:test?
Note that I've also tried to run the goals in the integration-test phase like explained here, but I still can't reference the needed resources.
UPDATE: Would running jetty:run-war from within the jasmine-maven-plugin help? If so, how can I achieve that?
I think you would need to first use the maven-dependency-plugin to unpack the jar, under a different goal.
Something like this: unpack dependency and repack classes using maven?
Then you can specify the parameters, under the configuration section of the plugin for that goal, from wherever you unpacked the jar.
wherever/you/unpacked/
Run the unpack goal first, then the bdd and test.

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.

get maven to call a method before jar

how do i get maven to run a specific method of mine before it creates the jar file?
basically, i have java code in my junits folder (not a test, just a class with a
main([path to save to])
) that generates an xml file that must be included in the jar. how do i get maven to follow this flow?
compile
run custom method to create xml file passing it the path of the build folder (this method needs the full classpath of the project to run)
jar classes
Bind the maven exec plugin to the generate-resources phase. And your are done.
In short, make it a plugin/MOJO.
In long, philosophy of Maven is not having you to write the build logic inside. It is a more declarative approach.
Anyway, you still have a last resort: use maven-antrun plugin to write a short logic to call whatever u need as ant target.

Resources