Whenever I run mvn test in my Maven project, only the test files ending in Test are executed.
However, all my test files in the current project are ending in Spec and I don't want to change their names.
So how can I configure maven to execute files ending in Spec as well?
By default, the Maven Surefire Plugin will automatically include files respecting these patterns:
**/Test*.java
**/*Test.java
**/*TestCase.java
To add more patterns, and specifically yours, just configure the plugin in the following way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>**/*Spec.java</include>
</includes>
</configuration>
</plugin>
See also the complete documentation about this kind of configuration.
Related
I have a maven project which has multiple feature files
Each file has a tag, some file does not any tag
I am trying to invoke
mvn verify -Dit.test=MyRunner -Dtags="#mytag" -Dcucumber.options="--tags '#mytag'"
But the execution ignores the command line and chooses all the feature files
unfortunately I have to live serenity 2.0.x and cucumber 4.2.0
I have identified my issue that I am using verify for test
And I am using maven-failsafe-plugin it has configuration and systemPropertyVariables, I have introduced cucumber.options there as shown below
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<cucumber.options>${cucumber.options}</cucumber.options>
</systemPropertyVariables>
</configuration>
</plugin>
after doing this it started working
I am tasked with converting a java project which is created with ant to maven.
This is how the project is set up.
All the sources are stored in src directory.
ant's compiling target is to compile the entire src directory.
ant's packaging target has several sub-targets.
Each target has different jars which has include or exclude directories.
This is the approach that I took.
Find out all dependencies. Store them in DependencyManagement section of parent pom
Create a module and copy entire src directory.
compiled it.
Tried to create separate modules for different jar files.
Problem: the files are in-separable. Most of the files are depending on other files. I tried separating them. It results in creating cyclic dependencies. Hence, this step failed.
Use different profiles and maven-jar-plugin to include or exclude packages.
Question 1 when I tried this mvn install -P profile1,profile2, target has only jar file for profile2. They both have maven-jar-plugin and each has different finalName.
<profile>
<id>profile1</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<includes>
<include>**/src/.../profile1/**</include>
<finalName>profile1-lib</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Is there a different command or am I doing something wrong?
Question 2 one of the lib has several image files in it.
The above approach does not copy the image files in the result jar.
I understand maven wants all resources in resources directory. I will move the images, but for now I am trying to include them in the jar.
I added maven-resources-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resource>
<directory>src/main/java/jpl/mipl/mdms/FileService/komodo/ui/savannah/subscription/util/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
When I run mvn install -P profile1, I can see the logs saying Copying # resources.
I can also see them being copied to target/classes. the path that I gave to the plug-in. But they are all copied to that directory.
I want to retain the structure, and the result jar still doesn't have it.
Answer: I have to move them to resources directory. maven is strict.
Anything that I should be doing differently?
Extra Question Am I using the correct plug-ins? Or is there more efficient plug-ins that I should be using?
Thank you.
Answer to Question1 : You have missed the </includes> tag.(Probably because of the stackoverflow formatting!). I am not sure whether you can execute a maven command on two profiles at a time. when you do so, only the second profile gets executed. Try executing the command on each profile separately.
Answer to Question2: You have missed the <resources> opening tag.(Probably because of the stackoverflow formatting!). Moreover, if you want to retain the structure, you can mention the structure too in the <outputDirectory> tag, something like the below:
<outputDirectory>${basedir}/target/classes/src/main/java/jpl/mipl/mdms/FileService/komodo/ui/savannah/subscription/util/resources</outputDirectory>
This may look insane!, but may work if you have few resources directory. If you have more resources, then this may become cumbersome. But anyways, check whether this can be helpful!
I have the following directory structure for my maven project:
|-src
|---itest
|-----java
|-------com
|---------corp
|-----------div
|-------------dept
|---------------prod
|-----------------config
|-------------------integration
|---------------------PersistenceConfig.java
|-----------------test
|-------------------LandingPageInfoTest.java
|-----resources
|-------db
|---------migration
|-----------V1__SeedLandingPageInfo.sql
|-------log4j.properties
|-------persistence.properties
(note: src/i test, not src/test)
I have an appropriate set of build-helper-maven-plugin plugin declarations that tell maven to add src/itest/java and src/itest/resources via the add-test-source and add-test-resource goals.
Everything works great when I run mvn test from the command line.
The problem: when I move my plugin declarations into a separate <profile> named integration my tests refuse to run when I execute mvn test -Pintegration and I'm not sure why...
The convention tends to be use the maven-failsafe-plugin (http://maven.apache.org/surefire/maven-failsafe-plugin/) with configuration to include your integration tests whereever they are, even alongside your unit tests, and they should then be excluded from the unit test phase surefire plugin. As a simple example:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<!-- Include your integration test files/directories/suites here -->
</includes>
</configuration>
</plugin>
And then you can remove the profile you've specified and as mentioned, only unit tests will be executed in the test phase and your integration tests will be executed in the verify phase (included before install/deploy).
Following this convention will let you get rid of all the extra plugin configuration to move files around and point at your itest directory.
I have a maven web application. I am using springsource tool suite and its built in tc fabric server.
Every time I make any changes I have to do mvn clean install and restart the server. Even for the JSP changes.
Is there any way I can just make JSP changes and they are reflected in the browser on refresh like a regular web app (not maven app).
I have searched over internet but no success as yet.
Still No clue about it. It makes the development process extremely slow. I looked into jrebel but its not free and I am not looking for hot deploy of classes but just the hot deploy of JSP's, javascripts, css etc.
I currently use a profile to copy JSPs to my target dir, which I call from Eclipse once i need the JSPs updated.
You could also copy classfiles like this by adding executions.
<profile>
<id>copyJsps</id>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<outputDirectory>${basedir}/target/app/WEB-INF/jsp</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp/WEB-INF/jsp</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Use: mvn resources:copy-resources -PcopyJsps
You can use Eclipse Filesync plugin to achieve this. You can configure plugin to map maven output folder to Application server directory
I know this is not maven way but it works.
Maybe this question will give some more insights.
This is my simple solution (workaround?) while using JBoss on Linux. It should also be valid for Tomcat or any other container that supports exploaded war's.
1. Use mvn war:inplace to create an exploaded war in src/main/webapp.
This will copy classes and lib there. You don't need to repeat this step if you're just changing JSP (or other /webapp/ files).
Alternatively you can create symlinks to classes and lib in src/main/webapp/WEB-INF:
cd src/main/webapp/WEB-INF
ln -s ../../../../target/classes
mvn package
ln -s ../../../../target/*/WEB-INF/lib
2. In the deployment directory create a symlink to src/main/webapp:
cd $DEPLOYMEN_DIR
ln -s $MYAPP_DIR/src/main/webapp myapp.war
This makes changes to JSP and other webapp files instantly available.
If you want to see the changes in your classes then you can trigger a reload of the application only my modifying web.xml. On you can run this script to monitor trigger the restart:
#!/bin/bash
while sleep 1; do
if [[ -n $(find WEB-INF/classes -newer WEB-INF/web.xml -type f) ]];then
date
touch WEB-INF/web.xml
fi
done
Run it from the src/main/webapp/ directory.
Note on JBoss AS 7: Here to trigger a reload you need to create a myapp.war.dodeploy file instead of touching web.xml
You can clean and copy the static files easy with the clean and resources plugins, but it won't work for the JSP files always. If the java source in the JSP files being copied introduces a new dependency you are not going to copy it to the lib folder. In that case, the app will break with a ClassNotFoundException.
Even if it is copied it can still break because the server has to be configured to scan the folder with dependencies and refresh the classpath. And that is the start of hot deployment I believe (details).
Try with Vinay's suggestion also. Judging by his answer it seems that the tc server supports scanning of dependencies by default, and with proper maven build this might be a satisfying solution.
To clean and copy the static files from your source directory to where it is deployed:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>clean-loaded</id>
<phase>compile</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>${path.server.input}</directory>
<followSymlinks>false</followSymlinks>
<useDefaultExcludes>false</useDefaultExcludes>
<includes>
<include>**/*.jsp</include>
<include>**/*.js</include>
<include>**/*.html</include>
<include>**/*.css</include>
<include>**/*.png</include>
<include>**/*.gif</include>
<include>**/*.jpg</include>
<include>**/*.jpeg</include>
</includes>
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-compile-output</id>
<phase>compile</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${path.server.input}</outputDirectory>
<overwrite>true</overwrite>
<includeEmptyDirs>true</includeEmptyDirs>
<filtering>true</filtering>
<resources>
<resource>
<directory>${path.jsp.source}</directory>
<targetPath>${path.element.jsp.deploy}</targetPath>
<includes>
<include>**/*.jsp</include>
</includes>
</resource>
<resource>
<directory>${path.static.source}</directory>
<targetPath>${path.element.static.deploy}</targetPath>
<includes>
<include>**/*.js</include>
<include>**/*.html</include>
<include>**/*.css</include>
<include>**/*.png</include>
<include>**/*.gif</include>
<include>**/*.jpg</include>
<include>**/*.jpeg</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
add this to your properties section:
<path.server.input>ABSOLUTE_PATH_TO_DEPLOYED_WEBAPP_ROOT</path.server.input>
<path.jsp.source>ABSOLUTE_PATH_TO_JSP_SOURCE_ROOT</path.jsp.source>
<path.static.source>ABSOLUTE_PATH_TO_STATIC_SOURCE_ROOT</path.static.source>
<path.element.jsp.deploy>REALTIVE_PATH_TO_JSP_DEPLOY_ROOT</path.element.jsp.deploy>
<path.element.static.deploy>REALTIVE_PATH_TO_STATIC_DEPLOY_ROOT</path.element.static.deploy>
Properties that start with path must be absolute paths or start with a ${project.basedir} or similar. Properties that start with path.element are relative paths meaning that they must not be prepended with a / or start with another property that is an absoulte path. That is so because resources plugin copies in outputDirectory/targetPath (resources:copy-resources,
resource)
In my experience, IDE's usually bind their clean-and-build UI action to the compile phase. Also IDE's usually have a way to map a shell command or maven custom goal to be visible from it's UI menu.
To run with clean-and-build
The plugins are already bound to the compile phase. To ensure that clean plugin will run before resources plugin at the end of the compile phase, place them together at the end of your plugins section. It doesn't matter if a plugin is defined twice, just make sure that when reading the pom from top, the first clean plugin definition comes before the first resources plugin definition.
To run as separate action
Change under the execution tag for both like this:
<id>default-cli</id>
<phase>never</phase>
and now it won't be run in compile phase but by invoking from command line:
mvn clean:clean resources:copy-resources
In this case the placement of plugin definitions in pom is irrelevant, since you are defining their order with command arguments order. If this suits you your IDE most probably has a way to map this command as a custom goal visible from it's UI menu.
In both cases, I recommend backing-up the project folder when running for the first time.
In my code I have set to create some files in ./ directory, because when the software will be deployed in the installation machine, all the configuration files will be created in ./ if it's the first run
Unfortunately when I build the project, and Maven executes tests, all the files are created in the project directory.
I want those files to be created in target/test-run/
How can I do it?
Thanks
If you are using the surefire plugin to execute tests (which is the default), then you can set working directory like this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<configuration>
<workingDirectory>${java.io.tmpdir}</workingDirectory>
</configuration>
</plugin>
</plugins>
</build>
Reference: http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html
To set to the specific directory in the question:
<workingDirectory>${project.build.directory}/test-run</workingDirectory>