Search code examples
eclipsegradleeclipse-wtp

Sharing resources between multiple Eclipse WTP projects with Gradle


I'm attempting a modular Eclipse (Mars) setup where there are core resources in one project used by different child projects.

core/
  src
    main/
      webapp/
        js/
          subfolder
            another-core.js
          core.js
        css/
          default.css
child/
  src/
    main/
      webapp/
        js/
          project.js
        WEB-INF/
        project.html

.settings/org.eclipse.wst.common.component looks like this

<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="2.0">
    <wb-module deploy-name="child">
        <property name="context-root" value="child"/>
        <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="/" source-path="../core/src/main/webapp/"/>
    </wb-module>
</project-modules>

Eclipse deploys to its wtpwebapps folder the following structure, i.e. only the top level directories from core. I'm expecting css/ to contain default.css and js/ to contain both core.js and project.js

child/
   css/    <== must have come from core
   js/
     project.js
   WEB-INF/
   project.html

If I change the .settings to explicitly list the folders then it works as expected merging the folders put again only at the top level

<wb-resource deploy-path="/" source-path="../core/src/main/webapp/"/>
<wb-resource deploy-path="/js" source-path="../core/src/main/webapp/js"/>
<wb-resource deploy-path="/css" source-path="../core/src/main/webapp/css"/>

wtpwebapps/ contents

child/
  css/
    default.css
  js/
    core.js
    project.js
    subfolder/   <== missing another-core.js
  WEB-INF/
  project.html

Is there anyway to get eclipse to do this without listing all the folders separately?

Another limitation of external wb-resources is that when they overlap, for example including "js/subfolder" and "js/" Eclipse automatically removes the "js/subfolder"


Solution

  • The only way I believe this is possible is via the use of linked resources, as these support fully recursive source directories, unlike relative paths which only include their top-level. However Gradle appears to have an issue where linked resources are excluded if included as follows:

    eclipse.project {    
        linkedResource name: 'core', type: '2', location: new File(project(':core').projectDir, "src/main/webapp").absolutePath
    }
    eclipse {
        wtp {
            component {
                resource sourcePath: "/core", deployPath: '/'
            }
        }
    }
    

    However, there is a withXml hook which can be used to force the correct XML into org.eclipse.wst.common.component.xml

    eclipse {
        wtp {
            component {
                file {
                    withXml {
                        def node = it.asNode().children()[0]
                        node.appendNode('wb-resource', ["deploy-path": '/', "source-path": '/core'])
                    }
                }
            }
        }
    }