Configure NetBeans to execute single unit tests with JMockit as javaagent - netbeans

When using JMockit with Maven for unit tests, it is required to pass the location of jmockit.jar to the VM by setting the -javaagent parameter. The maven-dependency-plugin can do this automatically, I have set up a configuration that does the expected like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${dependency.plugin.version}</version>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
</configuration>
</plugin>
This works when the test suite and also single tests are called from command line, e.g. by
mvn test -Dtest=MyClass#someTest
From within NetBeans it is also possible to run the whole test suite (e.g. when "Clean and Build" is executed). But when a single file is tested, the path is not injected. There is a command like the following in the log when the VM crashes:
Command was /bin/sh -c cd /home/kap && /usr/lib/jvm/adoptopenjdk-8-hotspot-amd64/jre/bin/java '-javaagent:${org.jmockit:jmockit:jar}' -jar ...
i.e. the placeholder is not filled with the correct location. In contrast, a call on the command line produces
[DEBUG] Forking command line: /bin/sh -c cd /home/kap/ && /usr/lib/jvm/adoptopenjdk-8-hotspot-amd64/jre/bin/java -javaagent:/home/kap/.m2/repository/org/jmockit/jmockit/1.49/jmockit-1.49.jar
It is especially weird that it works with the whole test suite, but not for single tests.

Why does the single test fail?
When executing clean and build NetBeans by default executes mvn clean install. So the maven executes the goal org.apache.maven.plugins:maven-dependency-plugin:properties during its normal build lifecycle and the plugin creates variable ${org.jmockit:jmockit:jar}.
When executing single test file (i.e. Project -> TestFile -> RightClick -> Test File (or Ctl+F6)) NetBeans executes only single goal mvn -Dtest=MyClass#someTest surefire:test. So the maven-dependency-plugin:properties doesn't execute at all and maven could not find the variable ${org.jmockit:jmockit:jar} because it was not been created.
How to solve it?
Option 1.
Go to Project -> Properties -> Actions and for actions Test File set options to execute goal as follows:
test-compile org.apache.maven.plugins:maven-dependency-plugin:properties surefire:test
NetBeans creates the nbactions.xml file so the solution would work only when executing a single test from NetBeans.
Option 2.
Remove maven-dependency-plugin from you pom.xm. Instead specify the location to jmockit.jar using ${settings.localRepository} property:
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jmockit.version>1.43</jmockit.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>-javaagent:"${settings.localRepository}"/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar</argLine>
</configuration>
</plugin>
</plugins>
</build>
...
Option 3.
I would assume binding the properties goal to the test-compile phase of maven but it would only work if disabling the Compile On Save feature of NetBeans.
...
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>test-compile</phase>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
...

Related

Maven exec plugin not running

I am trying to executing a powershell script that writes to a file during a maven build.
I am invoking the build with mvn clean install through Eclipse IDE.
This is the plugin in my pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>${project.basedir}/.my-file.ps1</executable>
</configuration>
</plugin>
</plugins>
The powershell script is a hidden file so that's why I have a . in front of the file name.
However, the plugin is not executing and I am following the instructions in the official documentation.
You are running mvn clean install which will go through various build phases, but your exec plugin execution is not attached to any phase. You'll have to either:
Attach your execution to a phase by adding the <phase> element to your execution, for example to attach it to the pre-integration-test phase:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>my-exec</id>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>${project.basedir}/.my-file.ps1</executable>
</configuration>
</plugin>
Or call specifically the exec goal using mvn exec:exec command.
If you are not familiar with the Maven lifecycle and various phases of a build, read the Introduction to the Build Lifecycle guide, or specifically the Plugins part to learn more about plugins executions and phases attachment.

GWT compilation is skipped in maven when module packaging is pom

I am trying to reuse a assembled gwt compilation in another war. For this i am try to change the current maven module's packaging from war to pom. I then plan to use maven-assembly-plugin to zip up gwt's module js output and use that later on in another war module.
I tried changing the packaging tag from <packaging>war</packaging> to <packaging>pom</packaging> in pom.xml from Validation sample . gwt-maven-plugin never enters into compilation. Instead, it skips compilation!!!!!
What is happening?
Is this expected?
Is there any workaround?
To join multiple gwt compiled modules into a single .war file, it is very easy with the maven-dependency-plugin
Package all your gwt examples as habitual (.war), and install them mvn install or mvn deploy if you have a private maven repo.
Create an empty maven module of type war, with no code but with the maven folder structure, you can put any additional stuff you need here like a global src/main/webapp/index.html.
Configure the new module to use the maven-dependency-plugin like shown below, and run mvn package:
<dependency>
<groupId>my.group</groupId>
<artifactId>example1</artifactId>
<version>...</version>
<type>war</type>
</dependency>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-gwt-examples</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>my.group</includeGroupIds>
<includes>**/example1/**</includes>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Finally and related with the gwt-maven-plugin, like with any other maven pluging, it would be enough to select an appropriate phase of the pom-packaging life cycle (package, install or deploy):
...
<packaging>pom</packaging>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
...
<configuration>
...
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
Unfortunately, gwt-maven-plugin specifically disallows compilation when packaging is pom, take a look to line #269 of CompileMojo.java
You can create the reusable modules (that you mention as samples in the comments) as separate GWT projects with no EntryPoint. Package them as jar and add the following as resources:
the client side source code
other resource items that will be necessary for the final compilation (images, xml files, etc.)
Something like this:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
...
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/services/**</include>
<include>**/client/**</include>
<include>**/public/**</include>
<include>**/*.gwt.xml</include>
</includes>
</resource>
</resources>
</build>
That's it, you can reuse it in any other GWT project. When you will do so, you just have to add the dependency (to the reusable module) in the pom.xml and import in the *.gwt.xml.
As for Maven's behaviour, it seems correct. pom packaging is going through package, install and deploy phases and
By default, the compile goal is configured to be executed during the ''prepare-package'' phase to run as late as possible.
You could change the phase in the plugin's execution, but I think it's risky because you can't know when exactly during the package phase will your code get compiled.

Skip Maven Flex PMD

We are looking for an option in flex-pmd-maven-plugin to skip the pmd execution temporarily using a command-line option. The maven pmd plugin has a -Dpmd.skip=true option.
I am looking for a similar kind of thing for flex-pmd-maven-plugin too.
<plugin>
<groupId>com.adobe.ac</groupId>
<artifactId>flex-pmd-maven-plugin</artifactId>
<version>${flex-pmd.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<ruleSet>build_rules/Flexpmd.xml</ruleSet>
<excludePackage>**/Mocked*.as</excludePackage>
</configuration>
</plugin>
I had a look at the source code for the plugin and there seems to be no option to do that.
Any ideas or a work around on this is welcome..
I found a workaround to skipping the Flex PMD without having to actually remove the plugin.
The flex-pmd plugin runs in the verify maven phase. I got the phase in the plugin parameterized as and passed a command line argument -Dpmd.phase as either deploy or package.
An then I then ran the command
mvn clean install -Dpmd.phase=deploy
Since maven runs only until install phase and the pmd phase is changed to deploy, the Flex PMD is skipped.. :)
When I want to run the Flex PMD, I just change the hudson build to run the following
mvn clean install -Dpmd.phase=package
Here the code snippet
<plugin>
<groupId>com.adobe.ac</groupId>
<artifactId>flex-pmd-maven-plugin</artifactId>
<version>${flex-pmd.version}</version>
<executions>
<execution>
<phase>${pmd.phase}</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<ruleSet>build_rules/Flexpmd.xml</ruleSet>
<excludePackage>**/Mocked*.as</excludePackage>
</configuration>
</plugin>
I have also the following default property in the pom so that pmd will run by default for developers who run the normal mvn clean install
<properties>
<pmd.phase>package</pmd.phase>
</properties>

running a maven scala project

Im starting to learn scala and mongo , my IDE is intellij IDEA. I created a scala project using
mvn:archetype-generate
and typed a simple hello world program in the IDEA with some arithmetic options such as
println(5)
val i = 1+2
println(i)
Then i compiled it using
mvn compile
It said
build success
But now how should i execute my application and verify the output. There isn't a single article which explains how to start off with scala,maven,idea and i am entirely new to all of this. any help would be useful for me.
maven-exec-plugin
Try with this code:
package com.example
object Main {
def main(args: Array[String]) {
println(5)
val i = 1 + 2
println(i)
}
}
Place it under /src/main/scala/com/example/Main.scala and run it using:
$ mvn package exec:java -Dexec.mainClass=com.example.Main
If you don't want to pass mainClass manually, you can do this in plugin configuration:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
There are other possibilities, this is the easiest one. Of course in IntelliJ you should be able to run the program directly.
maven-jar-plugin
If you want to ship the application, use maven-jar-plugin to add Main-Class and Class-Path entries to the manifest:
Main-Class: com.example.Main
Class-Path: lib/scala-library-2.9.0-1.jar lib/slf4j-api-1.6.1.jar ...
The following configuration does that and also copies all the dependencies (including Scala runtime library) to target/lib.
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.Main</mainClass>
<addClasspath>true</addClasspath>
<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>lib/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}
</customClasspathLayout>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
</plugin>
Now you can simply run your application by (note the target/lib directory is required):
$ java -jar target/your_app-VERSION.jar
You can ship your application simply by copying your JAR file along with /lib subdirectory.
Also see Exec Maven Plugin and Playing with Scala and Maven.

How to copy dependencies to gae war/WEB-INF/lib

I'm coming from Ant perspective, so pardon me. I realise there are quite a few questions here already on how to maven dependencies, but none of them seem to tell how to do what need to do.
Question 1:
Currently, in conjunction with using maven-war-plugin, when I run mvn war:war, it creates a war folder in target folder.
However, I wish copy all the dependencies' jars to war/WEB-INF/lib set up by google eclipse plugin (with gae enabled, gwt disabled), without overwriting the jars that google eclipse plugin placed there.
I don't wish to setup a war file or war directory. I just need to copy/consolidate all the non-gae jars with the gae jars so that when the project is run as a gae web app, Eclipse would not complain ClassNotFoundException.
Question 2:
When using Ant in Eclipse, I could run Ant targets within Eclipse.
Right now, I have to perform mvn commands from a shell window (which is mutually oblivious to the existence of an Eclipse session). It appears that the only thing that is automatically done is whenever I update dependencies.
Is there a way, or any plugin for eclipse that would allow me to run mvn goals within Eclipse?
Additional info:
mvn dependency:copy-dependencies persists in copying to target/dependency directory, with the following:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/war/WEB-INF/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeArtifactIds>gwt-user,gwt-dev</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
I even tried changing to absolute path
<outputDirectory>
/home/geek/eclipse/workspace/Demo-autoshoppe/holycow
</outputDirectory>
But holycow directory is still empty and mvn still persists in copying to target/dependency directory. My current solution is to softlink target/dependency as war/WEB-INF/lib, which is a very very bad kludge. Why is maven not sensitive to outputDirectory specification? I am using Ubuntu's maven 2.2.
I have the real answer for you, my man.
Use the "default-cli" execution id. Make sure you're using Maven 2.2+. This exec-id applies to command-line executions of the mojo.
<build>
<pluginManagement>
<plugins>
<!-- Copy dependencies to war/WEB-INF/lib for GAE proj compliance. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/war/WEB-INF/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeArtifactIds>gwt-user,gwt-dev</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Cheers.
An associate emailed me this answer which works. Trigger the follow through
mvn build or mvn package
but not directly thro mvn dependency:copy-dependencies.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/war/WEB-INF/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeArtifactIds>gwt-user,gwt-dev</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
Regarding #1: I'd suggest creating your maven project based off this plugin and archetype http://code.google.com/p/maven-gae-plugin/ (see the GWT based example if you are writing a GWT app for GAE).
Regarding #2: Check out m2eclipse plugin for full maven integration in eclipse. It's written by Sonatype (the creators of maven): http://m2eclipse.sonatype.org/