3

Using Spring Tool Suite 3.5 with gradle plugin, gradle 1.11

I want to achieve this: extract the content of project dependend library into WAR file both when running gradle build from command line and when deploying from Eclipse into Tomcat.

I have my build.gradle project like this:

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'

webAppDirName =  'WebContent'

configurations{
    common
}

eclipse{
    wtp {
        facet{
            facet name: 'jst.web', version: '3.0'
            facet name: 'java', version: '1.7'
        }
    }
}

war{
    from ({zipTree(configurations.common.singleFile)}){
        into('common')
    }
}   

dependencies {
    common "group:artifact:version:common@jar"
}

Eclipse doesn't extract the configurations.common as prescribed in the war{} section, so I had to change the script slightly:

eclipse{
        wtp{
            facet{
                ...
            }
            component{
                resource sourcePath: "build/common", deployPath: "/common"
            }
        }
}

task extractCommon(type:Copy){
    from {
        configurations.common.collect{ zipTree(it) }
    }
    exclude 'META-INF/**'
    into "$buildDir/common"
}

eclipseClasspath.dependsOn(extractCommon)

This way the dependend JAR gets extracted before re-creating the eclipse project files and settings, but unfortunately the webAppDirName disappears from the Deployment Assembly settings in eclipse project.

So another workaround:

eclipse{
        wtp{
            facet{
                ...
            }
            component{
                resource sourcePath: "build/common", deployPath: "/common"
                resource sourcePath: "$webAppDirName", deployPath: "/"
            }
        }
}

This way the eclipse behaves as intended, but it looks cumbersome and I feel that I am missing something and that eclipse-wtp plugin should be doing this automatically somehow.

Is there easier way to achieve:

  1. Eclipse to follow the prescribed behavior defined in war{} block when deploying from Eclipse (extracting some JARs before creating the web app).
  2. Define resources in an incremental way without purging the webAppDirName folder from the deployment

EDIT: Let me rephrase the problem:

If I have a war project with typical directory structure

src
 main
  java
  webapp
generated_source

And build.gradle like this:

apply plugin:'java'
apply plugin:'war'
apply plugin:'eclipse-wtp'

eclipse.wtp.component.resource(sourcePath:'generated_source', deployPath:'/WEB-INF')

Then after running gradle cleanEclipse eclipseWtpComponent the resulting file - .settings/org.eclipse.wst.common.component

Should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="2.0">
    <wb-module deploy-name="gradleissue">
        <property name="context-root" value="gradleissue"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/java"/>
        <wb-resource deploy-path="/" source-path="src/main/webapp"/>
        <wb-resource deploy-path="/WEB-INF/" source-path="generated_source"/>
    </wb-module>
</project-modules>

But it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="2.0">
    <wb-module deploy-name="gradleissue">
        <property name="context-root" value="gradleissue"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/java"/>
        <wb-resource deploy-path="/WEB-INF/" source-path="generated_source"/>
    </wb-module>
</project-modules>

The /src/main/webapp folder disappeared from the deployment assembly. I have a bad feeling, that this is not what it's supposed to do. I also checked the tests in https://github.com/gradle/gradle/blob/master/subprojects/ide/src/test/groovy/org/gradle/plugins/ide/eclipse/EclipseWtpPluginTest.groovy and it looks like this case is missing (I am not a groovy guy).

Fanick
  • 713
  • 7
  • 25
  • Found related question: http://stackoverflow.com/questions/21658116/eclipse-doesnt-deploy-gradle-project-properly – Fanick Apr 09 '14 at 09:10
  • 1
    run into my own question when trying to solve this problem month later - unbelievable :) – Fanick May 20 '14 at 12:09
  • 1
    This is a reported bug, see https://issues.gradle.org/browse/GRADLE-2894 and I created a pull request fixing this, see github.com/gradle/gradle/pull/368. – Andreas Schmid Dec 14 '14 at 13:51

1 Answers1

0

Did you add the "generated_source" directory to you source set? I have similar project setup but i organized my generated source directory in a different way:

src/main
  java
  generatedJava
  webapp

Then you only need to add the generatedJava directory to the source set:

sourceSets.main.java {
        // added additional source folder for generated classes
        srcDir "${rootDir}/src/main/generatedJava"
}

This leads to the expected eclipse wtp configuration:

<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="2.0">
    <wb-module deploy-name="myproject">
        <property name="context-root" value="myproject"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/resources"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/java"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="src/main/generatedJava"/>
        <wb-resource deploy-path="/" source-path="src/main/webapp"/>
    </wb-module>
</project-modules>

You can find a working example build at github: https://github.com/jereh16/gradle-example-build

Jens
  • 241
  • 2
  • 9
  • That won't help if you want to deploy resource to some other directory (e.g. `META-INF` in my case..) – bompf Aug 30 '16 at 20:58