In our web project there are a couple of executions of maven-war-plugin that generates war files for each environment. I need to add a new dependency to one of them. So far we have been doing this using overlays but this approach does not operate on drpendencies and as a result we often end up with the same library added twice in different versions. Is there any way to create multiple wars per one build(not multiple builds with different profiles) with custom dependencies?
Hy Tomasz W
I have created a sample pom.xml file
With the maven-war-plugin alone you cannot add a dependency for only one execution. but in my working example i separated the war plugin output into separate folders and then with the dependency plugin i copied the depencencies required only for "dev" war into the specific WEB-INF/lib folder...
<?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>
<groupId>com.github.stefanheimberg.stackoverflow-34083152</groupId>
<artifactId>stackoverflow-34083152</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<!-- maven default properties -->
<encoding>UTF-8</encoding>
<maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
<maven.compiler.showWarnings>true</maven.compiler.showWarnings>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.5.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>add-dev-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<excludeGroupIds>org.eclipse.persistence</excludeGroupIds>
<excludeArtifactIds>eclipselink</excludeArtifactIds>
<outputDirectory>${project.build.directory}/${project.build.finalName}-dev/WEB-INF/lib</outputDirectory>
</configuration>
</execution>
<execution>
<id>add-test-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<excludeGroupIds>org.hibernate</excludeGroupIds>
<excludeArtifactIds>hibernate-core</excludeArtifactIds>
<outputDirectory>${project.build.directory}/${project.build.finalName}-test/WEB-INF/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>create-dev-package</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<warName>${project.build.finalName}-dev</warName>
<webappDirectory>${project.build.directory}/${project.build.finalName}-dev</webappDirectory>
</configuration>
</execution>
<execution>
<id>create-test-package</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<warName>${project.build.finalName}-test</warName>
<webappDirectory>${project.build.directory}/${project.build.finalName}-test</webappDirectory>
</configuration>
</execution>
</executions>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
file output:
$ ls -l target/*/WEB-INF/lib/*
-rw-r--r-- 1 heimbergs 1049089 434678 Okt 1 15:03 target/stackoverflow-34083152-1.0-SNAPSHOT/WEB-INF/lib/commons-lang3-3.4.jar
-rw-r--r-- 1 heimbergs 1049089 445288 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/antlr-2.7.7.jar
-rw-r--r-- 1 heimbergs 1049089 434678 Okt 1 15:03 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/commons-lang3-3.4.jar
-rw-r--r-- 1 heimbergs 1049089 313898 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/dom4j-1.6.1.jar
-rw-r--r-- 1 heimbergs 1049089 75288 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/hibernate-commons-annotations-5.0.1.Final.jar
-rw-r--r-- 1 heimbergs 1049089 5570751 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/hibernate-core-5.0.5.Final.jar
-rw-r--r-- 1 heimbergs 1049089 113371 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/hibernate-jpa-2.1-api-1.0.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089 187752 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/jandex-2.0.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089 714194 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/javassist-3.18.1-GA.jar
-rw-r--r-- 1 heimbergs 1049089 85147 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/javax.json-1.0.4.jar
-rw-r--r-- 1 heimbergs 1049089 66802 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/jboss-logging-3.3.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089 63777 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/validation-api-1.1.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089 109318 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-dev/WEB-INF/lib/xml-apis-1.0.b2.jar
-rw-r--r-- 1 heimbergs 1049089 445288 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/antlr-2.7.7.jar
-rw-r--r-- 1 heimbergs 1049089 21007 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/commonj.sdo-2.1.1.jar
-rw-r--r-- 1 heimbergs 1049089 434678 Okt 1 15:03 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/commons-lang3-3.4.jar
-rw-r--r-- 1 heimbergs 1049089 313898 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/dom4j-1.6.1.jar
-rw-r--r-- 1 heimbergs 1049089 9051263 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/eclipselink-2.6.1.jar
-rw-r--r-- 1 heimbergs 1049089 187752 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/jandex-2.0.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089 714194 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/javassist-3.18.1-GA.jar
-rw-r--r-- 1 heimbergs 1049089 85147 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/javax.json-1.0.4.jar
-rw-r--r-- 1 heimbergs 1049089 162126 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/javax.persistence-2.1.0.jar
-rw-r--r-- 1 heimbergs 1049089 66802 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/jboss-logging-3.3.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089 63777 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/validation-api-1.1.0.Final.jar
-rw-r--r-- 1 heimbergs 1049089 109318 Dez 4 13:49 target/stackoverflow-34083152-1.0-SNAPSHOT-test/WEB-INF/lib/xml-apis-1.0.b2.jar
you see the dev war contains the hibernate jars incl transitive dependencies and the test war contains the eclipselink jar incl. transitive dependencies.
i think this is not a good usage of maven. but it should work. and it is not scaleable.. and you must then manage all of the exclude lists for all different war files... the pom.xml could be really fast a mess...
recommendation : perhaps it would be a good idea, to take a closer look at the architecture of the application. a deployment unit should be the same for all environments.
UPDATE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.