How to check Gradle CopySpec for export to text view?

I use an application server function that allows me to deploy resources and classes directly from Gradle output folders and static files from source folders. I don’t want to waste time packing EAR , WAR or JAR , and I want to use HotSwap functions without redistributing them.

Let me demonstrate the idea using a simple Gradle and Tomcat 7 project.

Suppose we have a folder with the following structure:

.
|-- build.gradle
`-- src
    |-- css
    |   `-- main.css
    |-- html
    |   `-- index.html
    `-- images
        `-- logo.png

4 directories, 4 files

I am creating a simple Gradle project:

build.gradle

apply plugin: 'war'

war {
  destinationDir temporaryDir
  into 'META-INF/resources', {
    from 'src/html/index.html'
  }
  into 'META-INF/resources/static', {
    from 'src/css'
  }
  into 'META-INF/resources/static', {
    from 'src/images'
  }
}

Then it would be great to be able to automatically create the following configuration:

$ CATALINA_HOME / Catalina / local / myApp.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="">
  <Resources className="org.apache.naming.resources.VirtualDirContext" extraResourcePaths="
    /META-INF/resources/index.html=C:/myapp/src/html/index.html,
    /META-INF/resources/static=C:/myapp/src/css,
    /META-INF/resources/static=C:/myapp/src/images
  "/>
</Context>

, ( ), :

/META-INF/resources/index.html=C:/myapp/src/html/index.html
/META-INF/resources/static/main.css=C:/myapp/src/css/main.css,
/META-INF/resources/static/logo.png=C:/myapp/src/images/logo.png

, .

1

issue Gradle github

2

, - , . java.

- CopySpec . CopySpec ( CopySpec), , .

+4
3

,

+1

eachFile , , doLast XML.

Guava Multimap, ( ) .

script stdout, :

buildscript {
    repositories { jcenter() }
    dependencies { classpath 'com.google.guava:guava:18.0' }
}

import com.google.common.collect.ArrayListMultimap
import groovy.xml.MarkupBuilder

apply plugin: 'war'

war {
    destinationDir temporaryDir
    into 'META-INF/resources', {
        from 'src/html/index.html'
    }
    into 'META-INF/resources/static', {
        from 'src/css'
    }
    into 'META-INF/resources/static', {
        from 'src/images'
    }

    def parentDirs = ArrayListMultimap.create()
    eachFile { file ->
        parentDirs.put(file.relativePath.parent, file.file.parent)
    }

    doLast {
        def lines = []
        for (e in parentDirs.entries()) {
           lines.add("$e.key=$e.value")
        }

        def writer = new StringWriter()
        def xml = new MarkupBuilder(writer)
        xml.Context(docBase:"") {
            Resources(className:"org.apache.naming.resources.VirtualDirContext",
                extraResourcePaths:lines.join(","))
        }
        println writer
    }
}
0

? Gradle ( Maven)? :

  • src/html/index.html src/main/webapp/META-INF/resources
  • src/css/*.* src/main/webapp/META-INF/resources/static/*.*
  • src/images/*.* src/main/webapp/META-INF/resources/static/*.*
  • into ... from ... war ( )
  • / C:/myapp/src/main/webapp $CATALINA_HOME/Catalina/localhost/myapp.xml

War - src/main/webapp

0

Source: https://habr.com/ru/post/1668529/


All Articles