Jetty daemon and useTestClasspath, what am I doing wrong? - maven

Since I'm trying to set an embded container in my maven project I want to it run during the integration test phase. I have two problems with jetty that I can not manage to resolve :
<daemon>true</daemon> doesn't have the expected effect. The server is run but then it locks the build process (in fact it blocks the unit tests). So where am I supposed to place that configuration ?
The <useTestClasspath>true</useTestClasspath> is a mystery for me. I don't want to use the src/main/webapp/WEB-INF/lib to place the postgresql jar (which is called by jetty for the datasource (postegresql-driver)) because it would be embeded in the application and I don't want it to be in the war (client side). So I want to use <useTestClasspath>true</useTestClasspath> but when I place postgresql in the src/test/resources it doesn't find/recognize it. So, how am I supposed to use that property ?
Here is the complete plugin configuration :
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.9</version>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war</goal>
</goals>
<configuration>
<useTestClasspath>true</useTestClasspath>
<daemon>true</daemon>
<contextPath>agepro-prototype</contextPath>
<webApp>
${project.build.directory}/agepro-prototype.war
</webApp>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9091</port>
</connector>
</connectors>
<stopPort>9092</stopPort>
<stopKey>test</stopKey>
</configuration>
</plugin>
Thanks in advance for the help you could provide me. I must apologize for my grammar, because my english is quite bad.
Regards,
Depado

Not sure about your first question, but as for your second, specify the postgresql jar as provided scope in your main dependency block (this will prevent it from being bundled in the war), and add an additional dependency block in the jetty plugin definition (with compile scope), which will make the postgresql jar available at jetty runtime:
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.9</version>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war</goal>
</goals>
<configuration>
<daemon>true</daemon>
<contextPath>agepro-prototype</contextPath>
<webApp>
${project.build.directory}/agepro-prototype.war
</webApp>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9091</port>
</connector>
</connectors>
<stopPort>9092</stopPort>
<stopKey>test</stopKey>
</configuration>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
<scope>provided</scope>
</dependency>
</dependencies>
I don't want to use the src/main/webapp/WEB-INF/lib to place the postgresql jar (which is called by jetty for the datasource (postegresql-driver)) because it would be embeded in the application and I don't want it to be in the war (client side). So I want to use true but when I place postgresql in the src/test/resources it doesn't find/recognize it
You shouldn't be placing jars in any folders (src/main/resources or src/main/webapp/WEB-INF/classes), they should all be defined as dependencies in your pom.
I also imagine the useTestClasspath is being ignored when you define the webApp configuration element, as the it's using the packaged war, which will not contain your test resources / classes

useTestClasspath/useTestScope are only available for jetty:run
This is why it doesn't work with jetty:run-war and other jetty:goals

Related

Checkstyle checkstyle-checker.xml gets overriden when multiple configs are used

I need to separate the checkstyle plugin configs for production and test source code.
I managed to do it (see the config bellow), but there is 'checkstyle-checker.xml' file which is always overriden and stays in the root of the target directory.
Is there a way to move it to /target/checkstyle directory?
Is there a way to separate it between prod and test source code?
<plugin>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven-checkstyle-plugin.version}</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<failOnViolation>${maven-checkstyle-plugin.failOnViolation}</failOnViolation>
<logViolationsToConsole>${maven-checkstyle-plugin.logViolationsToConsole}</logViolationsToConsole>
<violationSeverity>warning</violationSeverity>
<linkXRef>true</linkXRef>
<skip>${maven-checkstyle-plugin.skip}</skip>
</configuration>
<executions>
<execution>
<id>checkstyle-validate</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<sourceDirectories>${project.build.sourceDirectory}</sourceDirectories>
<includeTestSourceDirectory>false</includeTestSourceDirectory>
<configLocation>/checkstyle/checkstyle.xml</configLocation>
<outputFile>${project.build.directory}/checkstyle/checkstyle-result.xml</outputFile>
<cacheFile>${project.build.directory}/checkstyle/checkstyle-cache</cacheFile>
</configuration>
</execution>
<execution>
<id>checkstyle-validate-test</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<sourceDirectories/>
<testSourceDirectories>${project.build.testSourceDirectory}</testSourceDirectories>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<configLocation>/checkstyle/checkstyle-test.xml</configLocation>
<outputFile>${project.build.directory}/checkstyle/checkstyle-result-test.xml</outputFile>
<cacheFile>${project.build.directory}/checkstyle/checkstyle-cache-test</cacheFile>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${maven-checkstyle-plugin.checkstyle.rules.version}</version>
</dependency>
</dependencies>
</plugin>
This file appears to be generated by maven-checkstyle-plugin as seen at https://github.com/apache/maven-checkstyle-plugin/blob/6d229a74b4a7eb2efc5fce287d932f6b5c250647/src/main/java/org/apache/maven/plugins/checkstyle/exec/DefaultCheckstyleExecutor.java#L733
Looking at https://github.com/apache/maven-checkstyle-plugin/blob/6d229a74b4a7eb2efc5fce287d932f6b5c250647/src/main/java/org/apache/maven/plugins/checkstyle/exec/DefaultCheckstyleExecutor.java#L765 it appears there is no way to override it's location and it will always go into the project's build directory.
I am not sure what purpose this file plays in the work the plugin does as this is not part of the base checkstyle library.

How to run junit tests when jetty is configured to start/end at pre/post-integration-test?

I want to run junit tests, while jetty is running , how to correctly configure this?
Now it seems that the tests run before jetty is started so they can't get a connection and the project build fails.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.7.1</version>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.10.v20130312</version>
<configuration>
<connectors>
<connector implementation="org.eclipse.jetty.server.bio.SocketConnector">
<port>9090</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<jvmArgs>-Xms1024m -Xmx2048m</jvmArgs>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.16</version>
</dependency>
</dependencies>
</plugin>
Please help me with this! How to ensure that the junit tests (during the maven build) run in the integration-phase?
The Maven test phase is for running 'Unit' tests, that is, small tests that check that the smallest possible functionality or 'Unit' is functioning as expected.
These are run by the Maven Surefire plugin. You can find more info on this here:
http://maven.apache.org/surefire/maven-surefire-plugin/
What you have are integration tests. That is, they are broader tests that check that different parts of your code integrates together. This is run by the Maven Failsafe plugin which you can find more details on here:
http://maven.apache.org/surefire/maven-failsafe-plugin/
What you need to do is identify your integration tests as integration tests to the Failsafe plugin. By default, it picks up classes named **/IT*.java, **/*IT.java, and **/*ITCase.java. The naming conventions between the plugins is mutually exclusive by default so your tests should only run in one or the other.
If you only have integration tests then you can configure Surefire to run during the integration-test phase instead. Instructions on doing that here:
http://docs.codehaus.org/pages/viewpage.action?pageId=62120

How do I set up datasource for JBoss plugin using Maven to run tests with Arquillian?

I'm trying to make work arquillian tests with jboss managed server and IBM DB2 database.
For now I'm stuck on creating datasource. Since JBoss is unpacked on each run, I'm trying to add driver and datasource configuration into pom.xml in order to Maven take care of creating proper configurations on JBoss and resulting section looks like this:
<profile>
<id>arquillian-jboss-managed</id>
<build>
<plugins>
<!-- JBoss server itself -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-dist</artifactId>
<version>7.1.1.Final</version>
<type>zip</type>
<overWrite>false</overWrite>
<outputDirectory>target</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<!-- adding datasource -->
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<executions>
<execution>
<id>deploy-driver</id>
<phase>process-test-classes</phase>
<!-- groupId and artifactId aren't global, I've got jar on defined path -->
<configuration>
<groupId>db2</groupId>
<artifactId>db2cc</artifactId>
<name>db2jcc4.jar</name>
</configuration>
<goals>
<goal>deploy-artifact</goal>
</goals>
</execution>
<execution>
<id>add-datasource</id>
<phase>process-test-resources</phase>
<configuration>
<address>subsystem=datasources,data-source=MyDataSource</address>
<properties>
<connection-url>jdbc:db2://host:port/database</connection-url>
<jndi-name>MyDataSource</jndi-name>
<enabled>true</enabled>
<pool-name>MyDataSource</pool-name>
<user-name>db2inst1</user-name>
<password>pass</password>
<driver-name>db2jcc4.jar</driver-name>
</properties>
</configuration>
<goals>
<goal>add-resource</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Yet I've got an error:
Failed to execute goal
org.jboss.as.plugins:jboss-as-maven-plugin:7.4.Final:add-resource
(add-datasource) on project testrunner: Could not execute goal
add-resource. Reason: I/O Error could not execute operation '{
"address" => [], "operation" => "read-attribute", "name" =>
"launch-type" }': java.net.ConnectException: JBAS012144: Could not
connect to remote://localhost:9999. The connection timed out
I guess the problem is JBoss isn't started at the moment Maven tries to apply configuration parameters or simply doesn't listen to required port.
Any help is greatly appreciated
Thanks in advance
Fixing this problem was as simple as adding start and shutdown goals to jboss-as-maven-plugin executions before and after other configuration:
<execution>
<id>start-server</id>
<phase>process-test-classes</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<!-- copying driver and datasource here -->
<execution>
<id>shutdown-server</id>
<phase>process-test-classes</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
Also this start goal downloads it's own JBoss instance if one is not provided. So this part is not needed any more:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<!-- skipped -->
</plugin>

JMS integration tests with Maven and Glassfish

I'm developing an app requiring MDB, running over Glassfish 3.1. I've managed already to run unit/integration tests of simple EJBs using embedded container with no problem. Now I'm trying to create integration tests for my MDBs.
1) I tried launching the Glassfish embedded server programatically, but it does not support creation of JMS queues.
2) I run a Glassfish server from the Maven plugin.
Now I can create queues, and deploy my MDBs, no problem at all. Now, I just can't figure out a way of running JUnit.
- When I create an InitialContext, it times-out when accessing the local server. I have no ways of accessing my beans.
I found a workaround, but it's not serving my needs perfectly:
In my test sources, I created a simple Singleton #Startup bean. In the #PostConstruct method, I call the unit test classes I want to achieve. In order for this bean to be deployed, I have a special special maven build rule that packages some of my tests files in the EJB jar. Deploying this special jar results in my tests being launch. To make it clear, here's an extract of my Maven file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/test-ejb</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/classes</directory>
</resource>
<resource>
<directory>${project.build.directory}/test-classes</directory>
<includes>
<include>**/*TestTrigger.class</include>
<include>**/*IntegrationTest.class</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>ejb</goal>
</goals>
<configuration>
<classifier>TEST</classifier>
<outputDirectory>${project.build.directory}/test-ejb</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.glassfish</groupId>
<artifactId>maven-embedded-glassfish-plugin</artifactId>
<version>${glassfish.version}</version>
<configuration>
<goalPrefix>glassfish</goalPrefix>
<app>target/${project.build.finalName}-TEST.jar</app>
<port>8080</port>
<name>MyApp</name>
<serverID>embedded</serverID>
</configuration>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>admin</id>
<phase>pre-integration-test</phase>
<goals>
<goal>admin</goal>
</goals>
<configuration>
<commands>
<param>create-jms-resource --restype javax.jms.QueueConnectionFactory jms/TestQueueConnectionFactory</param>
<param>create-jms-resource --restype javax.jms.Queue --property imqDestinationName=ceQueue jms/ceQueue</param>
</commands>
</configuration>
</execution>
<execution>
<id>deploy</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>undeploy</goal>
<goal>stop</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
Now, is there any way my IntegrationTest can be launched using surfire, in order to produce a proper report and fail build if test don't pass? Not to mention Cobertura.
Thank you for your help.
I didn't solve my problem so to say. What I had to do is upgrade to GlassFish 3.1.1. This version supports JMS in embedded mode. Therefore I can run a server programatically and deploy the queues I want using the admin command runner.
I just lost a bit of time trying to name my connection factory jms/connectionFactory where the jms/ prefix was unnecessary and was failing all lookups.
I also had to include a thread sleep in all my unit tests or else the server would close before tests are over.

Is Surefire (Maven) able to run several executions with different versions of dependencies? If yes, how?

I wonder if it's possible to have the Maven Surefire plugin running several times (several executions) with different different versions of dependencies ?
This could be convenient for example to ensure that your code is still compatible with previous versions of project's dependencies.
I manage at least to run 2 executions of surefire :
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>test-default-deps</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
</configuration>
</execution>
<execution>
<id>test-anotherversion-deps</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<reportsDirectory>${project.build.directory}/surefire-reports-anotherversion-deps</reportsDirectory>
<dependencies>
<!-- Version different of the default for the project -->
<dependency>
<groupId>com.dep.groupid</groupId>
<artifactId>dep-artifact</artifactId>
<version>anotherversion</version>
</dependency>
</dependencies>
</configuration>
</execution>
</executions>
</plugin>
But this different version is not taken into account during the 2nd execution.
Am I trying to do something unfeasible or am I doing in the wrong way ?
Is there another plugin that could be helpful for this purpose ?

Resources