Running EMMA with JMockit and JUnit in Maven - maven-2

I have a problem when running EMMA code coverage tool with JMockit + JUnit in maven.
I have a project and I am using JMockit as a mocking framework in it.
Once I run mvn test it is running successfully without any problem. That’s means JMockit is initializing with JUnit in a proper way.
Following is the way how I define my dependency for JMockit and JUnit within my POM (in the exact order).
<dependency>
<groupId>com.googlecode.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
But When I check the project for code coverage with EMMA, it says
java.lang.IllegalStateException: JMockit wasn't properly initialized; check that jmockit.jar precedes junit.jar in the classpath (if using JUnit; if not, check the documentation)
but I think I have configured EMMA plugin correctly and it is given below,
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<!—Some other plugins here -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>emma-maven-plugin</artifactId>
<version>1.0-alpha-3</version>
<inherited>true</inherited>
<configuration>
<check>
<classRate>100</classRate>
<methodRate>100</methodRate>
<blockRate>70</blockRate>
<haltOnFailure>false</haltOnFailure>
</check>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.googlecode.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>runtime</scope>
<version>${junit.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
Can anyone catch what is wrong there ?

I was able to figured out what went wrong there. It seems that we need to specifically say JUnit to use JMockit when work with EMMA.
We can do it by using maven-surefire-plugin.
We need to add following configuration to the POM.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<argLine>-javaagent:${settings.localRepository}/com/googlecode/jmockit/jmockit/1.7/jmockit-1.7.jar</argLine>
<useSystemClassLoader>true</useSystemClassLoader>
</configuration>
</plugin>
Note: Make sure to change the location of JMockit Jar in above configuration.
Further, we do not need to have dependencies within EMMA plugin configuration. Just having them in a dependencies section in POM (in exact order) will be enough.
References:
JMockit - initialization problem

Related

MessageBodyWriter not found Error when running from fatjar

I'm getting this error only when running from the fatjar, mvn run:java works as expected.
SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.example.Greeting, genericType=class com.example.Greeting.
I'm using moxy for json handling. I tried jackson, made no difference. Tried grizzly instead o jetty, same issue.
Here are my pom dependencies:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jetty</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
Shade plugin config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<id>shade</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<!--<minimizeJar>true</minimizeJar>-->
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.http.JettyHttpServer</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Jersey version is 2.30.1.
Thanks!
EDIT
Contents of org.glassfish.jersey.internal.spi.AutoDiscoverable does not include org.glassfish.jersey.moxy.json.internal.MoxyJsonAutoDiscoverable
org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable
org.glassfish.jersey.internal.config.ExternalPropertiesAutoDiscoverable
According to this it should. How do I fix it?
Try loading the dependencies you need explicitly on code and not relying on auto discovery, registering them on Jersey.
The fat jar plug-in tries to analyze your code and see what's being used and what's not, so it can exclude references not used and make a smaller jar. The problem with auto discovery is that since there are no explicit references to some classes, the shade plug-in can just think their aren't being used so they can be removed.
I think there's a way to force them to be included on the plug-in configuration, but I don't remember that configuration from memory, you would have to search for it in the docs.

net.masterthought.cucumber.ValidationException: No report file was added

Report is not getting generated When i am using cucumber-maven reporting plugin see image here
Primary Thing: Message "net.masterthought.cucumber.ValidationException: No report file was added!" generally comes when there is something wrong in the configuration of Maven Cucumber Html Report Plugin. (configuration example below)
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<version>4.2.3</version>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<projectName>TheDayAfterTomorrow</projectName>
<!-- output directory for the generated report -->
<outputDirectory>${project.build.directory}/cucumber-maven-report</outputDirectory>
<inputDirectory>${project.build.directory}/cucumber-json</inputDirectory>
<jsonFiles>
<!-- supports wildcard or name pattern -->
<param>**/*.json</param>
</jsonFiles>
<skippedFails>true</skippedFails>
<enableFlashCharts>true</enableFlashCharts>
<buildNumber>10.2.1</buildNumber>
<parallelTesting>false</parallelTesting>
</configuration>
</execution>
</executions>
</plugin>
Secondary Thing, in case you are still using old version of cucumber (1.2.5) so update your POM file with latest available cucumber version or anything above >=4.0.0 in order to have better outcome.
Cucumber Execution via JUnit:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.6</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.6</version>
</dependency>
Cucumber Execution via TestNG:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.6</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>4.2.6</version>
</dependency>
The root cause of this issue is that maven-cucumber-reporting can't find any JSON file in the directory that was defined in the POM. This JSON file is generated using #CucumberOptions annotation in the runner class. So the problem that you see is because you have defined a different directory in #CucumberOptions and in the inputDirectory field within the configuration of the plugin in the POM file.
To solve it, make sure that both directories match. For example:
In the runner file you should have target/cucumber/cucumber.json
#CucumberOptions(features = "classpath:feature", tags = "#component-test",
glue = {"feature.component"},
plugin = {"json:target/cucumber/cucumber.json")
So in the POM you should have <inputDirectory>${project.build.directory}/cucumber</inputDirectory> and you should make sure that jsonFiles property is correctly defined:
<jsonFiles>
<param>**/*.json</param>
</jsonFiles>
So at the end the configuration should look like:
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<version>${maven-cucumber-reporting.version}</version>
<executions>
<execution>
<id>generate-cucumber-reports</id>
<phase>test</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<jsonFiles>
<param>**/*.json</param>
</jsonFiles>
<outputDirectory>${project.build.directory}/cucumber-reports</outputDirectory>
<inputDirectory>${project.build.directory}/cucumber</inputDirectory>
<checkBuildResult>false</checkBuildResult>
</configuration>
</execution>
</executions>
</plugin>

How can I make sure that aspects generated by Spring roo are woven by a dependent project?

I have a Spring Roo multi module project. I noticed that after including the jar/module containing my domain model in another project, the aspects had not been woven leaving me with domain classes without any usable getters/setters.
How can I make sure that aspects generated by Spring roo are woven by the dependent project?
EDIT: In order to be able to use Roo's aspects from another project, one needs to include the aspectJ plugin in the dependent project. Note that compile time loading is not needed that way.
You need to include the aspectj maven plugin in the pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<!-- NB: do not use 1.3 or 1.3.x due to MASPECTJ-90 and do not use 1.4
due to declare parents issue -->
<dependencies>
<!-- NB: You must use Maven 2.0.9 or above or these are ignored (see
MNG-2972) -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
<!-- NB: force aspect compile before normal compile, required for 1.3+
see: MASPECTJ-13, MASPECTJ-92 -->
<phase>process-sources</phase>
</execution>
</executions>
<configuration>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>

yui compressor maven: A required class is missing: org.mozilla.javascript.ErrorReporter

I am not able to use yui-compressor maven plugin in my web app. When I run maven I get following error
[INFO] Internal error in the plugin manager executing goal 'net.sf.alchim:yuicompressor-maven-plugin:0.7.1:compress': Unable to load the mojo 'net.sf.alchim:
yuicompressor-maven-plugin:0.7.1:compress'
in the plugin 'net.sf.alchim:yuicompressor-maven-plugin'. A required class is missing: org.mozilla.javascript.ErrorReporter
Later I found that rhino js plugin contains this class org.mozilla.javascript.ErrorReporter. So I included this plugin in dependency tag but still I am getting the same error.
Has anyone came across such error.
--> updating main question to add the pom plugin details
<plugin>
<groupId>net.sf.alchim</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>0.7.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jslint</goal>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<failOnWarning>true</failOnWarning>
<nosuffix>true</nosuffix>
<aggregations>
<aggregation>
<!-- remove files after aggregation (default: false) -->
<removeIncluded>false</removeIncluded>
<!-- insert new line after each concatenation (default: false) -->
<insertNewLine>false</insertNewLine>
<output>${project.basedir}/${webcontent.dir}/js/compressedAll.js</output>
<!-- files to include, path relative to output's directory or absolute path-->
<!--inputDir>base directory for non absolute includes, default to parent dir of output</inputDir-->
<includes>
<include>**/autocomplete.js</include>
<include>**/calendar.js</include>
<include>**/dialogs.js</include>
<include>**/download.js</include>
<include>**/folding.js</include>
<include>**/jquery-1.4.2.min.js</include>
<include>**/jquery.bgiframe.min.js</include>
<include>**/jquery.loadmask.js</include>
<include>**/jquery.printelement-1.1.js</include>
<include>**/jquery.tablesorter.mod.js</include>
<include>**/jquery.tablesorter.pager.js</include>
<include>**/jquery.validate.js</include>
<include>**/jquery-ui-1.8.custom.min.js</include>
<include>**/languageDropdown.js</include>
<include>**/messages.js</include>
<include>**/print.js</include>
<include>**/tables.js</include>
<include>**/tabs.js</include>
<include>**/uwTooltip.js</include>
</includes>
<!-- files to exclude, path relative to output's directory-->
</aggregation>
</aggregations>
</configuration>
<dependencies>
<dependency>
<groupId>rhino</groupId>
<artifactId>js</artifactId>
<scope>compile</scope>
<version>1.6R5</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0.7</version>
<scope>provided</scope>
</dependency><dependency>
<groupId>net.sf.retrotranslator</groupId>
<artifactId>retrotranslator-runtime</artifactId>
<version>1.2.9</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin>
Could you try the latest version (1.1)?
The 0.7.1 version doesn't even seem to be on the official repository. Maybe a dependency resolution problem?
See the topic Yui compressor StringIndexOutOfBoundsException on jboss
The only way to use yuicompressor on web app is to manually merge it with rhino dependency. Otherwise, the app to run would require specifying required sequence of jars in classloader loading sequence (youcompressor must go before rhino).
I struggled with the ErrorReporter class missing too. I solved it by building a jar-with-dependencies which I then turned around to use in my web app,
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
Once I did that, everything worked. In my jar I could see that the org.mozilla.javascript.ErrorReporter.class was in there and Maven would then compile for me.

How to make maven-pmd-plugin support the latest PMD release?

http://maven.apache.org/plugins/maven-pmd-plugin/ is currently in version 2.4 which supports PMD version 4.2.2
Is it possible to use PMD version 4.2.5 with this plugin, if so how do we do this?
There is a Jira Issue for this, see MPMD-97 (I suggest to vote for it).
For now, you can try to upgrade locally the pmd version used in the plugin with:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>2.4</version>
<dependencies>
<dependency>
<groupId>pmd</groupId>
<artifactId>pmd-jdk14</artifactId>
<version>4.2.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
I didn't test this, I don't know if it'll work seamlessly.
This is an old question and things have moved on, but I faced a challenge upgrading maven-pmd-plugin:3.8 from pmd 5.6.1 to 5.8.1.
The documentation for doing this was missing from the maven-pmd-plugin page at the time of writing.
Add pmd-core, pmd-java and any other mavenized PMD artifacts as plugin dependencies.
If your rules are contained within in a separate .jar module, add that module to the dependencies also.
Ensure that the check goal is run after the compile phase - validate is too soon. (I picked process-test-classes to invoke it just before any tests are run rather than the more usual verify which is also OK but will run it later).
pom.xml configuration:
<properties>
<rev.javac>1.8</rev.javac>
<rev.pmd-plugin>3.8</rev.pmd-plugin>
<rev.pmd>5.8.1</rev.pmd>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${rev.pmd-plugin}</version>
<dependencies>
<dependency>
<groupId>my.project.group</groupId>
<artifactId>project-standards</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
<version>${rev.pmd}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>
<version>${rev.pmd}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>pmd-validation</id>
<phase>process-test-classes</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<rulesets>
<ruleset>/pmd/project-pmd-rules.xml</ruleset>
</rulesets>
<targetDirectory>${project.build.directory}</targetDirectory>
<targetJdk>${rev.javac}</targetJdk>
<failOnViolation>true</failOnViolation>
<failurePriority>5</failurePriority>
<verbose>false</verbose>
<linkXRef>false</linkXRef>
</configuration>
</plugin>