Maven equivalent of SBT "test->test" - maven

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.

Related

can you build a maven project inside of a gradle wrapper

It may be just that I have a general misunderstanding how gradle build works, but it feels to me that I can not build a maven file inside of a gradle build. Since gradle uses the gradle.build file, and maven uses a pom.xml, it does not seem as though I can do this. I have multiple maven projects that I would like to wrap up with a gradle wrapper. I can not find ANYTHING on whether this is even possible.
Both Maven and Gradle are build tools and you should only use one of them for a given project.
If you have existing Maven projects and like the functionality provided by the Gradle wrapper, there is a similar wrapper for Maven (note that this is currently a third-party plugin but they plan to include it in the upcoming release 3.7 of Maven).
Alternatively you could convert your projects entirely to Gradle.

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 :

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

How should a bash script determine a classpath for a maven project and its dependencies?

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"

RCP build with maven

How to build and package RCP (Rich client Platform) using Eclipse.
Is it possible to build as a .jar file.
I use Maven Tycho. I find it a lot easier to use than the PDE ant scripts.
The itp04 RCP example is a good project to get started.
We use Maven 3 and the sonatype-tycho plugin to build our Eclipse RCP-based application. It allows a plugin-first approach, i.e. you define the dependencies only in the plugin.xml using the editor of Eclipse. You don't have to care about dependencies in the pom.xml as these are managed by tycho.
There are detailed instructions at Apache Felix maven Bundle page on how to do this.
Alternatively, There are some quickstart maven archetypes you could download based on these instructions and experiment with.
I have wrapped PDE/Build by Maven manually. You can find an example in my answer to my own question:
How to set up Eclipse PDE/Build with Indigo?

Resources