Posts by Tom Wetjens

Package-only dependencies in Maven

Posted on by  
Tom Wetjens

Sometimes you have a Maven project that needs dependencies for running tests that you do not want ending up in the final packaged WAR. We all know the test directive in the POM that accomplishes this. You might also have dependencies that are only required at runtime and need to be in the WAR but not on the compile classpath. Normally you would use the runtime directive in the POM. Consider a situation where we have a dependency that we want to be available at runtime (in the WAR), but not on the classpath during the execution of our tests. A nice example of this is logging implementations: we want to use the slf4j-simple implementation for running unit tests, but we want logback-classic to be packaged in the WAR. To accomplish this, you can use the maven-dependency-plugin as illustrated in the following POM snippet:

 org.slf4j
            slf4j-api
            1.7.7 

        
        junit
            junit
            4.11
            test 
        org.slf4j
            slf4j-simple
            1.7.7
            test 
    org.apache.maven.plugins
                maven-dependency-plugin
                package-only-deps
                        
                        prepare-package
                        copy 
                        ch.qos.logback
                                    logback-classic
                                    1.1.2 
                            ${project.build.directory}/${project.build.finalName}/WEB-INF/lib 

Continue reading →

shadow-left