简体   繁体   English

使用Gradle从webapp创建包含类和资源的JAR

[英]Create a JAR containing classes and resources from webapp using Gradle

I would like to create a jar from the contents of a WAR using gradle. 我想使用gradle从WAR的内容创建一个jar。 The result I want is quite like what the archiveClasses = true setting of maven-war-plugin does. 我想要的结果非常类似于maven-war-pluginarchiveClasses = true设置

I understand that the gradle war plugin doesn't seem to do this (as per this stackoverflow question ). 我知道gradle war插件似乎没有这样做(根据这个stackoverflow问题 )。

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? 比如说操纵gradle jar任务从WEB-INF文件夹中收集所需的部分?

When I just use the default jar task it doesn't get the resources from the WEB-INF directory. 当我只使用默认的jar任务时,它不会从WEB-INF目录中获取资源。

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). 我想这样做的原因是因为我已经模块化了一个WAR,模块依赖于WAR中的公共资源(在我的例子中是FreeMarker文件)。 I want to be able to test each module by depending on JAR created from the WAR. 我希望能够通过依赖于从WAR创建的JAR来测试每个模块。

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. 我知道我也可以创建一个“通用”jar,它拥有所有资源并且WAR和模块都依赖于此,但是根据maven-war-plugin创建一个WAR的JAR会更方便。

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. 我也在Gradle论坛中找到了这个替代方案 ,但是我无法获得行resources.srcDir("src/main/webapp").exclude('images/')来编译,但我真的很喜欢以某种方式添加到jar任务的默认资源目录。 Edit : I think my first example above is as good as it gets. 编辑 :我认为上面的第一个例子就像它得到的一样好。

You can add this to include both src/main/resources and scr/main/webapp in the root of the created jar file: 您可以添加此项以在创建的jar文件的根目录中包含src / main / resources和scr / main / webapp:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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