Can't run and debug groovy tests under intellij idea - intellij-idea

I try to embed groovy test to java project.
I start with spock examples - https://github.com/spockframework/spock-example
Examples is compile and execute by running maven goal test but if i try to run test under intellij idea (ctrl+F10 under test method) it failure with classpath error.
Error running HelloSpockSpec.length of Spock's and his friends' names:
Class 'HelloSpockSpec' not found in module 'spock-example'
I try to apply advices from IntelliJ + Groovy + Spock but it didn't help.

Don't forget to mark the folder as "Test Sources" in IntelliJ
Then it should work as expected :-)

Intellij can automatically add the groovy source as a source directory based on your pom. Add build-helper-maven-plugin config to your maven pom under plugins specifying ${basedir}/src/test/groovy as a source dir:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-groovy-test-source</id>
<phase>test</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/test/groovy</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

Related

Maven build fails with failsafe plugin

I am trying to build my project and run the test by using junit5 and maven failsafe plugin for integration tests.
If I run my test by using
mvn failsafe:integration-test
it runs all the integration tests.
When I run
maven install
failsafe plugin fails with integration tests with error. But surfire plugin works normally. I receive following error for failsafe plugin:
TestEngine with ID 'junit-jupiter' failed to discover tests
...
...
Caused by: java.lang.NoClassDefFoundError: com/mypackage/service/MyObject
My pom with failsafe plugin looks like:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<useModulePath>false</useModulePath>
<includes>
<include>**/*IT.java</include>
</includes>
<excludes>
<exclude>**/*Test.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
I am using junit jupiter version 5.8.2. I tried to use older versions as it was suggested in other posts but it did not help. Any idea what can be wrong here ?

MAVEN: No plugin found for prefix 'dependency' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo]

[ERROR] No plugin found for prefix 'dependency' in the current project
and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo]
available from the repositories [local
(C:\Users\mdhore.m2\repository), central
(https://repo.maven.apache.org/maven2)] -> [Help 1]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- configure the plugin here -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
according to this you have to provide group and artifact id. I run mvn dependencies:tree like the following and it worked.
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:tree -Dverbose=true
I got this error when I forgot to add this parameter. I have to add to each of my Maven comamnds to autheticate with my Maven remote repository.
-Djava.net.ssl.trustStore=cacerts.jks. If you don't normally add a parameter like this you probably have another issue.
I have also encountered the same issue. I typed the wrong command mvn dependences:resolve, the correct one is mvn dependency:resolve.
I got the same error and in my case I didn't have the POM file in the same directory I was in.
make sure there is no import error anywhere in your code then run mvn dependency:tree the error will be gone. I assume you are using mvn and you have some import issue somewhere in your project.

Maven annotation processing with maven-compiler-plugin

I try to compile my code that contains annotations that generate source code. I use the maven-compiler-plugin and build-helper-maven-plugin. My POM is looking like that:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<generatedSourcesDirectory>${project.build.directory}/generated-sources/apt</generatedSourcesDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/apt</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
When I run mvn compile, ${project.build.directory}/generated-sources/apt is added as a source directory, and the generated sources are generated in the correct directory. But I get compiler errors because of missing references to the generated classes. It's like the generated source directory is not included in the compilation process.
I also try apt-maven-plugin which does not generate anything. And maven-annotation-plugin but it behaves as describe above.
The release 2.0.7-SNAPSHOT of maven-annotation-plugin should solve the problem
fwiw I just downgraded from 3.0 to 2.5.1 and fixed an issue I was seeing with APT processing not being executed after a few runs, using maven so just downgraded one version:
http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.apache.maven.plugins%22%20AND%20a%3A%22maven-compiler-plugin%22
Can you try with the latest version of maven compiler plugin (2.3.2)?
Also build-helper-maven-plugin is not required since it looks like you are using the maven-compiler-plugin itself to generate the sources from the annotation.

Get name of maven pom file

I am running a Maven (2) release build with with: mvn -f release.xml clean deploy
and want to get the currently running pom file name (release.xml) into a property or mojo.
Is it possible?
well you can use the property
${env.MAVEN_CMD_LINE_ARGS}
in your pom or a filtered resource, which will expand to something like
clean install -Paxis -Dmaven.test.skip -f mypom.xml -pl util
however, if you just want the mypom.xml part, you're going to have to do some scripting, which is not supported out of the box in maven. Common solutions are either maven antrun plugin or gmaven (groovy) plugin. Here's a way to do it in gmaven:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
System.out.println(
System
.getenv("MAVEN_CMD_LINE_ARGS")
.replaceFirst( /.*\-f\s+(\S+).*/ , 'POM File: $1')
);
</source>
</configuration>
</execution>
</executions>
</plugin>
Edit: as you want it in a property, either use System.setProperty or write directly to project.properties

How to wrap an Ant build with Maven?

We use maven for our large-ish product. All of our artifacts are deployed to a shared archiva repository using the maven deploy goal. I am now integrating a third party product that has an ant build. I know how to call ant targets from maven using the antrun plugin, but I'm not sure how to setup the pom in this instance. I don't want maven to actually generate an artifact, but I do want it to pull the artifact that was built by ant when the maven deploy goal is run.
I am planning on having the pom adjacent to build.xml. The pom will use the antrun plugin in the package goal to call the ant target at the appropriate time to build the .war artifact.
Questions:
a) I am creating a .war file but it is created via ant, not Maven, so having a war packaging type in the pom doesn't make sense. What should my packaging type be?
b) How do I cause maven to pull the artifact from my ant output directory for the deploy goal?
c) If there are no good answers to A and B, then are there ant tasks that replicate the maven deploy functionality for getting my .war artifact into the shared repository?
You can use the maven-antrun-plugin to invoke the ant build. Then use the build-helper-maven-plugin to attach the jar produced by ant to the project. The attached artifact will be installed/deployed alongside the pom.
If you specify your project with packaging pom, Maven will not conflict with the ant build.
In the example below, the ant build.xml is assumed to be in src/main/ant, have a compile goal, and output to ant-output.jar.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<tasks>
<ant antfile="src/main/ant/build.xml" target="compile"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-jar</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/ant-output.jar</file>
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
You can actually wrap an ANT project with Maven by using multiple ant run goals as I wrote in a different question. Assuming your existing ant project has clean and build tasks, this might be a useful way of wrapping the project so you can use maven goals and have it map to existing ant code.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>install-library</id>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>x.x</groupId>
<artifactId>ant-out-atifacts</artifactId>
<version>${project.version}</version>
<file>ant-output.jar</file>
<packaging>zip</packaging>
</configuration>
</execution>
</executions>
</plugin>
Refer this: Why you should use the Maven Ant Tasks instead of Maven or Ivy
And specifically, how to invoke a Maven goal from Ant can be found in this example:
http://code.google.com/p/perfbench/source/browse/trunk/perfbench/grails-gorm/build.xml
With the information above you should be able to achieve what you need. Let me know if you have any questions.