Reputation: 49
I have a JAR that depends on another JAR that is provided by the application container (so it is not included in the WAR file). But it needs to be included in the package when that component is used in a different standalone application. What is the proper way to specify that a JAR is provided by the application container so it should not be included in an EAR or WAR file, but included when packaged in a standalone application?
Here is the dependency tree:
WAR(app1) JAR(app2)
+ +
| |
v v
Dependency
+
|
v
Dependency (provided by the application server)
This is the pom.xml of the JAR that depends on a JAR provided by the application container, but required if building a standalone application.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testgroup</groupId>
<artifactId>moduledao</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
</dependencies>
</project>
This is the pom.xml of the web application:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testgroup</groupId>
<artifactId>mywebapp</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>testgroup</groupId>
<artifactId>moduledao</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
This is the pom.xml of the standalone application:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testgroup</groupId>
<artifactId>standaloneapp</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>testgroup</groupId>
<artifactId>moduledao</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
Upvotes: 1
Views: 102
Reputation: 4847
You need three projects:
war
packagingIn core pom.xml
you define all dependencies as they are required for your application logic, with default scope.
In web pom.xml
you define dependency on core with default scope, and additionally you define all dependencies that are provided by container with <scope>provided</scope>
. These additional dependencies will basically override dependencies in core and will end up not being bundled in .war
. If you have any tests, they will run with the provided
dependencies - this can be important if the versions are different and you want to make sure your app works with the versions that will be on the application server. This project might also contain some additional code for running within application container.
In standalone pom.xml
you define dependency on core, and additionally any dependencies necessary for it to run standalone, such as command-line parsing library, etc. Additionally you'll define here settings for running this as standalone application, such as maven-jar-plugin
to include main class in manifest. This project might also contain some additional code for running standalone.
Upvotes: 0
Reputation: 10463
As others said, you need to set the scope.
BUT
In order to use it in 2 different cases, you need to declare 2 profiles, the application container based one and the standalone application one which will have a propery named e.g. profile.scope
.
In first case it will be provided
and compile
in second and you must use each profile respectively depending on the case you need to run.
sth like this
<profile>
<id>appServer</id>
<profile.scope>provided</profile.scope>
</properties>
</profile>
<profile>
<id>standaloneApp</id>
<profile.scope>compile</profile.scope>
</properties>
</profile>
and then
<scope>${profile.scope}</scope>
Upvotes: 3