Exclude maven dependency for tests - maven

I have a dependency that is needed for a compilation and runtime but I want to exclude it when running tests. Is this possible? Maybe, by setting up a profile? But how do I deactivate it only for test lifecycle phase?

You could (re)configure the classpath during the test phase thanks to the maven surefire plugin. You can add classpath elements or exclude dependencies.
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>path/to/additional/resources</additionalClasspathElement>
<additionalClasspathElement>path/to/additional/jar</additionalClasspathElement>
</additionalClasspathElements>
<classpathDependencyExcludes>
<classpathDependencyExclude>org.apache.commons:commons-email</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
As noted by #jFrenetic you could do the same with maven-failsafe-plugin.

Related

PluginVersionResolutionException in Maven project when I set packaging to bundle

In my Maven project when I change the packaging type from 'jar' to 'bundle' most of my plugins (compiler, deploy, install, resources, surefire) lose their versions. Why is this?
My pom.xml is below:
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.ui_4.4.35_patch</artifactId>
<version>1.0.0</version>
<packaging>bundle</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>4.2.1</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Export-Package>web.admin.*</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
There are two possible approaches. The one I use for my own projects is to keep the bundle packaging and add the maven plugin versions to a pluginManagement section in the pom.xml. For example:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<!-- Add you current 'plugins' section here. -->
</build>
In addition to the version, you can also add configuration to each plugin. If your project has a parent pom, it would be natural to add the pluginManagement section there instead of in your bundle module.
Alternatively, as suggested by #khmarbaise, you can use jar packaging and just use the maven-bundle-plugin to generate the manifest. That approach is described in the plugin documentation page.

What does exactly mean to have the two goal sections (integration-test and verify) inside de failsafe plugin in maven?

<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<executions>
<execution>
<goals>
**<goal>integration-test</goal>
<goal>verify</goal>**
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
I'm new with Maven and I don't understand what means to have both goals declared inside the plugin in the pom.xml. Thank you.
From plugin Documentation
Goals Overview
The Failsafe Plugin has only two goals:
failsafe:integration-test runs the integration tests of an application.
failsafe:verify verifies that the integration tests of an application passed.

How to run specific profile ID / suites (to run specific TestNG.xml) in Gradle?

I am new to Gradle, but I know in Maven we can run specific profile.
In my case, I have 2 TestNG.xml files and in Maven POM.xml I can write like this
<profiles>
<profile>
<id>FirstRegression</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng1.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>SecondRegression</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng2.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
In command line , we can choose which profile (which TestNG.xml file) we want to run.
mvn test -PFirstRegression will execute our testng1.xml file only, meanwhile
mvn test -pSecondRegresion will execute our testng2.xml file only.
How can we do this in build.gradle file ? so we can choose which profile to run in gradle.
I can put like this in build.gradle
plugins {
id 'java'
}
test {
useTestNG() {
suites 'testng1.xml'
suites 'testng2.xml'
}
}
But when I run gradle clean build , it will run both of them.
Is there anyway we can say gradle clean build --"please run testng2.xml only" ? Thank You.

how to create deployable war in spring using maven

Creating war in spring what are the dependency needed ?
1) maven compiler and maven war plugins are required ?
2) How to add class path reference to war package ?
Create a Maven Spring project and in pom.xml change the packaging to war:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>Maven Spring Project</artifactId>
<packaging>war</packaging>
And just build the project using Maven by executing goal : "clean install". It will automatically create war file in target folder of Maven project.
You can try this in your pom.xml
<build>
<finalName>projectname</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin-version}</version>
<configuration>
<source>${jdk-version}</source>
<target>${jdk-version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin-version}</version>
<configuration>
<warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<argLine>-XX:-UseSplitVerifier</argLine>
</configuration>
</plugin>
</plugins>
</build>
and to make war run this package tomcat7:run goal from eclipse

run jasmine.js test as part of Maven 'test' goal

I have the following jasmine tests configuration in my project pom.xml:
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>test</goal>
<goal>jasmine</goal>
</goals>
</execution>
</executions>
<configuration>
<jsSrcDir>${basedir}/src/main/js</jsSrcDir>
<sourceIncludes>
<include>myCode.js</include>
</sourceIncludes>
<jsTestSrcDir>${basedir}src/test/js/specs</jsTestSrcDir>
<specIncludes>
<include>mySpec.js</include>
</specIncludes>
</configuration>
</plugin>
...
with this configuraton I can run jsamine:test and the test wil run.
I want to run the tests as part of the goal test, but Maven won't run them.
I even tried removing the line <goal>jasmine</goal> but Maven still won't run the jasmine tests.
What am I doing wrong?
You must also add the execution of the plugin like this into you project:
<project>
<build>
<plugins>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
cause the one you've defined in pluginManagement does not really execute the plugin.

Resources