Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

providedCompile without war plugin

Tags:

gradle

I want to reuse certain filter for many projects so I want to extract it and use a single jar to just add it to any Web App.

For building I am using Gradle 1.3 and the following build.gradle file:

apply plugin: 'java'

dependencies {

    compile group:'org.slf4j', name:'slf4j-api', version:'1.7.+'

    testCompile group:'junit', name:'junit', version:'4.+'

    compile group:'org.springframework', name:'spring-web', version:'3.+'   

    compile group:'org.slf4j', name:'slf4j-log4j12', version:'1.6.+'
    compile group:'log4j', name:'log4j', version:'1.2.+'

    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version:'3.+'        

}

repositories {

    mavenCentral()

}

As you can see, I need the servlet api to compile this filter succesfully so I want to add it like a maven provided dependency.

Anyways, after running gradle build I get the following error:

Could not find method providedCompile() for arguments [{group=javax.servlet, name=javax.servlet-api, version=3.+}] on root project 'hibernate-conversation-context'.

Now, I know that I cannot use providedCompile without the WAR plugin but I need the project to be a simple JAR. Is there another way to do this?

like image 830
ElderMael Avatar asked Dec 18 '12 03:12

ElderMael


People also ask

What is gradle war plugin?

The War plugin extends the Java plugin to add support for assembling web application WAR files. It disables the default JAR archive generation of the Java plugin and adds a default WAR archive task.


7 Answers

There is no such configuration out of the box for the java plugin. However you can build it yourself as follows:

configurations { providedCompile }

dependencies {
    providedCompile "javax.servlet:javax.servlet-api:3.+"
}

sourceSets.main.compileClasspath += configurations.providedCompile
sourceSets.test.compileClasspath += configurations.providedCompile
sourceSets.test.runtimeClasspath += configurations.providedCompile

This adds the configuration, and puts all the dependencies in there on the compile classpaths of both your main classes and test classes. You also need to add it to the runtimeClasspath, as that doesn't include the compile classpath according to the gradle DSL documentation.

like image 147
Hiery Nomus Avatar answered Sep 27 '22 21:09

Hiery Nomus


Take a look at the Gradle plugin propdeps from SpringSource. From the project description:

Provides additional optional and provided dependency configurations for Gradle along with Maven POM generation support.

like image 25
Gerhard Schlager Avatar answered Sep 27 '22 20:09

Gerhard Schlager


As of Gradle 2.12 the issue of defining compile only dependencies is finally solved in a simple and natural manner by the new "copmpileOnly" configuration:

dependencies {
    compileOnly 'javax.servlet:servlet-api:2.5'
}
like image 37
Doron Gold Avatar answered Sep 27 '22 20:09

Doron Gold


I wrote a blog post recently which covers exactly this scenario. It also shows you how to get integration with Eclipse set up properly too.

http://blog.codeaholics.org/2012/emulating-mavens-provided-scope-in-gradle/

like image 24
dty Avatar answered Sep 27 '22 20:09

dty


When you find yourself using Gradle 2.12, change 'providedCompile' to 'compileOnly'.

like image 23
ginzsa Avatar answered Sep 27 '22 21:09

ginzsa


There is an easier way:

configurations {    
    provided
    provided.extendsFrom(compile)
}

and then you can:

dependencies {  
    provided group: 'javax.servlet', name: 'javax.servlet-api', version:'3.+' 
}

You will also want to have the provided libraries in the generated project files of Eclipse or Idea:

idea.module.scopes.PROVIDED.plus += configurations.provided
eclipse.classpath.plusConfigurations += configurations.provided
like image 25
Dima Gutzeit Avatar answered Sep 27 '22 19:09

Dima Gutzeit


To make the javadoc work I had to add this:

javadoc.classpath=sourceSets.main.compileClasspath
like image 45
Peter Borbas Avatar answered Sep 27 '22 20:09

Peter Borbas