Create a JAR containing classes and resources from webapp using Gradle

谁说胖子不能爱 提交于 2020-01-03 09:04:20

问题


I would like to create a jar from the contents of a WAR using gradle. The result I want is quite like what the archiveClasses = true setting of maven-war-plugin does.

I understand that the gradle war plugin doesn't seem to do this (as per this stackoverflow question).

Is there a way to do this manually? Say by manipulating the gradle jar task to collect the required parts from the WEB-INF folder?

When I just use the default jar task it doesn't get the resources from the WEB-INF directory.

The reason I want to do this is because I have modularised a WAR and the modules depend on common resources in the WAR (FreeMarker files in my case). I want to be able to test each module by depending on JAR created from the WAR.

I understand I could also create a "common" jar that held all the resources and have both the WAR and the module depend on this, but it would be more convenient to create a JAR form the WAR as per the maven-war-plugin.


回答1:


You can add this to include both src/main/resources and scr/main/webapp in the root of the created jar file:

sourceSets {
    main {
        resources {
            srcDirs "src/main/resources", "src/main/webapp"
        }
    }
}



回答2:


So here is how I solved it (not sure if this is the best way):

jar {
    from ("${projectDir}/src/main/webapp/WEB-INF") {
        include('freemarker/**')
    }
}

I also found this alternative in the Gradle forums but I couldn't get the line resources.srcDir("src/main/webapp").exclude('images/') to compile, but I really like the idea of somehow just adding to the jar task's default resources directories. Edit: I think my first example above is as good as it gets.



来源:https://stackoverflow.com/questions/21175585/create-a-jar-containing-classes-and-resources-from-webapp-using-gradle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!