Maven surefile plugin use - maven

Can any one please tell me the use of adding plugins in POM.xml file in Maven project. I tried working with creating new maven project and run it. In the site which reffered suggesting to add those two plugins. Can anyone please tell me the use ? Are these plugins important for running maven test ?

The Surefire plugin is an implicit plugin that will automatically run your tests in a jar project, i.e. mvn test binds to the surefire:test goal. You don't need to add it to your POM unless you need to customize its configuration.
From the Surefire documentation:
By default, the Surefire Plugin will automatically include all test
classes with the following wildcard patterns:
"**/Test*.java" - includes all of its subdirectories and all Java
filenames that start with "Test". "**/*Test.java" - includes all of
its subdirectories and all Java filenames that end with "Test".
"**/*TestCase.java" - includes all of its subdirectories and all Java
filenames that end with "TestCase".
See this page for more details:
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings
http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html
http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

Related

Is there a way to make Maven warn or error on bad file extensions?

IntelliJ defaults to locating a new Spock test in the java/ folder rather that the groovy/ folder, so we periodically wind up with groovy tests in our java directory, which are not run.
Is there an option in Maven, or in a plugin, to fail a build based upon the presence of a file with an unwanted extension?
The Maven Enforcer Plugin allows the writing of custom rules, which can applied to the build lifecycle. The plugin can be configured to fail on the first rule that does not pass.

What is the purpose of the # symbol in <groupId>#project.groupId#</groupId>?

I'm trying to run 'mvn clean install' in a sub-module of a multi-module project. The project is Jacoco but I assume it's more an issue of me not understanding how Maven is being used here than an issue with Jacoco itself.
https://github.com/jacoco/jacoco/blob/master/jacoco-maven-plugin.test/it/it-site/pom.xml
I get the following error:
[ERROR] Plugin #project.groupId#:jacoco-maven-plugin:#project.version#
or one of its dependencies could not be resolved: Failed to read
artifact descriptor for
#project.groupId#:jacoco-maven-plugin:jar:#project.version#
I see the following in the pom:
<groupId>#project.groupId#</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
I'm not familiar with using the # symbol in #project.groupId# although i assume it is somehow supposed to get substituted at runtime.
I can run Maven from the top level pom and I even see [INFO] Building: it-site/pom.xml in the log but a target directory is not created there.
A nudge in the right direction would be appreciated.
This probably has something to do with the pom file here: https://github.com/jacoco/jacoco/blob/master/jacoco-maven-plugin.test/pom.xml
It is using a plugin called the maven invoker.
The Invoker Plugin is used to run a set of Maven projects. The plugin can determine whether each project execution is successful, and optionally can verify the output generated from a given project execution.
And if you read about filtering files with this plugin, it mentions:
POM files selected by the include/exclude patterns. The tokens to be filtered must be enclosed in #...# to avoid interferences with Maven's normal POM interpolation, i.e. use #project.version# instead of ${project.version}.
Regarding why the Invoker Plugin and filtering is being used here...
The Spring Boot documentation provides some relevant background on why that is. Although the docs are for Spring Boot, i think it applies to testing all plugins (which Jacoco is).
Multi-module Maven builds cannot directly include maven plugins that
are part of the reactor unless they have previously been built. ...
The standard build works around this restriction by launching the
samples via the maven-invoker-plugin so that they are not part of the
reactor.
The sample application are used as integration tests during the build
(when you mvn install). Due to the fact that they make use of the
spring-boot-maven-plugin they cannot be called directly, and so
instead are launched via the maven-invoker-plugin.

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.

Maven surefire test results for a project with various modules

I've a parent pom project with various submodules. I want to get a report of the unit tests results but using surfire plugin, I get an independent result for each module.
I mean, if I execute:
mvn surefire-report:report
from the parent directory where the parent pom is located, it creates a surefire-report.html for each subproject but what I want, is only one html with the results of all the subprojectes.
Is there any way to achieve this automatically? Using surefire or some other plugin.
To create the aggregate report please try to use the following command at theparent project.
mvn surefire-report:report -Daggregate=true
I hope this may help.

Can we use maven surefire plugin to execute testcases present in the source directory

The Java project I am working on is a list of testcases written in Java for testing C++ code. So I want to run the testcases from the src directory in the test phase of the maven lifecycle. How do I configure the maven surefire plugin to achieve this.
Since it is Java based tests that presumably will not be part of the production code, I suggest that you follow Maven's standard directory layout and move the tests to the src/test/java directory as usual (otherwise, the tests will be part of the binary file if someone executes mvn package).

Resources