I have multiple java packages in my java project. Is it possible to run a single package on eclipse using maven. I want to do it with the project level pom. I dont want to create POMs for every package.
Based on your comment, not sure if this is what you are looking for but I use a configuration variable to run test cases for a particular package. You have to add a plugin to the POM to achieve this.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version>
<configuration>
<includes>
<include>**/${testGroup}/*Test.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
Now if you want to run the test cases for lets say package org/myPackage you would use a command like this.
mvn test -DtestGroup=org/myPackage
Related
I'm trying to execute some test cases in an very big applications. There are 100s of error test cases and some of them
requires specific property files which won't be available in development environment.
are outdated, but can't be simply removed
Is there anyway in which I can provide an XML or any other input which can list all the test classes to ignore?
I'm using maven with surefire plugin for executing test cases.
PS
I'm aware of #ignore annotation which can be used to ignore test cases or test classes. I don't want to use this because it requires changing each class which I want to ignore.
What I want is a single configuration file where I can mention all the classes to ignore.
There are 2 options:
1.To skip running the tests for a particular project, you can set skipTests to true.
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
2) Also you can skip the tests via command line by executing the following command:
mvn install -DskipTests
You can potentially create a TestSuite that includes all the tests you do want to run, and then specify to Maven that it should use that particular Test Suite with:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<includes>
<include>TestSuite.java</include>
</includes>
</configuration>
</plugin>
with TestSuite.java being the name of the suite you wish to run.
You could potentially have two and just switch that setting in your pom.xmlwhen you wish to target only certain classes.
This setting affects what tests are executed when you run mvn test.
I am completely new to maven and testng. I am using maven as build tool, and testng as my testing framework. I am not following the standard maven project structure. Now I want my pom.xml to execute the test cases in my project. The question is, how pom.xml knows what are the test cases to consider for execution?
If you keep this in a single place you need to set the testClassesDirectory argument of the maven-surefire-plugin configuration:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<testClassesDirectory>path/to/compiled test classes</testClassesDirectory>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
All of this is well documented in the Maven Surefire Plugin Documentation
I want to compile only selected files or directories (including subdirectories) within source directory. I was pretty sure I can do this using <includes> of maven-compiler-plugin's configuration, but it seems to not work as I expect since it still compiles all classes into target/classes. What is really strange, Maven output suggest that the setting actually does its work, because with:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<includes>
<include>com/example/dao/bean/*.java</include>
</includes>
</configuration>
</plugin>
I have:
[INFO] Compiling 1 source file to c:\Projects\test\target\classes
but with no compiler's configuration I have:
[INFO] Compiling 14 source file to c:\Projects\test\target\classes
In both cases however, all 14 classes are compiled into target/classes as I mentioned. Can you explain that or suggest another solution to compile only selected files?
Simple app with 3 classes.
com/company/Obj1.java
com/company/Obj2.java
com/company/inner/Obj3.java
build in pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<includes>
<include>com/company/inner/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
result: 1 class is compiled.
And any combination of includes is working well
or you mean something else?
I have faced a similar situation. We needed to hot swap only modified files to our remote docker container in order to improve changes-deploy time. This is how we solved it.
Add includes option in build plugin with command line argument.
Note since we wanted to add multiple files, so we have used includes and not include
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<source>1.8</source>
<target>1.8</target>
<includes>${changed.classes}</includes>
</configuration>
</plugin>
Now run compile phase with argument, example:
mvn compile -Dchanged.classes=com/demo/ClassA.java,com/demo/ClassB.java,com/demo2/*
maven-compiler-plugin using Ant-like inclusion/exclusion notation.
You can see examples in Ant documentation Ant FileSet Type
If you are want include only files from one directory, you need write it like you did:
<include>com/example/dao/bean/*.java</include>
To include also subdirectories from path, it will be:
<include>com/example/dao/bean/**/*.java</include>
I had no difficulty including or excluding files for compilation with maven compiler plugin 2.5.1. Here is the dummy project I used for the purpose. Perhaps the include pattern that you use is different.
What I do is:
Create a new project with IntelliJ with Maven module.
Add Framework support to this project and pick: JSF.
Go to pom.xml and add: packaging: war.
And from Maven window in IntelliJ I click: Clean Install.
Well build fails, because maven is looking for a webapp directory instead of a directory called web. For building the project.
So, if I rename the folder web to webapp build goes fine.
However, I want to learn more about IntelliJ and maven, so I want to force maven to use the folder web. How can I properly do this
Using the command line? I mean without invvolving IntelliJ at all?
Using Intellij?
Regards.
You can configure this in the pom.xml file for your project.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
</configuration>
</plugin>
</plugins>
You can find the documentation here
If IntelliJ behaves as expected, it should pick up this new configuration.
Have a look at this post, which explains how to change the default webapp directory:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
I'm currently working on a Maven-backed project. I've chosen TestNg to implement my unitary tests. In order to run my unitary tests at each Maven build, I have added the maven-surefire-plugin to my pom.xml :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<!-- Configuring the test suites to execute -->
<suiteXmlFiles>
<suiteXmlFile>testsuite-persistence-layer.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
Moreover, I want to specify the tests to be executed using TestNg's TestSuiteXmlFile. For instance, in my pom.xml, I've configured the surefire plugin so that it will execute the tests defined in the xml file named "testsuite-persistence-layer.xml".
The problem is that by default, the surefire plugin seems to be looking for this xml file at the root of my project. How can I specify the directory in which the surefire plugin should look for the TestSuite xml files?
According to the TestNg documenation, this could be specified through the "maven.testng.suitexml.dir" property but the Surefire plugin does not seem to take it into account.
I am not sure if I understand your problem. You can easily specify the exact location of xml file, both as relative and fully qualified path.
<suiteXmlFile>c:/some/dir/testsuite-persistence-layer.xml</suiteXmlFile>
or
<suiteXmlFile>src/test/java/com/something/project/testsuite-persistence-layer.xml</suiteXmlFile>
But that's too easy, so I am guessing you are looking for a way to parametrize the directory where xmls are located. The quick solution that comes to my mind would be
<suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile>
Now you can run
mvn test -DxmlPath=c:/some/path
Of course xmlPath is just made-up name, you can use any other variable name you wish.
If you don't want to pass the path as an argument from command line you can specify the value of xmlPath variable in properties section of your POM. Properties is one of the main sections located just under < project > branch.
<project ... >
...
<properties>
<xmlPath>c:/some/path</xmlPath>
</properties>
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${xmlPath}/testSuite.xml</suiteXmlFile>
</suiteXmlFiles>
...
</configuration>
</plugin>
...
</plugins>
...
</build>
...
</project>