I am trying to exclude some files from a Maven war build by using profiles, and just cannot figure out to get that done.
I have created two profiles in pom.xml, created an eclipse run configuration for each, and can invoke them separately. But the resultant builds both have all the files in the eclipse project, no matter what I try.
I have simplified it to A and B, profile A is the 'default' profile.
<profile>
<id>profile-a</id>
<build>
<finalName>builda</finalName>
<plugins>
<plugin>
<artifactId>cloud</artifactId>
<version>11.0.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<classifier>profile-a</classifier>
<includes>
<include>**/*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
And profile B is the one where I want to remove some files, and have tried all sorts of ways, and have reduced it to not including anything, and all the possible exclude methods are shown also.
<profile>
<id>profile-B</id>
<build>
<finalName>buildb</finalName>
<plugins>
<plugin>
<artifactId>cloud</artifactId>
<version>11.0.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<classifier>cloud-service</classifier>
<excludes>
<exclude>**/*.txt</exclude>
</excludes>
<packagingExcludes>**/*.txt</packagingExcludes>
<warSourceExcludes>**/*.txt</warSourceExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
But both builda.war and buildb.war are identical - I compared the war files with Beyond Compare, and the only difference is the timestamp in the Manifest. I also tried removing a .jar file just to see if it was something about .txt files, but no difference at all.
So I'm thinking that the build information is coming from my eclipse project not from pom.xml. Or I completely missing something - I am still a maven newbie, so that is entirely possible.
But how to get this done?
First of all I feel that I should point out that there seem to be multiple problems with the POM you've provided. I think you should take some time to read through the Maven POM Reference, Maven Build Lifecycle, and related documentation.
Based on what you provided I think you're trying to add a classifier to your artifact based on the profile that was active during the build. I preserved this functionality and corrected the other aspects of the POM as best I could, but I made a few assumptions.
Here is the modified POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>web.example</groupId>
<artifactId>exampleWar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>profile-a</id>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<classifier>profile-a</classifier>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>profile-b</id>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<classifier>profile-b</classifier>
<packagingExcludes>%regex[.*\.txt]</packagingExcludes>
<warSourceExcludes>%regex[.*\.txt]</warSourceExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Related
Assume we have maven multimodule project "Foo":
Foo
|-web-module-war
|-dependency-jar
There are two profiles defined for moduleC:
<profile>
<id>poll-some-external-service</id>
<properties>
<dependency-jar.poll.configured>true</dependency-jar.poll.configured>
</properties>
</profile>
<profile>
<id>produce-some-product</id>
<properties>
<dependency-jar.poll.configured>false</dependency-jar.poll.configured>
</properties>
</profile>
Now we run two builds:
mvn clean package -P poll-some-external-service
mvn clean package -P produce-some-product
First build produce following artifacts:
web-module-war-1.0.0-poll.war
dependency-jar-1.0.0-poll.war
Second build produce following artifacts:
web-module-war-1.0.0-produce.war
dependency-jar-1.0.0-produce.war
This means that war file contains web application which works in a different way based on selected profile.
Naming is based on the following configuration in the parent pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<jarName>${project.build.finalName}${foo.build.info}</jarName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>${project.build.finalName}${foo.build.info}</warName>
</configuration>
</plugin>
How can I deploy these artifacts into Nexus? -poll/-produce part is stripped during deployment. This means we have two different applications of the same version but we can deploy only one of them
Thanks
Instead of changing the name use a classifier
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<classifier>poll</classifier>
</configuration>
</plugin>
Your profile for the pom should look similar to the following example. Note that you have to change the dependencies by using the profile too.
<profile>
<id>poll</id>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<classifier>poll</classifier>
</configuration>
</plugin>
</plugins>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>dependency-jar</artifactId>
<classifier>poll</classifier>
</dependency>
</dependencies>
</build>
</profile>
I have a Maven project and I have two profiles (Dev and integraiton). I use integration-profile to call two plugins, WAR and EAR maven plugin.
My question : how can I use the plugin in the pom.xml dependencies when integration-profile is set in the Maven goal (I don't Like to have a plugins call in the profile)?
<profiles>
<profile>
<id>integration-profile</id>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>WEB-INF/jboss-web.xml</packagingExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<packagingExcludes>conf/jboss-roles-mapping.conf</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
I want to be able to config values in some property file that lately is being used by Java tests.
Here is my pom.xml
<profiles>
<profile>
<id>env-dev</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<target.env>http://myurl.com</target.env>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/test/java</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
Properties file
targetEnv=${target.env}
When I run mvn install -Denv=dev I can see that in target folder, the properties file is getting the right value but during the tests the placeholder is being used where I want to use the real value when tests are running.
What am I doing wrong?
Thanks
What am I doing wrong?
First of all, don't use profiles to switch between environments. Especially a unit-test should never be aware of any environment settings. Please remove it in this case.
For filtering test resources you should use the <testResources/>-tag instead.
How do I set in the pom to not compile tests in Maven? I've tried:
<properties>
<skipTests>true</skipTests>
</properties>
but in that case, Maven compile the tests but don't run them. I need Maven don't compile my tests.
You have to define maven.test.skip to true.
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html
In my case a solution was to put tests in a profile (e.g. runTests), so when I want to run these tests, I add the parameter -PrunTests. Thanks for the replies.
Configure maven-compiler-plugin to skip the compilation.
Once again, I do not recommend it.
<project>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<skip>${maven.test.skip}</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
If you are using the surefire-plugin for executing tests, you can configure it to skip them based on a naming pattern:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<configuration>
<includes>
<include>%regex[.*[Cat|Dog].*Test.*]</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
This, however, requires the tests file names to conform to the desired pattern(s). At work we are using this approach, and have our tests end with ..UnitTest or ..IntegrationTest, so that we can easily turn each of them off by modifying the regex in the corresponding build profile.
Take a look at Apache's documentation on the surefire plugin. You may find something more useful or better suited for your case.
Is it possible to isolate integration tests from unit tests within same module?
I created simple pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>prj</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/integration/**/*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/integration/**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
however with mvn -Pintegration test it doesn't invoke anything. If I comment out excludes section in main build - then it starts to execute tests, but without profile as well.
instead of:
<exclude>*/integration/**/*.java</exclude>
try:
<include>*/unit/**.java</include>
then in the integration profile do
<includes>
<exclude>**/unit/**/*.java</exclude>
<include>**/integration/**/*.java</include>
</includes>
you may have to play with getting the includes/excludes exactly right, but that's the general idea.