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 :
Related
I have a gradle project where I've added the archunit-junit5 dependency and written some test classes with #ArchTests. These get picked up by IntelliJ.
How do I get gradle to execute them?
I've found the com.societegenerale.commons:arch-unit-gradle-plugin but that seems to need configuration in the gradle file.
I just want gradle to pick up the tests I already have in the test/java directory.
Gradle should pick up #ArchTests with archunit-junit5 if you useJUnitPlatform().
https://github.com/TNG/ArchUnit-Examples/tree/main/example-junit5 shows a quite minimal working example.
I would like to use some classes defined in the test section of a Maven project for tests of another project that depends on it. The classes are more testing-related utilities than actual tests. This is achieved in SBT by using the notation test->test when declaring the dependency.
Is there a Maven equivalent for this? Does the solution work with IntelliJ?
You don't do that in Maven.
You write an additional project/module that contains the testing-related utilities.
I have multi-module Java project. In one of the modules, there is a top level Kotlin function which is accessible when called inside the module but not accessible from different module. See the failing test here. To make it even more confusing, when executed through mvn clean test all test pass, but when executed using mvn clean package, the compilation fails. I suspect it's something related to kotlin_module file missing in JAR file, but since it's poorly documented, I do not know how to fix it.
It was caused by OSGi bundle plugin that was missing Include-Resource directive to add files from target/classes/META-INF. See this commit for details.
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
Yay, my thesis is done! Now that the pressure is off and I've had my fill of playing Skyrim, I'm converting the code I wrote for my thesis from a chaotic directory built with ant to a nice maven project.
I originally had a bin directory with about 20 bash scripts that ran the various java and ruby programs used in my thesis, including the final jruby/sinatra-based web server. I am planning on moving my scripts to src/main/scripts, but I need to figure out how to handle the classpath.
I had previously just hardcoded paths in my scripts to the manually-downloaded dependencies. However, now that maven is downloading and storing all the jars I need, what's the best way to reference them from my scripts:
Should I just get the scripts to reference the full paths of various jars in the local repository like before?
Should I make the local repository directory a configuration option for my scripts and use relative paths to this directory?
Should I build a big hairy jar with all the dependencies using the maven assembly plugin and access this via the script-relative path ../../../target/*-jar-with-dependencies.jar?
Is there some better option I haven't thought of?
In your script, use the exec:java plugin to run Java classes. It will sort out the classpath based on the defined dependencies. Then you don't need to worry about it.
Relook at all the scripts that you have. Potentially you could achieve the functionality of some of them using maven exec plugin.
Besides assembly and shade plugins, you may want to look at the functionalities provided by maven dependency plugin as well.
In my project (Soluvas fb-tools/fbcli), because I use Java 6 and later (which supports wildcard classpaths), here's what I do:
#!/bin/bash
# Must run first: mvn package dependency:copy-dependencies
java -cp 'target/dependency/*:target/fbcli-1.0.0-SNAPSHOT.jar' org.jboss.weld.environment.se.StartMain "$#"
No need for manual generation of classpaths. :)
There are quite some plugin doing similar things you mentioned. Assembly plugin you mentioned is doubtless one of them (and the way you suggested is also a neat working solution).
You may want to take a look in AppAssembler and Shade. They all provide some mechanism to bundle the dependencies and produce a directly executable package.
Here is CLI example using Maven plugin exec:java as mentionned by #artbristol in another comment:
mvn exec:java -Dexec.mainClass="mypackage.MyClassWithMain" -Dexec.args="arg1 arg2"