how can measure on test code using javancss maven plugin - java

I’m using javancss maven plugin for measuring lines of codes and cyclomatic complexity.
I can not find the way of configuring the plugin in order to analyze test code as well.
This is a fragment of pom.xml file:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>javancss-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<includes>
<include>**/Exam.java</include>
<include>**/Topic.java</include>
<include>**/TopicSequenceTest.java</include>
<include>**/TestWithMockObjects.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</reporting>
This is the project structure:
src
main
...
test
...
This is the way I use it:
mvn javadoc:javadoc javancss:report jxr:jxr jxr:test-jxr
I think it doesn’t work because ** is representing src/main, then it can not include any java file under src/test
But I cannot find the solution.
Thank you in advance

Related

How to add maven-checkstyle to existing project?

I tried adding the maven-checkstyle-plugin to my project, but it does not produce any results:
<project>
....
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<configLocation>google_checks.xml</configLocation>
</configuration>
</plugin>
</plugins>
</build>
</project>
mvn checkstyle:checkstyle or mvn checkstyle:check results in:
[INFO] You have 0 Checkstyle violations.
Which is essentially not true, because I added violations explicit in my code.
So my guess is that checkstyle is not running the analyser properly over my code (that resides just normal in /src/main/resources).
What could I do?
Would I maybe have to add the google_checks.xml file explicit to my classpath??

Exclude a package from build path in POM

I have a package de.ht.ak.praktikum.hook in a project which is in a source folder src but it should be excluded from build path. I used to do this with right click on it and choosing Build Path -> Exclude. Since I added maven to the project every time I update the project the excluded folder turns into package again, i.e. the exclusion gets deleted. I tried to fix it this way:
...
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>de/ht/ak/praktikum/hook</exclude>
</excludes>
</resource>
</resources>
...
</build>
...
I tried also to do it as described there:
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>3.1</version>
<configuration>
<sourceExcludes>
<sourceExclude>de/ht/ak/praktikum/hook/</sourceExclude>
</sourceExcludes>
<sourceIncludes>
<sourceInclude>**/*.java</sourceInclude>
</sourceIncludes>
</configuration>
</plugin>
...
However none of the both methods help. Any ideas?
Your first attempt won't work because you're specifying to exclude it as a resource (i.e., those files that get packaged in your resulting JAR file - you don't usually want source files to be among them).
The second attempt is more on the right track. However, you want to exclude them from compilation, hence you need to set the exclude option of the maven-compiler-plugin. I.e., like this:
<build>
..
<plugins>
..
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<excludes>
<exclude>de/ht/ak/praktikum/hook/*.java</exclude>
</excludes>
</configuration>
</plugin>
..
</plugins>
</build>
When updating the project in Eclipse (Maven -> Update Project), Eclipse should honor this configuration and also exclude it from the Eclipse-internal build path.

Cobertura excludes not working in multi-module Maven 3 project

I have a multi-module Maven 3 project. We are using Cobertura as our code coverage tool, but the excludes tag is not working. We have some bad tests from a package we inherited from another team, but need to consume.
The structure is as follows:
<module1>
.../com/aaaa/...
<module2>
.../com/aaaa/...
<module3>
.../com/aaaa/...
...
<moduleN>
packages with .../com/xx/... WE WANT TO EXCLUDE
pacakges with .../com/aaaa/... WE WANT TO STILL INCLUDE
parent-pom.xml
Our parent POM is configured as such:
<build>
...
<plugins>
<other plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<aggregate>true</aggregate>
<outputDirectory>coverageReports</outputDirectory>
<instrumentation>
<excludes>
<exclude>**/com/xx/**/*</exclude>
</excludes>
</instrumentation>
/configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<aggregate>true</aggregate>
<outputDirectory>coverageReports</outputDirectory>
</configuration>
</plugin>
</plugins>
</reporting>
I've tried a lot of various configurations, including:
Excluding the test/com/xx files as well
Adding the exclusion pattern to ignore
Setting exclude in the reporting AND build section
Multiple permutations of the exclude file pattern, including being more implicit
Any thoughts? I've had some other build engineers look at my various POM configurations and it always seems valid, but we never get accurate reports.
Put the exclude configuration into the pom.xml file of moduleN that you want to do the exclusions from:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<instrumentation>
<excludes>
<exclude>com/aaa/**/*.class</exclude>
<exclude>com/xxx/**/*.class</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>

cobertura-maven-plugin excludes configuration

I have a Maven project with a test case DefaultViewTypeToFragmentMapperTest.java in the directory /src/test/java/test/com/mycompany/myproduct/android/viewtype2fragmentmapper/.
I want this test case to be excluded from unit test coverage calculation. In order to achieve this result, I configured the plugin like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<instrumentation>
<excludes>
<exclude>test/co/**/*.class</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>
But I still see the aforementioned class in the coverage report.
How can I fix it such that the test case does not appear in the report and its coverage (0 % according to the report) is not taken into account?
After a lot try and fail I got it working.
Edit the pom.
mvn clean test-compile
mvn cobertura:cobertura
Reopen the page from Firefox. (make sure the page is not cached)
I got it working with:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.6</version>
<configuration>
<instrumentation>
<excludes>
<exclude>aaa/**/*.class</exclude>
<exclude>com/test/bbb/**/*.class</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>
Change 'aaa' with the beginning of the package name to be excluded.
Change 'bbb' with your package name that you want to exclude from the report.
I hope it helps,
Marc Andreu
You should use the <ignore> tag.
<configuration>
<instrumentation>
<ignores>
<ignore>com.example.boringcode.*</ignore>
</ignores>
</instrumentation>
</configuration>
<exclude> used within <instrumentation> simply excludes the package from what your instrumenting. Which in this case, is nothing because you're not doing anything.
Please see the Mojo Maven Cobertura Plugin docs here.
Is it a typo?
<exclude>test/co/**/*.class</exclude>.
The co should be com.
BTW, <ignores> instructs Cobertura to ignore any calls to any method that matches the ignore regular expression. It will NOT skip over these classes during instrumention. To exclude classes from being instrumented, <excludes> should be used.
You should not append the .class as the following example
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<instrumentation>
<excludes>
<exclude>test/co/**/*</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>
I hope this may help.
I just lost two hours of my life getting an exclusion for Cobertura to be excluded, but finally succeeded.
The solution I found is that the plugin configuration with instrumentation & exclusion for the cobertura-maven-plugin MUST be in the build/plugins or build/pluginManagement chapter of the pom, while there also must be a reference to the cobertura-maven-plugin in the reporting chapter.
The pitfall here is that you initially start with defining the plugin at the reporting chapter, otherwise no report is generated. But the instrumentation itself doesn't pick up any configuration from that part of the pom. You need to define that within the build chapter.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<instrumentation>
<excludes>
<exclude>my/exclusion/package/**</exclude>
</excludes>
</instrumentation>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
</plugin>
</plugins>
</reporting>
In addition to BertKoor's answer, I'd like to point out that if you're executing mvn cobertura:cobertura or mvn cobertura:cobertura-integration-test directly, your report will still include coverage on all instrumented classes found in your target directory, as reported here!
If this is the case, make sure you do mvn **clean** cobertura:cobertura in order to clean up the target dir from a previous build first, and then instrument and run your tests.

Maven: Excluding tests from build

I have some classes I'm using as tests in my src/test/java folder of my project. When I run maven using the standard maven compile plugin. Those items are compiled into .class files and are included in the jar where the compiled code is packaged.
I've created these tests for myself to run within eclipse, prior to running maven and building my release. They are just sanity tests and should not be included in the build. I'd rather not put them in a seperate project, because, to me, they make sense here. How can I tell maven that I do not want it to compile/include the files in that directory?
I beleive the maven compiler plugin is generating the jar as follows:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
I understand from your comment on this answer that the "tests" aren't unit tests, but just ordinary classes that you want excluded from the final artifact? As such, your best option is to make use of the <exclude> tag with the maven-jar-plugin as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>**/yoursortoftestpackage/YourSortOfTestClass*</exclude>
</excludes>
</configuration>
</plugin>
Hope that helps!
Cheers,
Use the option of -Dmaven.test.skip will skip both compilation and execution of the tests. ( use -DskipTests just skips the test execution, the tests are still compiled)
The reference link
mvn clean install -Dmaven.test.skip
Annotation for junit #Ignore will ignore the class while building in maven.
Edit:
You can configure maven-surefire-plugin.
<project>
<build>
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
Put your custom test files in a folder src/localtest/java i think, maven will not know it is there.

Categories

Resources