简体   繁体   中英

Query for Gradle dependency for WAR plugin

I am new to Gradle. With Gradle 3.5, I am trying to create a war build from a java project. Below is my gradle file content.

apply plugin: 'war'

buildDir = "${rootProject.ext.buildGradle}/${project.name}"
def buildClassesDir = buildDir.getAbsolutePath() + '/classes/main'

configurations {
    localLibraries
}

task copyNonJava(type: Copy, dependsOn: compileJava) {
    from ('src/main/java') {
        exclude '**/*.java'
        include '**/*.properties'
    }
    from ('resources') {
        include 'default_content.properties'
    }

    into buildClassesDir

    includeEmptyDirs = false
}

task bundleJar (type: Jar, dependsOn: ['compileJava', 'copyNonJava']) {
    baseName archivesBaseName
    from buildClassesDir
}

task bundleWar (type: War, dependsOn: ['bundleJar']) {
    baseName = project.name
    from 'web'
    webXml = file( 'resources/WEB-INF/web.xml' )
    classpath = configurations.localLibraries
}

dependencies {
    compile group: 'com.system', name: 'core', version: rootProject.version, changing: true
    compile group: 'com.system', name: 'core-ui', version: rootProject.version, changing: true
    compile group: 'com.persistence', name: 'persistence', version: '1.0'
    compile group: 'com.surveys', name: 'survey', version: '1.0'

    localLibraries fileTree("lib") {
        exclude 'spring*'
    }
}

When I generate war build, it adds jar files under WEB-INF/lib directory. But along with those jar files I want jar files from com.system group and jar file generated from bundleJar task. How can I achieve this?

Libraries from the compile configuration are included in classpath by default but classpath = configurations.localLibraries overrides the default value.

Instead of overriding the default classpath ( classpath = ... effectively means setClasspath(...) ) you can append additional files to it :

task bundleWar (type: War, dependsOn: ['bundleJar']) {
  baseName = project.name
  from 'web'
  webXml = file( 'resources/WEB-INF/web.xml' )
  classpath configurations.localLibraries
  classpath bundleJar.archivePath
}

Thank you for the response on this. Really appreciate.

I have figured out a way from this. Learned that gradle follows some directory conventions so make changes to directory structure supported by gradle and made changes to the gradle script.

Here is working gradle script:

apply plugin: 'war'

buildDir = "${rootProject.ext.buildGradle}/${project.name}"
def buildClassesDir = buildDir.getAbsolutePath() + '/classes/main'

configurations {
    localLibraries
}

task bundleJar (type: Jar, dependsOn: ['compileJava']) {
    baseName archivesBaseName
    from buildClassesDir
}

task bundleWar (type: War, dependsOn: ['bundleJar']) {
    dependsOn = [ 'bundleJar' ]
    baseName = project.name
    from 'web'
    webXml = file( 'resources/WEB-INF/web.xml' )
}

dependencies {
    compile group: 'com.system', name: 'core', version: rootProject.version, changing: true
    compile group: 'com.system', name: 'core-ui', version: rootProject.version, changing: true
    compile group: 'com.persistence', name: 'persistence', version: '1.0'
    compile group: 'com.surveys', name: 'survey', version: '1.0'

    // Replace this and make sure all necessary jar dependencies are been fetched from repository instead from file system
    /*localLibraries fileTree("lib") {
        exclude 'spring*'
    }*/
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM