views:

1280

answers:

1

I have a maven POM file for a web service. For one of the dependencies I have to specify several exclusions for jar files that are already kept at a higher-level in the web-application server (accessible to all web-applications, not just this particular one). One example of such exclusion is the JAR containing my JDBC driver.

Example (with fictional details):

<dependency>
    <groupId>mygroup</groupId>
     <artifactId>myartifact</artifactId>
     <version>1.0.0</version>
     <exclusions>
         <!--The jdbc driver causes hot-deployment issues-->
         <exclusion>
             <groupId>db.drivers</groupId>
             <artifactId>jdbc</artifactId>
         </exclusion>
     </exclusions>
 </dependency>

The problem I am encountering is that I need the JDBC driver for my tests. My tests currently fail since they cannot load the JDBC driver.

How can I configure the POM so that the excluded parts are accessible to my tests, but do not get included into my WAR file?

Update: I cannot make changes to the POM for mygroup.myartifact since this it is being depended on by many other projects, and this exclusion requirement is unique for my project.

Update 2: It seems I did a poor job of phrasing this question. Lars's solution below is perfect for one exclusion (as the example shows), however in my real scenario I have multiple exclusions, and adding additional dependencies for each seems smelly. The solution that seems to work is to set the scope of the shown dependency to compile and then create a second dependency the same artifact (mygroup.myartifact) with no exclusions and the scope set to test. Since Lars both answered my poorly phrased question correctly, as well as led me in the direction of the actual solution, I will mark his reply as the answer.

+5  A: 

Use the "scope" tag inside your dependency.

<scope>test</scope>

http://maven.apache.org/pom.html#Dependencies

edit: if I understand your configuration correctly, the scope=test that you need to add should be added in the mygroup.myartifact POM. That way you can test that artifact with jdbc jar included, but always when other POMS want to include mygroup.myartifact, they don't get jdbc included as a transitive dependency.

second edit: Ok, if you don't control the POM you want to include - do an exclusion like you have already done, and then add jdbc as a new dependency, with scope=test.

Lars Westergren
Wouldn't that cover the entire dependency, I'm merely talking about the exclusions here. I'm only excluding parts of the dependency, but those are parts that I need for testing.
Einar
Unfortunately it is not an option to update that POM. I updated the question to clarify this.
Einar
Tack så mycket :)
Einar
Inget problem. Happy to help.:)
Lars Westergren