Maven2 + JMeter + JUnit with dependencies - maven-2

I'd like to run my JUnit tests in JMeter. Using maven-jar-plugin I can create a jar with my tests in order to put it inside the JMeter's classpath ($JMETER_HOME/lib/junit). The problem is that my tests have a lot of dependencies that Maven2 doesn't put into the jar, including the main classes of the project, classes from other projects and external libraries. How can I do this?

You can use the fatjar plugin.

As iwein has mentioned, you may use the maven-fatjar-plugin which will put all the dependent JAR's inside of your JAR artifact and create the appropriate MANIFEST entries to include them on your classpath.
Another option is that you can use the maven-shade-plugin which will simply take all of the ".class" files out of the dependecy JARs and include them directly in your JAR. This is called a UBER-JAR. There are a couple of reasons which I prefer this approach:
This often leads to slightly smaller JARs
I have other plugins which already manipulate the MANIFEST (including the Classpath property) and I don't want to chance having an incorrect manifest being generated.
Creating an UBER-JAR is just too good to pass up ;)

Related

Gradle project with no project classes but with some buildscript-accessible custom classes?

I am thinking to use Gradle to manipulate with mysql database. It will read some files from filesystem, analyse them and populate database accordingly.
Such project will not produce any project code, because all output will go to database tables. On the other hand, gradle script should access some custom java or groovy classes to facilitate working with source data.
Is this a possible Gradle usage? Where to put gradle-accessible classes then? I don't want to have separate project, producing JAR for this project. I wan't single project, so that Gradle first compiles classes and the utilizes them in the script.
Is this possible?
Gradle is extensible, so you can utilize buildSrc for such scenarios. It works in the following way:
along build.gradle in the project there is buildSrc dir with custom build.gradle
in buildSrc/build.gradle you can define the script dependencies itself, implement plugins and tasks
finally you can apply a plugin from buildSrc to build.gradle.
It's quite handy, since e.g. IntelliJ can import such project and provide code completion for instance.
Another way is to put all the necessary stuff in build.gradle itself.
Such buildSrc project can be compiled to a jar, published and provided as a plugin, or it can be a separate project on github to be downloaded and used to manipulate data. Also, there no need to implement Plugin, you can use static methods e.g. Have a look at the demo.

How to automatically generate JAXB classes on build in IDEA?

I have an IDEA project that uses auto-generated JAXB classes from .xsd files. I have “client” and “server” modules that include a “common” module that contains, among other things, the JAXB classes.
I do not want to keep generated code under source control, but if the generated java classes do not exist, “client” and “server” modules do not compile. How to make IntelliJ automatically run JAXB before building?
There is no direct way to do it only with IntelliJ IDEA, you will need to use Ant or Maven or some other external process that will perform the code generation.
Check out jaxb2-maven-plugin.
In IntelliJ IDEA you can execute Maven or Ant before compilation.
In the build system's tool window you can bind a phase or a plugin goal to IDEA's build process.
For example the jaxb2-maven-plugin can be executed Before Rebuild or Before Build with a secondary click on the goal:
Another option would be to bind the goal to a lifecycle phase and execute the phase like 'generate-sources' before rebuild. In case of the jaxb2-maven-plugin the goal xjc is by default bound to the generate-sources phase of Maven.

Can I install several files into one artifact with Maven2 instal:install-file command

I'm developping application with JOGL2 and my favorite IDE Eclipse, also I want to use Maven2 for this purpose. Unfortunately, JOGL2 has no artifact yet. Also, I plan to deploy it as a runnable jar file.
So I want to install JOGL artifact locally : so i'll use the install:install-file command.
But I want to group several jars to make several artifacts, that is :
gluegen-rt.jar and jogl.all.jar as a single artifact named jogl.core
gluegen-rt-natives-linux-i586.jar and jogl.all-natives-linux-i586.jar as a single jar named jogl-natives-linux-i586
and so on
Is it possible ? (The official documentation does not mention the possibility or unpossibility to do so).
Thanks in advance
Install all files as usual like file:jar:version. Than create pom with pom packaging and use gluegen-rt.jar and jogl.all.jar as dependencies in it (they must be already installed). After that use new pom as dependency in your project.
maven doesn't have support for that. You would have to unpack these JAR files and repackage them together.
maven does have support for merging JAR with dependencies (http://stackoverflow.com/questions/574594) - and it's done the way I mentioned above. But you are asking about merging two arbitrary JARs, which is not possible in maven.

How do I make the manifest available during a Maven/Surefire unittest run "mvn test"?

How do I make the manifest available during a Maven/Surefire unittest run "mvn test" ?
I have an open-source project that I am converting from Ant to Maven, including its unit tests. Here's the project source repository with the Maven project:
http://github.com/znerd/logdoc
My question pertains to the primary module, called "base". This module has a unit test that tests the behaviour of the static method getVersion() in the class org.znerd.logdoc.Library. This method returns:
Library.class.getPackage().getImplementationVersion()
The getImplementationVersion() method returns a value of a setting in the manifest file. So far, so good. I have tested this in the past and it works well, as long as the manifest is indeed available on the classpath at the path META-INF/MANIFEST.MF (either on the file system or inside a JAR file).
Now my challenge is that the manifest file is not available when I run the unit tests:
mvn test
Surefire runs the unit tests, but my unit test fails with a mesage indicating that Library.getVersion() returned null.
When I want to check the JAR, I find that it has not even been generated. Maven/Surefire runs the unit tests against the classes, before the resources are added to the classpath.
Further investigation shows Surefire generates its own JAR file in a temporary directory, e.g.
/private/var/folders/TR/TREvj1wIHYyAcUy-xmc3UU+++TI/-Tmp-/surefirebooter7448562488934426857.jar
And then uses this JAR to load the Library class. This JAR does not contain the resources I stuck under src/main/resources. So putting a META-INF/MANIFEST.MF file also does not work.
So how do I tell Surefire to have my META-INF/MANIFEST.MF file available from the same class loader as the Library class.
Note that I use Maven 2.2.0, Java 1.6.0_17 on Mac OS X 10.6.2, with JUnit 4.8.1.
Well, as you pointed you, the problem is that the MANIFEST.MF is generated during package and directly included in the final jar and all this occurs after test. So I guess you'll have to either:
provide your own MANIFEST.MF (that would be available in target/classes before being merged during package). I don't know if this is an option (and if it will work).
put and run your test from another module depending on the JAR.

find dependencies in target/classes instead of local repository?

Summary: I'm looking for a way to instruct maven to search for dependencies in target/classes instead of jar in the local repository
Say I have 2 modules, A and B where A depends on B. Both are listed in a module S. Normally I need to run 'mvn install' in S. I'm looking for a way to run 'mvn compile' so that when A is compiled its classpath will contain ../B/target/classes instead of ~/.m2/repository/com/company/b/1.0/b-1.0.jar.
(my reason is so that i can have continous compilation without the need to go through packaing and installation, or, more exactly, use 'mvn scala:cc' on multiple modules)
I don't think that this is possible without horrible hacking, this is just not how maven works. Maven uses binary dependencies and needs a local repository to resolve them. So, the maven way to handle this is to launch a reactor build on all modules. Just in case, have a look at Maven Tips and Tricks: Advanced Reactor Options.
But, during development, can't you just import all your projects in your IDE and use "project references" (i.e. configure your projects to depend on source code instead of a JAR) like most Java developers are doing? This is the common approach to avoid having to install an artifact to "see" the modifications.
If this is not possible and if you really don't want to install artifacts into your local repository, then you'll have to move your code into a unique module.
i know this is annoying. which helped me here is definitely IDE support. eclipse and IntelliJ are clever to collect all dependencies once a maven-project import is done. even cross module dependencies are compiled live.