如何使用Grunt将jar文件复制到WEB-INF/lib目录

9
我是一名新手使用Grunt构建。我的要求是,在使用Grunt构建进行war任务时,创建WEB-INF / lib目录并将Jar文件复制到其中。
以下是我的war.js示例代码:
module.exports = {
/*
 * Build a WAR (web archive) without Maven or the JVM installed.
 */

target: {
    options: {
        war_dist_folder: 'deploy',
        /* Folder to generate the WAR into */
        war_name: 'mySampleApp',
        /* The name fo the WAR file (.war will be the extension) */
        webxml_webapp_version: '2.5',
        war_extras: [{
                filename: 'WEB-INF/weblogic.xml',
                data: '<?xml version = "1.0" encoding = "US-ASCII"?> \n\n\
                        <weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \n\n\
                        xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" \n\n\
                        xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app"> \n\n\
                            <context-root>my-sample-app</context-root> \n\n\
                            <session-descriptor> \n\n\
                              <timeout-secs>1800</timeout-secs> \n\n\
                               <cookie-name>JSESSIONID</cookie-name> \n\n\
                                <cookie-path>/my-sample-app</cookie-path>  \n\n\
                                <url-rewriting-enabled>false</url-rewriting-enabled>  \n\n\
                            </session-descriptor> \n\n\
                        </weblogic-web-app>'
            }],
        /* the war_extras are extra files to be generated, needed since grunt-war doesn't create a weblogic.xml */
        webxml_welcome: 'index.html',
        /* to point web.xml to the default page */
        webxml_webapp_extras: ['<login-config />\n', 
                               '<session-config>\n    \n\
                                    <session-timeout>\n    30\n    </session-timeout>\n\n\
                                </session-config>\n',
                                '<servlet>\n   \n\
                                    <servlet-name>\n    MyServlet\n    </servlet-name>\n\n\
                                    <servlet-class>com.sample.servlet.MyServlet</servlet-class>\n\n\
                                </servlet>\n',
                                '<servlet-mapping>\n\
                                    <servlet-name>MyServlet</servlet-name>\n\
                                    <url-pattern>/maySampleApp</url-pattern>    \n\
                                </servlet-mapping>'
                              ]

    },
    files: [{
            expand: true,
            cwd: 'release',
            /* find the source files for the WAR in the /release folder */
            src: ['**'],
            dest: ''
        }]
    }
};

请为我提供创建WEB-INF/lib目录并将jar文件复制到其中的指令。

4
如果您编辑问题并提供更多细节,例如:1. 显示您的源目录结构,2. 指示.jar文件的位置,即您想要复制的文件。3. 指示您希望在生成的mySampleApp.war文件的WEB-INF/lib目录中如何组织所复制的.jar文件,则可能会增加获得答案/解决方案的机会。 - RobC
1
此外,您在这里的目的不太清楚。Grunt是一种“前端构建工具(JavaScript)”部分。WEB-INF/lib通常是“项目构建工具(Maven/Gradle/Ant/Ivy)”生命周期的一部分。除非您想完全用grunt替换“常见的Java构建工具”,否则我认为您真正想使用的至少是类似于maven-frontend-plugin的东西:https://github.com/eirslett/frontend-maven-plugin。 - xsalefter
1个回答

2

你是否尝试过像这样的“简单”操作(files 接受一个“数组”参数...这个特定选项字段并没有真正记录在grunt-war文档中):

// ...,
files: [{
        expand: true,
        // better ...no cwd, "copy single file tree" @see [2]
        src: ['release/*'],
        dest: ''
    }, // a second "files" object! (and my particular answer)
    {
        // expand: false, assuming/hoping for a flat *.jar structure (all in one folder)
        // cwd: '', NO cwd ...
        /* GET all files with "lib/*.jar" "matcher" */
        src: ['lib/*.jar'],
        /* ... and "destinate" into "WEB-INF/lib" */
        dest: 'WEB-INF/lib'
    }
] // ...

没有保修,没有测试,只有一次尝试的机会! :)

当你的“lib”文件夹结构过于复杂时,你需要使用epxandcwd,参见:[1][2],@RobC的评论。

如果所有这些都无法解决grunt-war插件的问题,我建议在执行war之前将(jars) 复制到“release/WEB-INF/lib”中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接