Conditionally run JUnit tests from Maven - java

In Maven, is there some way to specify that JUnit tests only run when there are some source code changes?
For instance, I see this message
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) # my_project ---
[INFO] Nothing to compile - all classes are up to date
Now -since no code was compiled (since there were no changes from the last compile), I'd prefer that no tests run. Is this possible?

Your project setup should be broken up into separate modules. However if you can not do that you could set the maven.test.skip property in your profiles in which you want no tests to be run. See more about skipping tests on the site.
You are talking about a war project. Typically a war project should NOT have any code in it, but rather just some configuration and stuff like html, jsp, css and java script and bundle jar projects, that contain java code. The jar projects will have the code. And if you do integration tests a good practice is to have them in separate modules.
Different war configs can be in profiles or potentially also in separate modules using the war overlay feature of the maven war plugin.

Related

Maven: Finding out if tests passed or failed after using maven.test.failure.ignore=true

I'm trying to run a complete Maven build of multiple projects for an automated build tool. If unit tests fail, but the project itself builds correctly, I want to be able to continue the build and detect this after the build completes. I tried doing this:
mvn clean package -Dmaven.test.failure.ignore=true -Dmaven.test.error.ignore=true -Dmaven.test.reportsDirectory=/Users/bfraser/misc/reports
The "maven.test.failure.ignore" and "maven.test.error.ignore" properties work fine. However, surefire seems to ignore the "maven.test.reportsDirectory" completely (in fact, if you look at the documentation for the test goal, the reportsDirectory property is not documented to be tied to the system variable). This may be because I'm building a multi-module project? All reports seem to go in the target/ folder of the subprojects.
It is very difficult for me to be able to edit the POMs in an automated way since many of them have parent POMs that might be on a Nexus repo somewhere, etc. -- I need to be able to do this externally to the project (preferable via command line switches, but if I need to create some files so be it... as long as I don't have to edit the project POM it's all good).
I just need to know if any test failed. I'm not particularly fussy about what/how many tests failed.

Maven extra sources wildcards

I'm adding some extra sources to my Maven project and until now all is working well. I just want to make the extra source selection a little more more generic, because today I have something like:
<source>client/plugins/plug1/classes</source>
<source>client/plugins/plug2/classes</source>
<source>client/plugins/plug3/classes</source>
<source>client/plugins/plug4/classes</source>
<source>client/plugins/plug5/classes</source>
...
And I want to do it in this way:
<source>client/plugins/**/classes</source>
That's because some developers do not have all the plugins under their working space, and I don't want to modify my pom.xml in each dev environment.
Unfortunately that pattern doesn't work -at least in Eclipse- because the folders are not marked as source. I made a little research and I saw that Maven uses the Ant patterns but it is not working.
Could you please tell me if it is possible to use that convention in <sources> definition?
No, this is not possible.
The sources attribute of the build-helper-maven-plugin:add-source goal is a File array, which means that every source must be a File, hence you can't use an Ant path pattern. The plugin will literally add the file client/plugins/**/classes as a source directory (and since it doesn't exist, nothing will happen). This is the logs that I had when I tested it:
[INFO] --- build-helper-maven-plugin:1.10:add-source (add-source) # test ---
[INFO] Source directory: <omitted>\client\plugins\**\classes added.
But there's the matter of why you would want such a thing. It isn't clear from your question where are those "plugins" coming from but you shouldn't be in that situation. Each plugin should probably be a separate module of a multi-module Maven project. This way, each plugin will handle its build and you can add a dependency on those modules.

Is there any plugin or feature that determines which JARs are unused in an application or a project (in a given project life cycle)

A typical Java project has 30-40 jar files. Maven resolves dependencies. Sometimes some features are no longer needed. But no one touches the JARs with the fear of breaking the code or application.
For example: jersey (JAX-RS) implementation with JAXB has many workarounds, different Maven configurations.
Different developers sometimes employ different methods to do the same thing. All are correct. I agree they should discuss out. But the problem is that unused JARs make the build package heavy.
Isn't there a plugin which can map (use reflection) and check for one time (one project life cycle) and mark unwanted or unused JARs. This will use java reflection and may get slow but this is needed.
Any solutions?
Maven helps to find out unused dependencies with dependency plugin.
mvn dependency:analyze
its displays the unused dependencies as follow
[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) # mymaven ---
[WARNING] Unused declared dependencies found:
[WARNING] org.apache.commons:commons-lang3:jar:3.0:compile

Skip integration tests in maven by directory

Our project structure looks something like this (sorry about the formatting):
server/
pom.xml
src/
main/
java/
test/
java/
it/
java/
I would like mvn clean test to skip all the tests in it/java regardless of the name of the test. The solutions I have seen depend on integration tests being in a different package or having some prefix on the name to indicate that they are integration tests.
Is there a way to exclude it/java from running when I run mvn clean test? Eventually we want to be able to run the integration tests with by running mvn integration-test.
Your best best is probably to make it a multi-module project, and then in the it module skip tests based on different maven properties. This would allow you the flexibility that you're looking for.
I don't think src/it/java is a folder known to maven so if you put your tests in there they will be ignored simply because its a random folder to maven? :)
Anyway. To execute integration tests I recommend using the maven-failsafe-plugin which was build for this: http://maven.apache.org/surefire/maven-failsafe-plugin/
It does also rely on some naming convention to distinguish integration tests from standard ones but I could re-configure the testSourceDirectory. But I think the classes may need to be compiled first if you change that folder (but that should be possible).
The unit tests are then configured to be executed by the maven-surefire-plugin.
What I usually to is have the surefire-plugin execute all tests but exclude the IntegrationTests (using a pattern in the configuration) and the failsafe-plugin include those but exclude the standard tests. But I have the tests named differently so I can use the conventions for the folders.

How to run unit tests in multi-module maven setup if i put the tests in a separate module?

i have created a multi-module maven project and i am trying to write and execute some tests on specific modules. I am trying to put all test-code into a separate module, but i am wondering if this is the correct way and if so how to i setup the maven build/test cycle so mvn install will use these tests?
According to the Maven Standard Directory Layout test classes belong to the directory src/test/java and required resources to src/test/resources. On the long term it will make your life easier by following the rules, especially when you work on many different projects (where you sometimes can not define alternative structures). In this setup the test cases are automatically invoked by mvn install or mvn test.
You find more informations about the surefire-plugin (responsible for executing the tests) here.

Categories